EasyUp - Simple File Uploads
January 9, 2010
If you're working with PHP (or any server side language), you've probably constructed a script to upload files at some point or another, especially if you are constantly developing tools to assist people with no knowledge of code or basic FTP to manage files. You also might be searching for a way to accomplish this for the first time. In either case, let's see if we can't make your job easier.
There are no lack of PHP uploader classes out there, so let's not pretend this is groundbreaking. The problem is a lot of those classes are extremely bloated and difficult to either learn the basics from or adapt to your program's specific needs.
File handling is also one of the best places to implement some simple OOP to make this oft-used functionality reusable. If your eyes darted nervously and your cursor went for that back button as soon as you read OOP, fear not, you can easily find all of the tools to build a procedural file uploader here.
Cut to the Chase
Our class, affectionately called easyUp until someone inevitably informs me the name is already in use, is a pared down, easy to read, and easy to implement class:
class fileDir {
private $fileInfo;
private $fileLocation;
private $error;
private $direct;
function __construct($dir){
$this->direct = $_SERVER['DOCUMENT_ROOT'].$dir;
if(!is_dir($this->direct)){
die('Supplied directory is not valid: '.$this->direct);
}
}
function upload($theFile){
$this->fileInfo = $theFile;
$this->fileLocation = $this->direct . $this->fileInfo['name'];
if(!file_exists($this->fileLocation)){
if(move_uploaded_file($this->fileInfo['tmp_name'], $this->fileLocation)){
return 'File was successfully uploaded';
} else {
return 'File could not be uploaded';
$this->error = "Error: File could not be uploaded.\n";
$this->error .= 'Here is some more debugging info:';
$this->error .= print_r($_FILES);
}
} else {
return 'File by this name already exists';
}
}
function overwrite($theFile){
$this->fileInfo = $theFile;
$this->fileLocation = $this->direct . $this->fileInfo['name'];
if(file_exists($this->fileLocation)){
$this->delete($this->fileInfo['name']);
}
return $this->upload($this->fileInfo);
}
function location(){
return $this->fileLocation;
}
function fileName(){
return $this->fileInfo['name'];
}
function delete($fileName){
$this->fileLocation = $this->direct.$fileName;
if(is_file($this->fileLocation)){
unlink($this->fileLocation);
return 'Your file was successfully deleted';
} else {
return 'No such file exists: '.$this->fileLocation;
}
}
function reportError(){
return $this->error;
}
}
For some the class may be self-explanatory, but no worries if not. Before we can create file manipulation magic, we need some HTML.
The HTML
Nothing unusual here. A form that submits to the same page with a single browse for file input and a submit button. In addition to the HTML file, we're going to need a folder on the server to house our files. Here, we'll be using a directory called myUploads.
The Implementation
if(isset($_POST['mySubmit'])){
include 'easyUp.php';
$up = new fileDir('/myUploads/');
$up->upload($_FILES['myFile']);
}
First we set up an if statement to see if our form was submitted, and then set include our neatly packaged class inside the if statement. After that we create a new object and set it to a variable name. This can be whatever you'd like, but we're going to go with $up this time. When the object is created, we're going to let it know which directory we're working with. The path is always taken from the root directory, so for our myUploads folder, we simply pass in /myUploads/. You can also get more organized with a subfolder such as /myUploads/images/.
Next we just need to call the upload() method, and direct it to the name of our file in the $_FILES array. In this case: $_FILES['myFile']. You may repeat this step for as many files as you'd like, simply changing the file name:
$up->upload($_FILES['myFile2']);
That's it. Fire it up and start uploading files to your server.
Deleting a file is just as easy.
include 'easyUp.php';
$up = new fileDir('/myUploads/');
$up->delete('myPic.jpg');
This will delete a jpeg named myPic.jpg from the /myUploads/ directory. Keep in mind that you will receive an error if you try to upload a file where one already exists with the same name. In this situation, use the overwrite() method instead of upload() to replace the old one.
Finally, location() and fileName() retrieve information about the last file uploaded.
if(isset($_POST['mySubmit'])){
include 'easyUp.php';
$up = new fileDir('/myUploads/');
$up->upload($_FILES['myFile']);
echo $up->location();
// outputs: yourServersAbsolutePath/myUploads/
echo $up->fileName();
// outputs: myPic.jpg if our last uploaded file was named myPic.jpg.
}
That's all for now. Upload to your heart's content or use this as a base model for tweaking your own upload class. If you do find something useful to add, leave me a comment.

photostream





