Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 10th, 2005, 02:42 AM
cli cli is offline
Authorized User
 
Join Date: May 2005
Location: , , .
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:)
Reply With Quote
  #2 (permalink)  
Old May 10th, 2005, 06:20 AM
Friend of Wrox
 
Join Date: Mar 2004
Location: Bhopal, MP, India.
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>";
        }
    }


Reply With Quote
  #3 (permalink)  
Old May 10th, 2005, 11:31 AM
cli cli is offline
Authorized User
 
Join Date: May 2005
Location: , , .
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"

Reply With Quote
  #4 (permalink)  
Old May 11th, 2005, 12:55 AM
Friend of Wrox
 
Join Date: Mar 2004
Location: Bhopal, MP, India.
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>
Reply With Quote
  #5 (permalink)  
Old May 16th, 2005, 11:25 AM
cli cli is offline
Authorized User
 
Join Date: May 2005
Location: , , .
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hey great, now work fine thanks anshul

Reply With Quote
  #6 (permalink)  
Old May 29th, 2005, 04:59 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
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?
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 06:10 AM.


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