How to use jQuery-File-Upload with PHP and database? I want to insert or delete rows about images when I upload or delete images and that name of each image will be as time() when they are uploaded to the directory.
All result I found through google tell me that I need edit to upload.class.php but the last release has index.php and UploadHandler.php only...
file UploadHandler.php has class UploadHandler with code
public function post($print_response = true) {
if (isset($_REQUEST['_method']) && $_REQUEST['_method'] === 'DELETE') {
return $this->delete($print_response);
}
$upload = isset($_FILES[$this->options['param_name']]) ?
$_FILES[$this->options['param_name']] : null;
// Parse the Content-Disposition header, if available:
$file_name = isset($_SERVER['HTTP_CONTENT_DISPOSITION']) ?
rawurldecode(preg_replace(
'/(^[^"]+")|("$)/',
'',
$_SERVER['HTTP_CONTENT_DISPOSITION']
)) : null;
$file_type = isset($_SERVER['HTTP_CONTENT_DESCRIPTION']) ?
$_SERVER['HTTP_CONTENT_DESCRIPTION'] : null;
// Parse the Content-Range header, which has the following form:
// Content-Range: bytes 0-524287/2000000
$content_range = isset($_SERVER['HTTP_CONTENT_RANGE']) ?
preg_split('/[^0-9]+/', $_SERVER['HTTP_CONTENT_RANGE']) : null;
$size = $content_range ? $content_range[3] : null;
$info = array();
if ($upload && is_array($upload['tmp_name'])) {
// param_name is an array identifier like "files[]",
// $_FILES is a multi-dimensional array:
foreach ($upload['tmp_name'] as $index => $value) {
$info[] = $this->handle_file_upload(
$upload['tmp_name'][$index],
$file_name ? $file_name : $upload['name'][$index],
$size ? $size : $upload['size'][$index],
$file_type ? $file_type : $upload['type'][$index],
$upload['error'][$index],
$index,
$content_range
);
}
} else {
// param_name is a single object identifier like "file",
// $_FILES is a one-dimensional array:
$info[] = $this->handle_file_upload(
isset($upload['tmp_name']) ? $upload['tmp_name'] : null,
$file_name ? $file_name : (isset($upload['name']) ?
$upload['name'] : null),
$size ? $size : (isset($upload['size']) ?
$upload['size'] : $_SERVER['CONTENT_LENGTH']),
$file_type ? $file_type : (isset($upload['type']) ?
$upload['type'] : $_SERVER['CONTENT_TYPE']),
isset($upload['error']) ? $upload['error'] : null,
null,
$content_range
);
}
return $this->generate_response($info, $print_response);
}
public function delete($print_response = true) {
$file_name = $this->get_file_name_param();
$file_path = $this->get_upload_path($file_name);
$success = is_file($file_path) && $file_name[0] !== '.' && unlink($file_path);
if ($success) {
foreach($this->options['image_versions'] as $version => $options) {
if (!empty($version)) {
$file = $this->get_upload_path($file_name, $version);
if (is_file($file)) {
unlink($file);
}
}
}
}
return $this->generate_response($success, $print_response);
}
What rows do I need to add to insert or delete file names in mysql?
P.S.: I use PHP
I use docementation and now can said that instead file upload.class.php need edit file UploadHandler.php and than use next:
Search this line - > $this->options = array(
Add the following Code in the next lines :
So now you have to write a function for the SQL Query, copy & paste the following code for example after the handle_file_upload function :
Add file details to database I explain this function with a picture upload, so here we save the picture name to the database Add this function also too the upload.class.php
so in this function we call the function query with the string between the clamps.
You could also insert other details too, for example, the file size.
At least we have to call this function, with the following code at the end of the function handle_file_upload. Paste the following code underneath or over this line : $file->size = $file_size;
Delete the entry we created Deleting the entry we made before is very easy, we create a new function which makes also an sql query to delete it.
Now we must call the function, this time of the delete function. Go to the delete function and search this line : if ($success) { paste the following code over this.
Enjoy=)
You should overload the default methods (as described in the documentation) in order to add your custom features. If you modify original files, you'll have more work when a new version (or a fix) of the plugin is released.
For my own purposes, I basically defined a custom class which extends the original one and modified only the /server/php/index.php file: