Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
|
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 10th, 2005, 02:42 AM
cli cli is offline
Authorized User
 
Join Date: May 2005
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default UPLOAD - MULTIPLE - FILES

Hi to all i have a small problem (i think)....
I studyng the "Beginners PHP5" i'm reading about the file UPLOAD i need to UP LOAD multiple files now i working fine with one but what for more tree or four.....

I'm thinking about doing a FOR statement that control the $_FILES post but i'm not sure about the sintax ...

What yours...

Forget about my english please,,,, I'm ITALIAN:)
 
Old May 10th, 2005, 06:20 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 357
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to anshul
Default

Here's ( may be ) for your help:
    
Quote:
quote:if ($_POST['submit']=="UPLOAD") {
Quote:
        for($i=0; $i<5; $i++) {
            if($_FILES['userfile']['size'][$i]>"1048576")
                echo "<pre>Can't upload a file; filesize > 1 mb or 1024 kb or 1048576 bytes.</pre>";
            if ( ($_FILES['userfile']['size'][$i]<="1048576") && ($_FILES['userfile']['tmp_name'][$i]!="") ) {
                if (move_uploaded_file($_FILES['userfile']['tmp_name'][$i], $_SERVER['DOCUMENT_ROOT']."/images/".$_FILES['userfile']['name'][$i])) {
                    echo "<pre>Upload successful.\n";
                    echo "File: ".$_FILES['userfile']['name'][$i]." (".$_FILES['userfile']['size'][$i].") bytes\n";
                    echo "Renamed: ".$_FILES['userfile']['name'][$i]."</pre>\n";
                }
                else
                    echo "<pre>Can't upload file due an error.</pre>";
            }
            if( ($_FILES['userfile']['tmp_name'][$i]=="") && ($_FILES['userfile']['size'][$i]=="0") )
                echo "<pre>No file specified.</pre>";
        }
    }


 
Old May 10th, 2005, 11:31 AM
cli cli is offline
Authorized User
 
Join Date: May 2005
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

ok i'm studing your script only a thing thah i don't understand the form for upload i've resolve your scritp this way but doesn't work::(


Code:
<?
function upload_form() {
   //global $PHP_SELF;
?>
<form method="POST" enctype="multipart/form-data" 
   action="<? echo $PHP_SELF ?>">
   <input type="hidden" name="action" value="upload">
   Upload file!
   <input type="file" name="userfile">
   <input type="file" name="userfile">
   <input type="file" name="userfile">
   <input type="submit" name="submit" value="upload">
</form>
<?
}

function upload_file() {

        for($i=0; $i<3; $i++) {
            if($_FILES['userfile']['size'][$i]>"1048576")
                echo "<pre>Can't upload a file; filesize > 1 mb or 1024 kb or 1048576 bytes.</pre>";
            if ( ($_FILES['userfile']['size'][$i]<="1048576") && ($_FILES['userfile']['tmp_name'][$i]!="") ) {
                if (move_uploaded_file($_FILES['userfile']['tmp_name'][$i], $_SERVER['DOCUMENT_ROOT']."/documenti_master/".$_FILES['userfile']['name'][$i])) {
                    echo "<pre>Upload successful.\n";
                    echo "File: ".$_FILES['userfile']['name'][$i]." (".$_FILES['userfile']['size'][$i].") bytes\n";
                    echo "Renamed: ".$_FILES['userfile']['name'][$i]."</pre>\n";
                }
                else
                error_reporting(E_ALL);
                    echo "<pre>Can't upload file due an error.</pre>";
            }        
            if( ($_FILES['userfile']['tmp_name'][$i]=="") && ($_FILES['userfile']['size'][$i]=="0") )
                echo "<pre>No file specified.</pre>";
        }

}
?>
<html>
<head><title>file upload</title></head>
<body>
<?
if($action == 'upload') upload_file();

else upload_form();
?>
</body>
</html>
The script say: "Can't upload file due an error"

 
Old May 11th, 2005, 12:55 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 357
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to anshul
Default

As we've many ( say 5 ) files:
Quote:
quote:<input type="hidden" name="MAX_FILE_SIZE" value="1048576">
<input name="userfile[]" type="file" class="text"><br>
<input name="userfile[]" type="file" class="text"><br>
<input name="userfile[]" type="file" class="text"><br>
<input name="userfile[]" type="file" class="text"><br>
<input name="userfile[]" type="file" class="text"><br>
 
Old May 16th, 2005, 11:25 AM
cli cli is offline
Authorized User
 
Join Date: May 2005
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hey great, now work fine thanks anshul

 
Old May 29th, 2005, 04:59 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes, it's important you understand WHY Anshul's script works. In your version, each file you uploaded was being stored in a variable called "userfile". You kept overwriting the value of this variable with each successive file.

By adding the "[]" to the end of the input name, you append each file to the end of an array named "userfile", instead of overwriting a single value.

Make sense?





Similar Threads
Thread Thread Starter Forum Replies Last Post
Upload Multiple Files Anypond .NET Framework 2.0 1 February 21st, 2008 05:07 AM
How to upload data in multiple files at once? hwsc Pro VB Databases 2 June 27th, 2007 05:25 AM
upload multiple files in db with persits aspjpeg dann2 Classic ASP Basics 0 May 10th, 2007 11:20 PM
Importing Multiple files in Multiple tables Versi Suomi Access 6 June 1st, 2005 08:47 AM
How to upload multiple Files at once hwsc VB How-To 1 January 23rd, 2005 12:45 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.