Creating a file manager with PHP
(Page 1 out of 3)Introduction
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:
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:
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.
January 23rd, 2006 at 1:31 pm
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
January 23rd, 2006 at 2:47 pm
Now think about a file manager web 2.0. Using set of class like prototype.js … now that would be something wouldn’t it?.
January 23rd, 2006 at 4:20 pm
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.
January 23rd, 2006 at 9:55 pm
[…] 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) […]
January 23rd, 2006 at 11:26 pm
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.
January 25th, 2006 at 9:19 pm
[…] var site=”s20phpit” « Previous Creating a file manager with PHP […]
January 26th, 2006 at 7:37 pm
[…] 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. […]
June 22nd, 2006 at 6:05 pm
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.
August 5th, 2006 at 11:24 pm
This is a great tutorial. You can add some AJAX to show the Progress bar to the user. Keep it up..
August 16th, 2006 at 4:22 am
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.