I am using this for multi image upload. https://github.com/CreativeDream/jquery.filer
Since it doesn't show example of how to use it with a database, I am having a bit of an issue. The following is my code. Couple things.
1. It won't insert into database with the "tokens". If I remove the token code, then it'll work. The same token works in other forms that I use.
2. Yes It inserts into the database. The issue is that it inserts only 1 file at a time and not all the selected files.
<?php require_once ($_SERVER['DOCUMENT_ROOT'] . '/home/templates/header.php');
if(isset($_POST['submit'])) {
if($_POST['token'] === $_SESSION['token']) {
if(isset($_FILES['files'])){
$date_added = date('Y-m-d H:i:s');
// Setting up folder for images
$user_dir = $_SERVER['DOCUMENT_ROOT'] .'/home/members/images/'.$db_userid.'/records/';
if(!is_dir($user_dir)){
mkdir($_SERVER['DOCUMENT_ROOT'] .'/home/members/images/'.$db_userid.'/records/', 0775, true);
} else {
$uploader = new Uploader();
$data = $uploader->upload($_FILES['files'], array(
'limit' => 10, //Maximum Limit of files. {null, Number}
'maxSize' => 10, //Maximum Size of files {null, Number(in MB's)}
'extensions' => null, //Whitelist for file extension. {null, Array(ex: array('jpg', 'png'))}
'required' => false, //Minimum one file is required for upload {Boolean}
'uploadDir' => '../uploads/', //Upload directory {String}
'title' => array('auto', 10), //New file name {null, String, Array} *please read documentation in README.md
'removeFiles' => true, //Enable file exclusion {Boolean(extra for jQuery.filer), String($_POST field name containing json data with file names)}
'perms' => null, //Uploaded file permisions {null, Number}
'onCheck' => null, //A callback function name to be called by checking a file for errors (must return an array) | ($file) | Callback
'onError' => null, //A callback function name to be called if an error occured (must return an array) | ($errors, $file) | Callback
'onSuccess' => null, //A callback function name to be called if all files were successfully uploaded | ($files, $metas) | Callback
'onUpload' => null, //A callback function name to be called if all files were successfully uploaded (must return an array) | ($file) | Callback
'onComplete' => null, //A callback function name to be called when upload is complete | ($file) | Callback
'onRemove' => 'onFilesRemoveCallback' //A callback function name to be called by removing files (must return an array) | ($removed_files) | Callback
));
if($data['isComplete']){
$files = $data['data'];
print_r($files);
}
if($data['hasErrors']){
$errors = $data['errors'];
print_r($errors);
}
function onFilesRemoveCallback($removed_files){
foreach($removed_files as $key=>$value){
$file = '../uploads/' . $value;
if(file_exists($file)){
unlink($file);
}
}
return $removed_files;
}
for($i = 0; $i < count($_FILES['files']['name']); $i++) {
$name = $_FILES['files']['name'][$i];
$temp = $_FILES['files']['tmp_name'][$i];
$ext = pathinfo($name, PATHINFO_EXTENSION);
$upload = md5( rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ));
$image_thumb_path = $user_dir . 'thumb_' . $upload . $ext;
try {
$insert_image = $db->prepare("INSERT INTO images(user_id, image_path, date_added)
VALUES(:user_id, :image_path, :date_added)");
$insert_image->bindParam(':user_id', $db_userid);
$insert_image->bindParam(':image_path', $image_thumb_path);
$insert_image->bindParam(':date_added', $date_added);
$result_image = $insert_image->execute();
if($result_image == false) {
$error = 'There was a problem inserting images!';
} else {
move_uploaded_file($temp, $image_thumb_path);
$success = 'Your images has been saved.';
}
} catch(Exception $e) {
$error = die($e->getMessage());
}
}
}
}
}
}
?>
<h1>Upload Images</h1>
<?php include_once ($dir_path . '/home/snippets/success-errors.php'); ?>
<form action="" method="post" enctype="multipart/form-data">
<fieldset>
<input type="file" multiple="multiple" name="files[]" id="input2">
<fieldset>
<input type="hidden" name="token" value="<?php echo $_SESSION['token'] = md5(rand(time (), true)); ?>" />
<input type="submit" name="submit" class="red-btn" value="upload" />
</fieldset>
</form>
<?php require_once ($_SERVER['DOCUMENT_ROOT'] . '/home/templates/footer.php');