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:
Rate this post
3 of 5 based on 4770 votesComments