|
|
|
|
|
|
| |
When you take a look at all the elements individually, this is a very simple process and certainly nothing too daunting. To upload a file to a Web server via an HTML form interface, you need the following:
- form
- file to upload (and a browser that supports it)
- place to put the file
- script to put it there |
|
Don't laugh at my simplicity! Nine times out of ten, the "cries for help" are because one of the elements listed above is missing, and the user just doesn't know it. As we tell clients all the time, "This isn't Star Trek - you can't just say 'Make it so' and expect it to work. You have to get out and push the ship a little." |
|
The process of uploading a file should go something like this:
1. User accesses form, sees text field and "Browse" button in their GUI Web browser.
2. User browses their own hard drive for the file they want to upload, and when the selection is made, the file path and name appears in the text field.
3. User presses the form's "submit" button.
4. File goes out into the ether, lands at the Web server, and sits around in the PHP temp directory, since nothing has told it what to do.
5. But wait! The form action points to a PHP script, and that script tells it what to do...
6. PHP script checks that a file was sent, then takes that file name (which is associated with the file sitting in the temp directory) and executes a copy command, thus moving the file from the temp directory to a real live directory on the server.
7. PHP script lets the user know that the file made it to its destination and all is well. |
|
A caveat: the PHP user (the user under which PHP runs, ie "nobody" or "www" or "joe") must have write permissions in BOTH the temporary directory and the directory in which it is copying the file. Otherwise, it won't go where it's supposed to go and you'll get nasty parse errors. |
|
|
Now it's time to start...with a simple, one-field form. Call it "show_upload.html" or something like that, and add in your basic HTML header/title stuff, then move on to the form itself:
Use the enctype="multipart/form-data" attribute so that the browser knows that more than just text variables are coming at it. |
|
<form enctype="multipart/form-data" method="post" action="do_upload.php">
|
|
|
The following hidden field sets the maximum file size, in bytes, that this uploaded file can be. |
|
<input type="hidden" name="MAX_FILE_SIZE" value="25000">
|
|
|
This chunk, note the type="file" attribute in the text field, will display the text field with the "browse" button, used to pick the file that's going to be uploaded. |
|
<p><strong>File to Upload:</strong><br>
<input type="file" name="img1" size="30"></p>
|
|
|
Add in your submit button, then close your form tags and other HTML tags.
The "show_upload.html" file could look something like this: |
|
<html>
<head>
<title>Upload a File</title>
</head>
<body>
<h1>Upload a File</h1>
<form enctype="multipart/form-data" method="post" action="do_upload.php">
<p><strong>File to Upload:</strong><br>
<input type="file" name="img1" size="30"></p>
<P><input type="submit" name="submit" value="Upload File"></p>
</form>
</body>
</html>
|
|
|
Take a moment to commit the following to memory, as they're the variables created when a successful file is uploaded. I'm using the name "img1" because that's the name of the input field in the upload form, above: |
|
* img1 - This now refers to the temporary file on the Web server.
* img1_name - This is the actual name of the file that was uploaded. For example, if the name of the file was "me.jpg", then the value of $img1_name is "me.jpg".
* img1_size - The size of the uploaded file, in bytes.
* img1_type - The mime type of the uploaded file, such as image/jpg. This is set by the browser, so may or may not come along, depending on the browser. |
|
The following script, called "do_upload.php", will copy the uploaded file to a specified path, and then echo certain results to the user:
First, the PHP snippet: |
|
<?
// if $img_name isn't empty, try to copy the file
if ($img1_name != "") {
// copy the file to a directory or
//die and print an error message
// NOTE! if you're on a Windows machine,
// use Windows pathnames, like so:
// copy("$img1", "C:\\some\\directory\\path\\$img1_name");
copy("$img1", "/your/directory/path/$img1_name")
or die("Couldn't copy the file!");
} else {
// if $img_name was empty, die and let us know why
die("No input file specified");
}
?>
|
|
|
Next, echo back the file information as a confirmation that it was successful: |
|
<P>You sent: <? echo "$img1_name"; ?>, a <? echo "$img1_size"; ?>
byte file with a mime type of <? echo "$img1_type"; ?>.</p>
|
|
|
The "do_upload.php" script could look something like this: |
|
|
<?
if ($img1_name != "") {
copy("$img1", "/your/directory/path/$img1_name")
or die("Couldn't copy the file!");
} else {
die("No input file specified");
}
?>
<html>
<head>
<title>Successful File Upload!</title>
<body>
<h1>Success!</h1>
<P>You sent: <? echo "$img1_name"; ?>, a <? echo "$img1_size"; ?>
byte file with a mime type of <? echo "$img1_type"; ?>.</p>
</body>
</html>
|
|
|
That's all there is to it! No need for fear and loathing. |
|
| |
| Uploading files to the server with PHP Categories : PHP, File System, HTML and PHP, HTTP | | | Jump Start to Easy URLs Categories : PHP, Beginner Guides, MySQL, File System, To PHP | | | A simple textfile based counter Categories : PHP, File System | | | PHP, MySQL and Authentication 101 Categories : PHP, Databases, MySQL, Authentication | | | Caching With PHP Cache_Lite Categories : PHP, Cache | | | Building A Persistent Shopping Cart With PHP and MySQL Categories : PHP, MySQL, Databases, Ecommerce | | | Case Study: Handling MySQL Growth With a PHP Class Categories : Databases, MySQL, PHP | | | Grabb'n and Pars'n Categories : Beginner Guides, PHP, To PHP | | | Abstracting Oracle Connectivity with PHP and OCI8 Categories : PHP, Databases, OCI8, Oracle | | | Building An Extensible Menu Class Categories : PHP, PHP Classes, Object Oriented, Navigation | | | Sending Form Data in EMail Categories : PHP, Email, HTML and PHP | | | Practical Date and Time examples with PHP and MySQL Categories : Databases, MySQL, PHP, Date/time | | | Flash 4 and PHP - Retrieving Text From a Database Categories : PHP, MySQL, Flash | | | PHP 101 Part 15 of 15 : No News Is Good News Categories : PHP, Beginner Guides, Content Management | | | Writing Self-Documenting PHP Code Categories : PHP, Documentation | |
| | | Ellen C wrote : 114 THANK YOU !!!!!!!!! | | | Bob Robson wrote : 174 Thanks for a good script | | | KianOn wrote : 180 Hi,
Thanks for your script.
Anyway, I am facing a problem where I am trying to
upload a huge binary file. It is failed by the following
error.
Warning: Unable to open `none` for reading: No such file
or directory in c:/file/upload.php on line 11
Couldn`t copy the file!
The script works fine for a file less than 1 MB.
Thanks! | | | Anonymous wrote : 191 When ever I try to upload a file more thana few k, the
script times out, and nothing happens. It uploads this
miniscule little file no problem, but it won`t even upload
a small html document. I checked the
upload_max_filesize, and it is set to 2M. Do you know
what this might be? | |
|
|
|