Skip to menu

Skip to content



Uploading files with PHP

Tuesday, July 15th, 2008 at 5:49pm, in File Management, written by Tom Foyster

Continuing on from our article on Listing files stored within a directory using PHP, this quick guide will walk you through how to upload a file to a server using PHP.

Firstly, we assemble the form to upload a file within our html page, starting with the form tags. In the action attribute, we call the page with the php to upload the file on, usually the same page. Next we set the enctype attribute to ‘multipart/form-data’. This tells the browser that we’re handling more than just plain text. Finally, use ‘post’ as the method to send the data to the server.

<form action="upload.php" enctype="multipart/form-data" method="post">

</form>

Now, we put in the input field so the user can select the file to upload.

<form action="upload.php" enctype="multipart/form-data" method="post">

   <input type="file" name="ufile" />

</form>

Then, we stick in a submit button to make the form work!

<form action="upload.php" enctype="multipart/form-data" method="post">

   <input type="file" name="ufile" />

   <input type="submit" name="upload" value="Upload" />

</form>

Now the HTML is done, we can move onto the PHP side of things! We start off, right at the top of the page, by creating our PHP tags and the if statement that checks to see if the submit button has been clicked.

<?php

if ($_POST['upload']) {

}

?>

Next we make sure a file has actually been submitted for upload. This is useful for error checking procedures.

<?php

if (isset($_POST['upload'])) {
  if (isset($_FILES['ufile']['name'])) {

  }
}

?>

When uploading files through PHP, we use the $_FILES array. This array stores all the information we need about the file we’re uploading. For example, the file name, temporary server path, type and size. So, now we use this to set a few variables. Using the name of the file we’re uploading, we set the whole target path for the file.

<?php

if (isset($_POST['upload'])) {
  if (isset($_FILES['ufile']['name'])) {

     $path = 'uploads/';
     $path = $path . basename( $_FILES['ufile']['name']);

  }
}

?>

Now we let PHP do its magic. Using the target path we’ve just set, and the temp file path, where the file is uploaded until we decide what to do with it, we call the move_uploaded_file function to move the file from the temp path, to the permanent one!

<?php

if (isset($_POST['upload'])) {
  if (isset($_POST['ufile'])) {

     $path = 'uploads/';
     $path = $path . basename( $_FILES['ufile']['name']);
     if (move_uploaded_file($_FILES['ufile']['tmp_name'], $path)) {
        echo ‘File uploaded successfully!’;
     } else {
        echo ‘There was an error uploading the file’;
     }

  }
}

?>

And there we go, its as easy as that. As with any basic technique in PHP, this can be expanded to incude error checking for things like file type and file size with ease.

Share and Enjoy:

  • bodytext
  • del.icio.us
  • Facebook
  • Google
  • Reddit
  • Technorati
  • Furl
  • StumbleUpon
  • NewsVine
  • Slashdot
  • Spurl
  • Ma.gnolia
  • Live
  • YahooMyWeb
  • E-mail this story to a friend!

Subscribe to Total PHP: RSS | Email

Related Posts:

- Creating a text or csv file of information from your database

- Browser based template editor

- Deleting files with PHP

- Listing files stored within a directory using PHP

There are no comments on this article yet

Please log in to post a comment about this article.


Subscribe to our RSS Feed

Subscribe: RSS | Email

Clear Content - Easy to use PHP 5 Content Management System
CSS Contest

Recommended resources

Recent articles