SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard
    Join Date
    Mar 2008
    Location
    United Kingdom
    Posts
    1,285
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Upload 5 images/files to web site?

    Hi,

    I've been asked by a designer friend to help with a PHP script that will upload 5 images to an 'images' directory on my server. No image generation/editing on-the-fly, just moving files.

    I have my HTML all ready (snippet) :
    HTML Code:
    		<label for="thumb">Thumbnail :</label> <br />
    				<input type="file" name="thumb" id="thumb" /><br /><br />
    			
    		<label for="headerimg">Header Image:</label> <br />
    				<input type="file" name="headerimg" id="headerimg" /><br /><br />
    			
    		<label for="img1">Image 1(optional) :</label> <br />
    				<input type="file" name="img1" id="img1" /><br /><br />
    			
    		<label for="img2">Image 2(optional) :</label> <br />
    				<input type="file" name="img2" id="img2" /><br /><br />
    			
    		<label for="img3">Image 3(optional) :</label> <br />
    				<input type="file" name="img3" id="img3" /><br /><br />
    Now I'd like to just do a loop and add these into my 'images' directory.

    Would something like this do?

    PHP Code:
    $target_path "images/";

    $target_path $target_path basename$_FILES['uploadedfile']['name']); 

    if(
    move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
        echo 
    "The file ".  basename$_FILES['uploadedfile']['name']). 
        
    " has been uploaded";
    } else{
        echo 
    "There was an error uploading the file, please try again!";

    Should I create an array, then use a foreach loop to loop through the fields?

    Also, I need to store the filename variable(for each) so I can insert all 5 elements into different columns in my SQL table.

    How easy would this be to do?



    Many thanks for any help you can provide.

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Take the code that you have, copy it, and paste it 4 more times. Now you have 5 calls to move_uploaded_file, to move 5 images. Change "uploadedfile" to the 5 form names you're using. Access the file names for your SQL query exactly as you already do to move the files.

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2008
    Location
    United Kingdom
    Posts
    1,285
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Genius!

    Was having a bit of a 'dumb moment' there. Thanks for your help, Dan.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •