For a car dealer website I have a upload form to upload multiple photos at once, that is working fine I but I have no idea how to store (what kind of query to use) the image names in the database. All images have the same foreign key occasion_id. So the occasion_id, and the image names have to be stored. In the form the input name is file
When the photos are uploaded I assume you are either moving or manipulating them in some way. At this point I again assume you have a foreach or for loop.
I would just add a pdo insert into the loop so that the file name is saved with the photo.
You would need to display your upload code for any further advice as it is all assumptions. Also how are the photo paths and other details linked in the database as you probably need a separate table linking the car ID to the photo.
include 'db.php';
if(isset($_FILES['image'])){
$errors= array();
$tablename = "files";
$file_name = $_FILES['image']['name'];
$file_size =$_FILES['image']['size'];
$file_tmp =$_FILES['image']['tmp_name'];
$content =$_FILES['image']['fcontent']; // content in database
$sql="INSERT INTO $tablename(file_name,content)VALUES('" . mysql_real_escape_string($file_name) . "')"; // here i need to insert the content i assume
$file_ext=strtolower(end(explode('.',$_FILES['image']['name']))); //convert to lower
$extensions = array("jpeg","jpg","png","txt","html","php","gif"); // File extension that are
allowed
if(in_array($file_ext,$extensions )=== false){ // check if value exists in array
$errors[]="extension not allowed.";
}
if($file_size > 2097152){ // cant be greater than 2mb
$errors[]='File size must be excately 2 MB';
}
if(empty($errors)==true){
mysql_query($sql);
move_uploaded_file($file_tmp,"upload/".$file_name);
echo "Success";
header("location:files.php"); // Send back to main page
}else{
print_r($errors);
}
}
?>
Hi kaufenpreis, thanks for the reply. I am a bit confused now:( Like i said before, I have no idea how to do this, so I don’t understand this sudden change (all different field and file names) at all. And isn’t this mysql where I would like to try PDO
Keep in mind that where your comment suggests the insert should go (which seems correct to me) you’re already in a loop through each uploaded filename, so you don’t need to loop again. You’ve built a destination filename in the $target_path variable, although you appear to have created a string called $image_names using an md5 and the original file extension, but then use $image_name (without the ending ‘s’) when building $target_path on the next line, and add the extension again. Also, you shouldn’t use $target_path for this - each time you go through the loop, you’re adding more stuff to the $target_path variable. I’d probably try:
<?php
if (isset($_POST['submit'])) {
$j = 0;
$target_path = "../occasion_photos/";
for ($i = 0; $i < count($_FILES['file']['name']); $i++) {
$validextensions = array("jpeg", "jpg", "png");
$ext = explode('.', basename($_FILES['file']['name'][$i]));
$file_extension = end($ext); // if this is correct, you could use it in the next two lines as well, for clarity
$image_name = md5(uniqid()) . "." . $ext[count($ext) - 1];
$uploaded_image = $target_path . $image_name . "." . $ext[count($ext) - 1];
if (($_FILES["file"]["size"][$i] < 300000) && in_array($file_extension, $validextensions)) {
if (move_uploaded_file($_FILES['file']['tmp_name'][$i], $uploaded_image)) {
// The Insert should come here I guess??????
$sql = "insert into occasion_photos (occasion_id, photo) values (11, $uploaded_image)";
// then call your pdo or mysqli object to run the query
} else {
echo $j. ').<span id="error">please try again!.</span><br/><br/>';
}
} else {
echo $j. ').<span id="error">***Invalid file Size or Type***</span><br/><br/>';
}
}
}