Jump to content

Recommended Posts

I have this script which starts with a form asking for title and thumbnail.  It then uploads the image to my upload folder and stores the name in the database (as well as storing the title with an ID).  How can I modify this script to allow for me to upload multiple files to the database at once?

 

Form:

    <form method="post" action="upload_process2.php" enctype="multipart/form-data">

            <p>
              Title:
            </p>
            <input type="text" name="title"/>
            <p>
              Thumbnail:
            </p>
            <input type="file" name="thumb"> 
            <br/>
            <br/>
            <input TYPE="submit" name="upload" title="Add data to the Database" value="Submit"/>
          </form>

 

Processing Code:

 


   <?php
   
echo "<pre>";
echo "FILES:";
print_r($_FILES);
echo "</pre>";


$target = "path/for/upload/";
$target = $target . basename( $_FILES['thumb']['name']);


$title=$_POST['title'];
$thumb=($_FILES['thumb']['name']);




mysql_connect("localhost", "user", "pw") or die(mysql_error()) ;
mysql_select_db("database") or die(mysql_error()) ;


mysql_query("INSERT INTO test2 (title,thumb)
VALUES ('$title', '$thumb')") ;


if(move_uploaded_file($_FILES['thumb']['tmp_name'], $target))
{


echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {


echo "Sorry, there was a problem uploading your file.";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/187457-help-me-modify-my-script/
Share on other sites

Do you want to upload multiple thumbnails associated with the same title, or is one title associated with one thumbnail and you want to be able to submit many from one page?

 

If the first, change the name of your file field from "thumb" to "thumb[]".  This will allow you to them loop through the multiple fields like so:

 

<?php

foreach($_FILES['thumb']['name'] as $key)
{
    move_uploaded_file($_FILES['thumb']['tmp_name'][$key], $target);
}

?>

 

 

Do you want to upload multiple thumbnails associated with the same title, or is one title associated with one thumbnail and you want to be able to submit many from one page?

 

Well I am going to need to use 1 title, 1 thumbnail, 1 full sized jpg of the thumbnail (so i suppose it could be resized with html), 1 tiff,  and 1 png.  (the site is for franchisees to get marketing materials so I need for our graphic designer to be able to upload several different types of the same file.  After I get all of this figured out, I'm going to have to figure out a way to pull the files for display and download.

 

 

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.