Jump to content


Check out our Community Blogs

RikRoy

Member Since 09 Feb 2014
Offline Last Active Mar 16 2017 10:59 AM
-----

#668232 How to Upload Images with PHP

Posted by RikRoy on 12 February 2014 - 06:28 AM

This is my first post. So, I decided to post an easy tutorial for the beginners of PHP. I will give line to line explanation of everything. So don't give up if you don't understand the code (Though, it'd be very easy. I promise ) 
 
First of all, create a folder named upload in directory where you wish to store the pages. This will be the location where our images will be uploaded.
 
You'll need two pages for it:
  • form.php
    <html>
      <head>
        <title>
          CodeCall Tutorials
        </title>
      </head>
      <body bgcolor="#000000" text="#ffffff">
        <form method="post" action="upload.php" enctype="multipart/form-data">
          <label for="upload">
            Choose an image
          </label>
          <input type="file" name="upload">
          <br>
          <input type="submit" value="Upload your file">
        </form>
      </body>
    </html>
    
    Explanation:
     
    It is simple HTML and I don't think I need to explain but here is the explaination

    title tag defines what the title bar should say and its very clear.
    bgcolor attribute defines the background color of the page which I have set to black.
    text attribute defines the text color which is set white.
    form tag defines the start of a form and enctype defines how it should be encrypted.
    Its value that is multipart/form-data is used for parsing binary files.
    input tag creates a specified input feild I have set its type to file which creates an input feild along with a browse button next to it.

    And if anyone wants to know why I have used label tag instead of font, I would like to inform him/her that font tag has been removed in HTML 5.
     
  • upload.php
    <?php
    $allowed = array(
       "jpg",
       "jpeg",
       "bmp",
       "gif",
       "png"
    );
    $name = explode(".", $_FILES["upload"]["name"]);
    $ext = end($name);
    
    if ((($_FILES["upload"]["type"] == "image/jpg") || ($_FILES["upload"]["type"] == "image/jpeg") || ($_FILES["upload"]["type"] == "image/bmp") || ($_FILES["upload"]["type"] == "image/gif") || ($_FILES["upload"]["type"] == "image/png")) && ($_FILES["upload"]["size"] < 20000) && in_array($ext, $allowed)) {
       if ($_FILES["upload"]["error"] > 0) {
          echo $_FILES["upload"]["error"];
       }
       else {
          if (file_exists("upload/" . $_FILES["upload"]["name"])) {
             echo "File already exists";
          }
          else {
             move_uploaded_file($_FILES["upload"]["tmp_name"], "upload/" . $_FILES["upload"]["name"]);
             echo "Upload Successful";
          }
       }
    }
    else {
       echo "Invalid File";
    }
    
    ?>
Explanation:
 
First, we declare three variables.
First is an array in which I have listed all the extensions that are allowed.
Secondly, I created another array and stored the name of the uploaded file.
 
You might be wondering what is that explode() function. Well, it breaks a string into an array.
 
It needs two parameters. First one defines from where, the string should be broken (here "."). And the
second defines the string.
 
The $_FILES variable stores information about the uploaded file.
 
Thus, by the second line, I have broken the name of the uploaded file into two parts. First contains the
name and the second contains the extension of the file.
 
Then, I have created another variable to store the extension only. The end function selects only the last
value from an array.
 
Now, I think you have understood what I have done in these lines.
 
After that, I have checked if the file size is below 20 KB
 
The next few lines either store the uploaded file into a temporary file or produce an error.
 
Then, I have checked whether a file with the same name already exists. Then we move the uploaded file to the folder upload in the server and if any other error occurs, it writes Invalid file.
 
I hope you enjoyed the tutorial.
 
Note: Letting users to upload files to your web site is very risky because now days, everyone wants to hack the sites. So I suggest some security measures
  • 1