article

Uploading files in PHP

Email
Submitted on: 1/1/2015 12:20:00 PM
By: S.S. Ahmed (from psc cd)  
Level: Beginner
User Rating: By 3 Users
Compatibility: PHP 4.0
Views: 1799
 
     The article shows how to upload files in PHP.

This article has accompanying files
 
				Uploading files in PHP
Think of an application where you want your users to upload files to your machine. Uploading files in PHP is very easy. You can upload any type of file from client to server. Files are uploaded from the browser using an  tag, set the type parameter in the  tag as "File". This is supported by all popular browsers currently available on the market. Important thing is to set the ENCTYPE attribute of the form to "multipart/form-data" and set the form's action element to the file upload page. The file upload page will handle the file uploading. Here is the code:
Create a simple html form for submitting the file:

submit this file:

Now, create a php file named as "myupload.php": Details: Handling uploaded file is very easy in PHP. First, the file is saved in the temporary directory of your operating system (if you have not set the path to your own temporary directory in the php.ini file). You can copy the uploaded file from the temporary directory to the location of your choice. The file in the temporary directory is automatically destroyed at the end of the request. File name is accessible in the same way as all form data. In our code, we have set the name for the uploaded file as "userfile". You can access this variable in the upload file page as $userfile. We copy the file to a permanent location using the copy() function. Although, the uploaded file is automatically destroyed at the end of the request but its a good practice to destroy the file explicitly. You can use the unlink() function to destroy the file. You can check if the file has been uploaded to the server using different attributes provided by the PHP. For example, if you dont want to allow your users to upload the same file again and again, you can put a restriction on the user to upload new files only. We can set limit on the size of the file to be uploaded by adding an element with the NAME attribute set to MAX_FILE_SIZE and the VALUE to the upper limit. I havent demonstrated this in my sample code but here the piece of code for your reference: Remember, the hidden tag should precede the file tag. Place a check before displaying a message, use the empty() function to check if $userfile variable contains something or not, if its empty then display the message telling the user that the file hasn't been uploaded otherwise display the message that the file has been uploaded successfully.

winzip iconDownload article

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

If you don't have a virus scanner, you can get one at many places on the net including:McAfee.com


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.