Jump to content

Uploading Images - Single and/or Multiple


HSKrustofsky

Recommended Posts

I am building a basic ecommerce site for someone where they may, or may not, need to upload multiple images for what they are doing. I have my code working where it will upload multiple files, but the issue lies when they only add 1 image, or no image at all. For example, at the moment I have 3 inputs and when uploaded the image filename is saved in my database. Like I said, everything is okay when all 3 images are uploaded, but if they were to only add an image to one, the database adds empty rows for the other two.

 

For the image, the user is to put the item number of the product, and upload the image(s). Here is my code:

 

<?php
if(isset($_POST['submit'])) {
$num = $_POST['item_num'];

if(isset($_FILES['images'])) {
foreach($_FILES['images']['tmp_name'] as $i => $tmp_name){
$file_name = $_FILES['images']['name'][$i];
move_uploaded_file($tmp_name, "./images/".$_FILES['images']['name'][$i]);

mysql_query("insert into item_images (item_num, item_image) value ($num, '$file_name')");
}
}
}
?>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<label for="item_num">Enter Item#: </label>
<input type="text" name="item_num" /><br />
<input type="file" name="images[]" /><br />
<input type="file" name="images[]" /><br />
<input type="file" name="images[]" /><br />
<input type="submit" name="submit" value="Submit" />
</form>

 

Any suggestions? Thank you in advanced for any help.

Link to comment
https://forums.phpfreaks.com/topic/273300-uploading-images-single-andor-multiple/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.