ASPit - Totally ASP JSit - Totally JavaScript
Search PHPit

Use this textbox to search for articles on PHPit. Seperate keywords with a space.


Creating a file manager with PHP

(Page 1 out of 3)


Moving files across multiple computers can often be a problem, and synchronization is one of the biggest problems. What if you could simply store your files in one place, and access it on each computer? With PHP that's no problem, and in this tutorial you'll learn how to create your own simple file manager.

Our file manager will have four main features: uploading, editing, deleting and streaming (downloading) files. Everything can be done without having to touch an FTP program, which makes it a lot easier to quickly change something.

Let's start with the most important feature: uploading files.

Before you start, read this: The file manager we create in this tutorial is prone to security problems, especially if you don't know what you're doing. If you're unsure, wait a few days more, because in the next article we'll have a look at creating a file manager that's completely 100% secure!

Uploading files

Uploading files with PHP is a very easy task, and it requires very little knowledge. The first thing you have to do when uploading files is create a simple HTML form, like so:

action="" method="POST" enctype="multipart/form-data">
        type="file" name="file" size="30">
        type="submit" value="Upload File" />

The most important thing here is the "file" input element. This form element gives the user the ability to select a file, and upload it (by clicking the submit button). Another difference with a regular HTML form is the enctype attribute in the form element. The standard enctype, which is used by normal forms, is not suitable for file uploads, and that's why we're using a different enctype.

The PHP code that handles this form is very simple as well, and it only does a few basic tasks. Something like this does exactly what we want:

// Is the file there
if (isset($_FILES['file']) == false OR $_FILES['file']['error'] == UPLOAD_ERR_NO_FILE) {
        die('No files');

// No problems?
if ($_FILES['file']['error'] != UPLOAD_ERR_OK) {
        die('Error occured during upload');

// Move file to our upload folder
$path = 'G:\projects\PHPit\content\creating file system php\demos\upload\\';

$newfile = $path . $_FILES['file']['name'];
move_uploaded_file($_FILES['file']['tmp_name'], $newfile);

die('Your file has been successfully uploaded.');

As you can see this code uses a variable called $_FILES, which is a superglobal specifically used for file uploading. Have a look at the PHP Manual for more on this variable, but it's basically a simple array that holds details on all the files that have been uploaded (in our case only one).

The first thing that happens in the above code snippet is a simple check to see if a file has been uploaded. After that the code makes sure that everything is okay, by using the error constants that are included standard in PHP (see the PHP manual for information on each error).

The uploaded file is then moved from its temporary directory to its final destination, which is a very important step. When a user uploads a file, the file is stored in a temporary directory (e.g. C:\Windows\Temp\), and your script must move the file to a final directory, or else the file will be removed again sometime by your OS. (The temp directory usually gets emptied every so often).

Please note: the destination directory must be writeable by your PHP script. Make sure to CHMOD it with the appropriate permissions.

That's all there is to it, which shows that file uploading in PHP is really easy. You can view a live demo of the above example by clicking here.

Next: Streaming & Editing »

10 Responses to “Creating a file manager with PHP”

  1. Evert Says:

    This is a really bad tutorial in a few ways:

    * All files are uploaded in a directory accessible by the webserver, I can enter /upload on the live example and run my own php scripts.
    * Magic quotes is probably enabled. So in the files ‘ will be converted to \’ . This is not really usefull for most files

  2. Bocse L Filip Says:

    Now think about a file manager web 2.0. Using set of class like prototype.js … now that would be something wouldn’t it?.

  3. Dennis Pallett Says:

    Evert: you are absolutely right, and I’ve fixed the problem, though this problem only exists in the demo. Normally, you would place the upload directory above the web root anyway.

    As for the magic quotes problem; it’s a very easy fix.

    Bocse: That would be pretty neat, and something to consider writing. Imagine what you could create with prototype.js, ajax, and more.

  4. Piku’s PHP Blog » PHPit articles Says:

    […] The second article is Creating a file manager with PHP. It treats the basics of uploading files and working with files in PHP (opening, editing, deleting files) […]

  5. Matthijs Says:

    Interesting article Dennis. I don’t agree with Evert, it’s not a bad tutorial. But it’s good you added the lines about security. The difficulty with tutorials like these is that you want to focus on one problem. Here, uploading/managing files. If you would add an explanation of every weak point of every script, each tutorial would be at least 20 pages. But nonetheless, a few warnings, even just a few lines is never bad. Looking forward to your next articles.

  6. PHPit - Totally PHP » Creating a SECURE file manager with PHP Says:

    […] var site=”s20phpit” « Previous Creating a file manager with PHP […]

  7. Piku’s PHP Blog » Creating a SECURE file manager with PHP Says:

    […] In a previous article I have posted a link to a PHPIt article: Creating a file manager with PHP. Now, PHPIt created the second tutorial in this series: Creating a SECURE file manager with PHP. It has all the information you need to secure your file manager made using the previous article. […]

  8. Tyler Says:

    This is a great tutorial, however why does it not include the ability to access different folders in a directory? Do I need to change something in the code? Maybe I am just overlooking something. If someone could help me I would greatly apreciate it.

  9. Moses Says:

    This is a great tutorial. You can add some AJAX to show the Progress bar to the user. Keep it up..

  10. Levi Says:

    I have problem in viewing encryted image file to view in IE6 SP1. just the images doesnt render properly while the other type (like pdf) doesnt have any error.

    is it IE setting? IE really sucks at all time.

Leave a Reply

About the author
Dennis Pallett is the main contributor to PHPit. He owns several websites, including ASPit and Chill2Music. He is currently still studying.
Article Index
  1. Uploading files
  2. Streaming & Editing
  3. Deleting & Wrap-up
Bookmark Article
Download Article
Download this article as a PDF file