PHP, MySQL, Drupal, .htaccess, Robots.txt, Phponwebsites: Upload files using PHP

29 Oct 2014

Upload files using PHP

                       You can see upload files at all social media sites to upload your photos. You can upload files using php. You can upload both text and binary files in php. You can upload any files such as .txt, .png, .jpg, .gif, .doc, .csv. At the same time you control the file extensions and file size while uploading files using php. For example, you allows the user can upload only .doc and docx file with particular file size.

PHP script for upload file:


                       Your form should be like this:

<form action="#" method="post" enctype="multipart/form-data">
File: <input type="file" name="file">
<input type="submit" name="submit" value="Upload">
</form>

                where,
                          enctype="multpart/form-data  -  must be present in form. Otherwise file does not upload.
                         type="file" in <input>  tag is used to take input as a file.

The php script for upload file is:

<html>
<body>
<form action="#" method="post" enctype="multipart/form-data">
File: <input type="file" name="file">
       <input type="submit" name="submit" value="Upload">
</form>
<?php
 if(isset($_POST['submit']))
 {
   $name=$_FILES['file']['name'];
   if(file_exists(dirname($_SERVER['DOCUMENT_ROOT']).'/oops/upload/files/'.$name))
   {
    echo'file is already present';
   }
   else
   {
    move_uploaded_file($_FILES['file']['tmp_name'],dirname($_SERVER['DOCUMENT_ROOT']).'/oops/upload/files/'.$name);
     echo'file added';
}
 }
?>
</body>
</html>

  where,

  

$_FILES:


                   It is the php global variable. It keeps information about uploaded file like below.
  $_FILES['file']['name'] - name of uploaded file.
  $_FILES['file']['type'] - type of uploaded file.
  $_FILES['file']['size'] - size of uploaded file.
  $_FILES['file']['tmp_name'] - temporary name of uploaded file which is in server.
  $_FILES['file']['error'] - errors while uploadeding file.
 ' file' is the name of <input> tag
  $_SERVER['DOCUMENT_ROOT'] - return the directory.
  move_uploaded_file()
 - uploaded file to specified directory.

                  Now you can upload any file without any restriction using php. The file is stored in '/upload/file/' directory already if it is not present in the directory. Otherwise you'll get error message ' file is laready present'.
                  

3 comments:

  1. Great point and a nice article. I believe that if you write about whatever you are passionate about you will receive a lot larger following. People can tell when you write about something of value vs something to simply appease… I just found your blog, and look forward to seeing future updates even if they aren't daily posts… Keep up the good work
    Thanks for sharing this great article.
    check ecommerce website
    for new tips and solution.

    ReplyDelete
  2. Sorry don't get " enctype="multpart/form-data"

    ReplyDelete