ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

How to Upload files using PHP Tutorial

In this tutorial, I’m going to show you how upload files from a client web browser to a web server. To do this we need to configure first our PHP for file uploads. Open php.ini then make sure to set the following:

  • file_uploads – must be set to (“on” or set to “true” or “1”) so that the php will accept file upload
  • upload_tm_dir – is where we’re going to upload file temporarily, and it uses system’s tmp dir.
  • post_max_size – maximum size to anypost request is 8MB.
  • upload_max_filesize – the upload maximum filesize is set by default to 2MB.
  • max_execution_time – how long is the execution time basically it is set to 30 seconds
  • max_input_time- it is set to -1 meaning it has no limit
  • memory_limit-how much is each script allowed to use and the default is set to 128MB.

At this time, we’re going to create our web directory. And I will name it as “upload” and inside upload folder I’m going to create a new directory called “uploads”, where all the uploaded files is going to save.

Next we will create a new PHP file named “upload.php”. And save it inside the upload folder. Then we will add the following code to upload.php. And here’s the code:

  1. <html>
  2.     <head>
  3.         <title>upload </title>
  4.     </head>
  5.     <body>
  6.         <form action="upload.php" enctype="multipart/form-data" method="POST">
  7.         <input type="hidden" name="MAX_FILE_SIZE" value="1000000">
  8.         <input type="file" name="upload_file">
  9.         <input type="submit" name="submit" value="Upload">
  10.    
  11.     </form>
  12.     </body>
  13. </html>

 

The code above is very basic HTML code,  that it will post to itself upload.php, and will some variables in the inputs and the submit button. And above the html we’re going to add some PHP code to do some processing.

 

Only the difference in using this HTML code is that there is enctype=”multipart/form-data”, it is the enclosure type and that’s let us know that we’re not submitting text only but there may be files also.
 

And the next thing is, in the input type since we’re inputting we will use “file” as type. And the name is “upload_file”.
 

And one of the important part here in the html code is <code><input type=”hidden” name=”MAX_FILE_SIZE” value=”1000000″></code> and the maximum file size in bytes, and must be declared before the file input field and can’t be larger than the setting for “upload_max_filesize” in php.ini.
 

This time we’re going to add php code above the HTML form. And here’s the code:
 

This code will give us some messages when file is uploaded succesfuly or not.

  1. <?php if(!empty($message)) { echo "<p>{$message}</p>";}?>

Now we have already web forms ready for submitting, we need to anderstand first the concepts on how to inspect the uploaded files.  To do this, we need look at the new superglobals called $_FILES. This $_FILES is going to contain any file submitted during the POST request.
 

To access this $_FILES is to say what is the name of those input field. In our case its “upload_file”. So the way use this $_FILES[‘upload_file’]. And this $_FILE[‘upload_file’] will return an associative array and will give us five pieces of data and these are the following:
 

Example : $_FILES[‘upload_file’][‘name’]

  • name – it will contain the original file name
  • type – the type (“image/gif”)
  • size – the size in bytes
  • tmp_name – temp file name on the server
  • error – error code

At this time, we’re going to look at the posible error when we upload files. And below are file upload error:
 

 UPLOAD_ERR_OK                              0   it means no error

UPLOAD_ERR_INI_SIZE                  1    it means that upload is larger than the specified upload_max_filesize

UPLOAD_ERR_FORM_SIZE           2   it says the Form size was to large or Larger than MAX_FILE_SIZE

UPLOAD_ERR_PARTIAL                  3   it means that it is a partial upload or the file didn’t finished

UPLOAD_ERR_NO_FILE                 4   No file has sent all

UPLOAD_ERR_NO_TMP_DIR      6   No temporary directory

UPLOAD_ERR_CANT_WRITE       7   it means that we can’t write to the disk or it is read only

UPLOAD_ERR_EXTENSION           8  file upload stopped by extension

Now let’s proceed to apply this error code and display it into message. To do this, add the following this PHP code above the HTML code:
 

This code below is that first we create an upload_errors as an array so that every time there’s an error it will look into the array and find key and give the corresponding message. And on the form is submit to itself then move the file out of the temporary directory into our “uploads” folder. But before to move the file we need to specify first the temporary directory, then we need also to specify the name of the directory that we’re going to put our file.

  1. <?php
  2. $upload_errors = array(
  3. UPLOAD_ERR_OK           => "No errors.",
  4. UPLOAD_ERR_INI_SIZE         => "Larger than upload_max_filesize.",
  5. UPLOAD_ERR_FORM_SIZE    => "Larger than form MAX_FILE_SIZE.",
  6. UPLOAD_ERR_PARTIAL          => "Partial upload.",
  7. UPLOAD_ERR_NO_FILE          => "No file.",
  8. UPLOAD_ERR_NO_TMP_DIR => "No temporary directory.",
  9. UPLOAD_ERR_CANT_WRITE  => "Can't write to disk.",
  10. UPLOAD_ERR_EXTENSION      => "File upload stopped by extension.",
  11. );
  12. if (isset($_POST['submit'])){
  13.     $tmp_file = $_FILES['upload_file']['tmp_name'];
  14.     @$target_file = basename($_FILES['upload_file']['name']);
  15.     $upload_dir = "uploads";
  16.    
  17.     if (move_uploaded_file($tmp_file,$upload_dir."/".$target_file)){
  18.         echo "File uploaded Succesfully";
  19.     }else{
  20.         $error = $_FILES['upload_file']['error'];
  21. $message = $upload_errors[$error];
  22.     }
  23.        
  24. }
  25.  
  26. ?>

 

And here’s all the code in the tutorial:4

 

  1. <?php
  2. $upload_errors = array(
  3. UPLOAD_ERR_OK         => "No errors.",
  4. UPLOAD_ERR_INI_SIZE   => "Larger than upload_max_filesize.",
  5. UPLOAD_ERR_FORM_SIZE  => "Larger than form MAX_FILE_SIZE.",
  6. UPLOAD_ERR_PARTIAL    => "Partial upload.",
  7. UPLOAD_ERR_NO_FILE    => "No file.",
  8. UPLOAD_ERR_NO_TMP_DIR => "No temporary directory.",
  9. UPLOAD_ERR_CANT_WRITE => "Can't write to disk.",
  10. UPLOAD_ERR_EXTENSION  => "File upload stopped by extension.",
  11. );
  12.  
  13. if (isset($_POST['submit'])){
  14.     $tmp_file = $_FILES['upload_file']['tmp_name'];
  15.     @$target_file = basename($_FILES['upload_file']['name']);
  16.     $upload_dir = "uploads";
  17.    
  18.     if (move_uploaded_file($tmp_file,$upload_dir."/".$target_file)){
  19.         echo "File uploaded Succesfully";
  20.     }else{
  21.         $error = $_FILES['upload_file']['error'];
  22. $message = $upload_errors[$error];
  23.     }
  24.        
  25. }
  26.  
  27. ?>
  28. <html>
  29.     <head>
  30.         <title>upload </title>
  31.     </head>
  32.     <body>
  33.     <?php if(!empty($message)) { echo "<p>{$message}</p>";}?>
  34.     <form action="upload.php" enctype="multipart/form-data" method="POST">
  35.         <input type="hidden" name="MAX_FILE_SIZE" value="1000000">
  36.         <input type="file" name="upload_file">
  37.         <input type="submit" name="submit" value="Upload">
  38.    
  39.     </form>
  40.     </body>
  41. </html>
Facebook Comments

Check Your Domain Ranking

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!