PHP - Upload multiple images

Discussion in 'Web Design and Development' started by JakeGWood, Jul 25, 2011.

  1. JakeGWood macrumors newbie

    Jul 3, 2011
    So I started with a code I found on (example 15) which uploads an image, writes it to a variable, and displays it. I want to modify it so that it instead writes it to an array and then displays, and then I can continue to upload images to subsequent array position and display them all, as opposed to just the most recent, as is the case now. You can see I set it up to put in an array and then after displaying, move to the next position, but given that the HTML that creates the button is not dynamic, I sort of get stuck. Here's what I have now:

    <?php // upload.php
    echo <<<_END
    <html><head><title>PHP Form Upload</title></head><body>
    <form method='post' action='upload.php' enctype='multipart/form-data'>
    Select File: <input type='file' name='filename' size='10' />
    <input type='submit' value='Upload' />
    function display(){
    if ($_FILES)
    	$name[$m] = $_FILES['filename']['name'];
    	move_uploaded_file($_FILES['filename']['tmp_name'], $name[$m]);
    	echo "Uploaded image '$name[$m]'<br /><img src='$name[$m]' />";
    echo "</body></html>";
  2. elppa macrumors 68040


    Nov 26, 2003
    Two things:

    [1] If you write to an array its contents won't persist between uploads. If you need to store the image locations, you will need to store them to a flat file or database.

    [2] If you want, you can include multiple <inputs> on your page, like so:
    <input type='file[]' name='filename' size='10' />
    <input type='file[]' name='filename' size='10' />
    <input type='file[]' name='filename' size='10' />
    <input type='file[]' name='filename' size='10' />
    Then to access:
    $name = array();
    $name[0] = $_FILES['filename']['name'][0]
    $name[1] = $_FILES['filename']['name'][1]
    $name[2] = $_FILES['filename']['name'][2]
    $name[3] = $_FILES['filename']['name'][3]

    [3] To dynamically add/remove inputs, it may be quickest to use some client side javascript.
  3. JakeGWood thread starter macrumors newbie

    Jul 3, 2011
    Truuuueee. Good point. It's still hard to get used to the fact that php is gone when it gets client side, so it's pretty much only dynamic server side. I guess to do what I want I would need to start MySQL and store pictures on db?

    With multiple input buttons, would one upload button be used to upload all the chosen files at once?

    I like the jscript idea. I guess just throw the input HTML tags in a for loop set for a user inputed iterations.
  4. elppa macrumors 68040


    Nov 26, 2003
    So that makes it like every other server side language in the world. :)

    Its pretty vital to have that separation. Think about it - if people were executing your code on their machines - they could be doing all manner of untrusted things. It's for the best, but you do have to remember the web is fundamentally stateless.

    You can write the pictures to the database as binary, but it might be best to just store a reference to the locations of the files on the filesystem.

    Yes, you have to be a bit careful, because if there are a number of large files then the time to process may exceed the maximum time allowed for a single request. But cross that bridge when you come to it...

Share This Page