0

I am currently using the following code to upload images to an ftp server:

<input name="userfile" type="file" size="50">

...

$filep=$_FILES['userfile']['tmp_name'];
$name=$_FILES['userfile']['name']; 

$upload = ftp_put($conn_id, $paths.'/'.$name, $filep, FTP_BINARY);

This works fine except that it only uploads one image.

Any help with multiple (3-4) image uploads pls.

Thanks in advance

3
  • Take all images name in array and make all code in loop Commented Oct 31, 2012 at 12:59
  • I was hoping for some code hint Commented Oct 31, 2012 at 13:03
  • Did any of these answers help you? Commented Oct 31, 2012 at 13:26

3 Answers 3

4
<input name="userfile[]" type="file" size="50" />
<input name="userfile[]" type="file" size="50" />
<input name="userfile[]" type="file" size="50" />

for($i = 0; $i < count($_FILES['userfile]); $i++) {
   $filep=$_FILES['userfile']['tmp_name'][$i];
   $name=$_FILES['userfile']['name'][$i]; 

   $upload = ftp_put($conn_id, $paths.'/'.$name, $filep, FTP_BINARY);
}
Sign up to request clarification or add additional context in comments.

Comments

1

Here's one I had used. All the image fields have the same name property value of files

<input type="file" name="files[]" class="files" multiple /> can be used for html5, or add multiple of this line for non-html5. I used jquery for my uploader, but you can just have multiple upload fields visible instead.

Obviously you can change my code to use your ftp code, but here's another way too.

        $files = array();

        $allowed_filetypes = array('.jpg','.jpeg','.gif','.bmp','.png','.tif');
        $max_filesize = 1524288;
        $upload_path = 'images/image_uploads/';

        for ($i = 0; $i < count($_FILES['files']['name']); $i++){
            if($_FILES['files']['name'][$i] != "") {
                $filename = $_FILES['files']['name'][$i];
                $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); 
                $ext = strtolower($ext);

                if(!in_array($ext,$allowed_filetypes))
                    die("The file you attempted to upload ($filename) is not allowed.");

                if(filesize($_FILES['files']['tmp_name'][$i]) > $max_filesize)
                    die("The file you attempted to upload ($filename) is too large.");

                if(!is_writable($upload_path))
                    die("You cannot upload to the specified directory, please CHMOD it to 777.");

                $ran = rand();
                $filename = $ran.$ext;
                if(move_uploaded_file($_FILES['files']['tmp_name'][$i],$upload_path.$filename)) {
                    $result = mysql_query("Insert Into image_uploads_images (upload_id, image, original_name) Values ('$id', '$filename', '".$_FILES['files']['name'][$i]."');");

                    if($result){
                        array_push($files, "http://www.site.com/images/image_uploads/$filename => ".$_FILES['files']['name'][$i]);
                    }else{
                        echo "<p style=\"color:#cc3333;\">Unable to upload ".$_FILES['files']['name'][$i]."</p>";
                    }
                }else{
                    echo "<p style=\"color:#cc3333;\">Unable to upload ".$_FILES['files']['name'][$i]."</p>";
                }
            }
        }

Comments

1

you should upload files async one by one :)

HTML :

<input name="userfile[]" type="file" size="50" />
<input name="userfile[]" type="file" size="50" />
<input name="userfile[]" type="file" size="50" />

PHP :

for( $i = 0 ; $i < count( $_FILES['userfile'] ) ; $i++ )
{
   $filep = $_FILES['userfile']['tmp_name'][$i];
   $name = $_FILES['userfile']['name'][$i];

   $upload = ftp_put( $conn_id , $paths . '/' . $name , $filep , FTP_BINARY );
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.