p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   PHP How-To (http://p2p.wrox.com/forumdisplay.php?f=98)
-   -   UPLOAD - MULTIPLE - FILES (http://p2p.wrox.com/showthread.php?t=29118)

cli May 10th, 2005 02:42 AM

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:)

anshul May 10th, 2005 06:20 AM

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>";
        }
    }



cli May 10th, 2005 11:31 AM

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"


anshul May 11th, 2005 12:55 AM

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>

cli May 16th, 2005 11:25 AM

hey great, now work fine thanks anshul


nikolai May 29th, 2005 04:59 PM

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?


All times are GMT -4. The time now is 03:00 PM.

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