You are here: Home » PHP » How to Upload file in PHP

How to Upload file in PHP

47 Flares 47 Flares ×

As a developer whether newbie or having good experience, you always need to give upload function in most of the web forms. not necessarily in every form but in most cases you need to integrate feature to upload files whether they are images, pdfs or docs, as per the client’s requirement.

Today I would like to share small chunk of code which helps you add the functionality to upload files. Here it goes:

<?
if(isset($_POST['submit']))
{
$pdf=$_FILES[file][name];
 move_uploaded_file($_FILES[file][tmp_name],"uploads/$pdf")  or die("Not able to write ");
}
?>

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

So all done! But wait, there are is one thing you need to keep in mind and that is: always use enctype=”multipart/form-data” in the form to upload any type of files such as images, pdfs, docx etc.

Few more things you need to do which are as:

  • Create a folder on the server, where files will save(in the above code files are saving in uploads folder.
  • Rename file before uploading, use a prefix, it can be current time or something else.
  • Remove spaces to avoid %20 kind of thing in URL and # from file name by using replace() function.

Thats all for today! Thanks for reading.

About Abhishek Sinha

I am Abhishek and I love to code on PHP.

Leave a Reply

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

*

Scroll To Top
47 Flares Twitter 6 Facebook 10 Google+ 6 StumbleUpon 20 Pin It Share 0 LinkedIn 5 47 Flares ×