File Uploads with PHP Part 2
Creating the PHP page for File Uploads
Now here comes the hard part. Create and save a php page with the name you put in the action of the form. For instance, I typed my action as "processfile.php" so that is what I would use for the name of my php page.
Next, go into the source code view of GoLive 6. Between the opening and closing body tags <body></body> copy paste in the following code, modifying the fields in blue to reflect your own values. Where I have typed sendCat, you would enter in the name you gave your file browser input field in your form. You may also want to change the messages for "if" and "else." What happens is that if someone sends an image that is too big, an error message is displayed. If the file upload goes smoothly, a message reflecting that success is displayed. You can also add whichever html tags you like to these messages.
<?php
if ($HTTP_POST_FILES['sendCat']['size']
<=0)
{
print "<h2><b>Your picture was not received.</b></h2><br>";
print "The file size was larger than 5k.<br>";
print "Reduce the size and resubmit.";
}
else
{
copy($HTTP_POST_FILES['sendCat']['tmp_name'],
$HTTP_POST_FILES['sendCat']['name']);
print "<h2>Thank you for sending your picture.</h2><br>";
print "<h2>Your picture has been saved on the system.</h2>";
}
?>
Things to remember:
- You need to enter the name that you gave the file browser object in your form.
- When the file is first uploaded to the Web server, it is assigned a temporary name by PHP. The temporary file is deleted once processing is complete. The copy function you see in the code above saves a permanent copy to the server.
- When a file is uploaded to the server, by default it is saved to the same directory as the php page.
- Make sure your Web server has file uploads enabled in the php.ini
file. If you are the web administrator, you want to see the following
in your settings:
file_uploads = On;
upload_tmp_dir = [location of directory];
upload_max_filesize = [list amount such as 2MB]; - Once you upload your html and php pages to the server, you must set the appropriate permissions to the pages and the directory to which files will be written (uploaded.)
- I like to password protect upload file pages so that only members or authorized people can upload to my server. Login pages will be a future tutorial!