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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.