Page 1 of 1

Trying to upload multiple files at once

Posted: Fri Sep 24, 2010 11:09 am
by gm2
Hi All. I am trying to implement a way to upload multiple files. I am using a file input in the form with multiple=true with the hopes of eventually being able to open a folder and select a bunch of images, and it upload all of them. Thus far, I have been unsuccessful.

Here is the form code that I am using, it seems to be working, as I can select multiple files, and hit upload.

Code: Select all


        <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $CONFIG['uploads']['max_file_size']; ?>"/>
        <label>File to upload</label>
        <input class="file" multiple="true" name="document[]" type="file"/>

Here is where the problem is, I believe. I don't think I am properly bringing in the array of files...

Code: Select all

    function add_file($client_id, $desc, $project, $phase, $uploaded_by)
    {
        global $CONFIG;

        /**
         * Ensure te file was uploaded,
         * fail otherwise
         */
        if (!is_uploaded_file($_FILES['document[0]']['tmp_name']))
        {
            trigger_error('Upload Error: ' . 'Error ' . $_FILES['document['.$i.']']['error'], E_USER_NOTICE);
            return false;
        }
        else
        {
            /**
             * Get all necessary information
             * about the file
             */
				$i = 0;
				 while ($_FILES['document['.$i.']']['name']) 
				 {
					$file['temp'] = $_FILES['document['.$i.']']['tmp_name'];
					$file['name'] = $this->rename_file($_FILES['document['.$i.']']['name']);
					$file['size'] = $_FILES['document['.$i.']']['size'];
					$file['type'] = preg_replace("/^(.+?);.*$/", "\\1", $_FILES['document['.$i.']']['type']);
					$file['type'] = strtolower($file['type']);
					$file['ext'] = $this->file_extension($_FILES['document['.$i.']']['name']);
					$file['size'] = round($file['size'] / 1024, 2);
					$file['name'] = preg_replace("/\s+/", "_", $file['name']);
					/**
					 * Move the file from the temp directory
					 * to the upload directory
					 */
					if (!copy($file['temp'], $CONFIG['uploads']['path'] . $file['name']))
					{
						if (!move_uploaded_file($file['temp'], $CONFIG['uploads']['path'] . $file['name']))
						{
							trigger_error('Upload Error: ' . 'Error moving from temp dir', E_USER_NOTICE);
							exit;
							return false;
						}		
					}
					/**
					 * Create a db record for the uploaded document
					 */
					if ($this->insert_asset_row($client_id, $project, $phase, $uploaded_by['id'], $desc, $file['ext'],
						$file['size'], $file['name']))
					{

						$file_id = mysql_insert_id();

						if ($this->get_type($file['ext']) == 'image')
						{
							$this->create_thumb($CONFIG['uploads']['path'] . $file['name'], $CONFIG['uploads']['path'] . 'thumbs/' . $file['name'], 120);
						}
					}
					
					$i++;
					if (!$_FILES['document['.$i.']'])
					{return $file_id;}
					
				}
				return false;
				
		}
    }

Thanks for any help!

Re: Trying to upload multiple files at once

Posted: Fri Sep 24, 2010 11:40 am
by John Cartwright
It's not possible to natively upload multiple files from a single selection box. You either need to create multiple upload elements, or use a flash uploader (with a PHP upload script). I would personally use a flash uploader, as it's far more flexible and intuitive to the user. If you google around you'll find plenty.

Re: Trying to upload multiple files at once

Posted: Fri Sep 24, 2010 1:40 pm
by gm2
Thanks. I guess that explains why it has not been working!

Re: Trying to upload multiple files at once

Posted: Fri Sep 24, 2010 3:18 pm
by shawngoldw
John Cartwright wrote:It's not possible to natively upload multiple files from a single selection box. You either need to create multiple upload elements, or use a flash uploader (with a PHP upload script). I would personally use a flash uploader, as it's far more flexible and intuitive to the user. If you google around you'll find plenty.
This is not entirely true John, multiple file uploads are part of html 5 and currently supported by the latest versions of Chrome, Firefox, and Safari. Sadly IE9 beta still doesn't support it so it will still be a while before it can broadly be used.

Shawn

Re: Trying to upload multiple files at once

Posted: Fri Sep 24, 2010 5:10 pm
by John Cartwright
shawngoldw wrote:
John Cartwright wrote:It's not possible to natively upload multiple files from a single selection box. You either need to create multiple upload elements, or use a flash uploader (with a PHP upload script). I would personally use a flash uploader, as it's far more flexible and intuitive to the user. If you google around you'll find plenty.
This is not entirely true John, multiple file uploads are part of html 5 and currently supported by the latest versions of Chrome, Firefox, and Safari. Sadly IE9 beta still doesn't support it so it will still be a while before it can broadly be used.

Shawn
Your right, but I wouldn't be relying on HTML5 standards and/or the major browsers compliance just yet, *cough*.

Re: Trying to upload multiple files at once

Posted: Sat Sep 25, 2010 12:23 am
by shawngoldw
John Cartwright wrote: Your right, but I wouldn't be relying on HTML5 standards and/or the major browsers compliance just yet, *cough*.
Definitely agreed.

Re: Trying to upload multiple files at once

Posted: Sat Sep 25, 2010 7:38 am
by requinix
Yay HTML 5! Too bad it won't be commonly adopted until IE 13 comes out with nearly complete support for it...

Anyways, assuming multiple uploads do work with your browser,

Code: Select all

$_FILES['document[0]']['tmp_name']
isn't the right way to get them. The name is still "document", but the tmp_name/type/etc are all arrays.

Code: Select all

$_FILES["document"]["tmp_name"][0]

Re: Trying to upload multiple files at once

Posted: Tue Sep 28, 2010 9:29 am
by gm2
oh... so there is potential for this to work?

This is part of a back-end update process, and I am one of 3 guys who would ever need to upload using this. And, we are all running firefox/chrome...

So far I am having little luck implementing any form of uploader that allows multiple files, and still allows me to do some SQL input.

Re: Trying to upload multiple files at once

Posted: Tue Sep 28, 2010 9:51 am
by gm2
tasairis wrote:Yay HTML 5! Too bad it won't be commonly adopted until IE 13 comes out with nearly complete support for it...

Anyways, assuming multiple uploads do work with your browser,

Code: Select all

$_FILES['document[0]']['tmp_name']
isn't the right way to get them. The name is still "document", but the tmp_name/type/etc are all arrays.

Code: Select all

$_FILES["document"]["tmp_name"][0]
Horray! That worked. Thanks a ton.

I am, however, limited to 9 files at a time. Anyone know how to change that?

Re: Trying to upload multiple files at once

Posted: Wed Sep 29, 2010 8:00 am
by gm2
Upon further testing, it is not limited to 9 files, but rather a limitation number of cumulative characters in the filenames. For example, I can select many files with short filenames, or just a few with long filenames. Not sure how to correct this.