John Davidson

Php multiple image uploader with class

0 comments
Message:


What am i doing wrong? I cannot send multiple files, only one. I cannot pass an array multiple files into a function.


Uploading files to the server and saving into sql database - it works.


form


<input type="file" name="product_images[]"


new.php


$photo_multi = new Photographmultiple();

$id = $database->insert_id;

$files = 'product_images';

$photo_multi->upload_file($files, $id);

class Photographmultiple


public function upload_file($field_name, $id) {

if(isset($_FILES[$field_name])) {

foreach ($_FILES[$field_name]['name'] as $key => $val) {

$image = ($_FILES[$field_name]['name'][$key]);
$target_path = $this->upload_dir . '/' . $image;
move_uploaded_file($_FILES[$field_name]['tmp_name'][$key], $target_path);

$sql = "INSERT INTO " . self::$table_name . " (";
$sql .= "product_id, product_images";
$sql .= ") VALUES ('";
$sql .= self::$database->escape_string($id) . "', '";
$sql .= self::$database->escape_string($image) . "')";
//echo $sql;
$result = self::$database->query($sql);
return $result;
}
}

}


My Answer:




$name) {
$tmp_name = $files['tmp_name'][$key];
$target_dir = "uploads/";
$target_file = $target_dir . basename($name);
move_uploaded_file($tmp_name, $target_file);
// Save $target_file to database
}
}
}
if(isset($_FILES['files'])) {
$uploader = new ImageUploader();
$uploader->uploadImages($_FILES['files']);
}
?>

Rate this post

3 of 5 based on 4770 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us