NerdyHearn
Home
Blog

Contact
Mailing List

Software

Active Directory Products
Object Compare
Permission Compare

IPhone Products
Calls To Calendar
SMS To CSV
SMS To Gmail
Voicemail To Gmail

Sites
DocuTerminal
How Long For Me
My Music To Me
SaveMySerials
TypeCount

Blog
Twitter

NerdyHearn - Blog


<< Back To All Blogs

A simple uploader class for PHP

Saturday, March 29th, 2008

I am a big fan of re-useable code. Because of this I tend to create classes for any form of common operation that I perform on any of my sites. I (like most other people with websites) do a lot of uploading of files, from images, to programs and executables. Because of this I recently created an Uploader class in PHP to upload files to my server, at a specified path. Here is the code for anyone that may be interested.

The class itself:

class Uploader {
// The base directory to upload the file to such as /home/user/myfiles
var $directory = "";
// The entire path to the file after upload, so you can reference the file if needed
var $path = "";

function Uploader($directory) {
$this->directory = $directory;
}

function Upload($varname) {
$this->path = $this->directory . basename($_FILES[$varname]['name']);
if (move_uploaded_file($_FILES[$varname]['tmp_name'], $this->path)) {
return(true);
} else{
$this->path = "";
return(false);
}
}
}

Using the class is very simple:

Instantiate the class:
$uploader = new Uploader("/home/user/myfiles");

to actually upload the file:
if ($uploader->Upload("myhtmlinputname")) {
echo "Successfully uploaded file " . $uploader->path;
} else {
echo "Upload failed";
}

A few notes:
The Upload function takes a single argument, which is the input variable name of the file input, so in the case of the example above, this would be:

<input type="file" name="myhtmlinputname">

You will need to add the enctype attribute to your form syntax in order to be able to access files, so your complete form syntax after adding enctype would be something as follows:

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

The Upload function returns a boolean value, indicating true for a successful upload, and false for a failed upload.

Thats all from me for now, happy uploading!

Tom "NerdyHearn" Out.

Comments

Currently no comments.

Add A Comment

Name:


URL:


Email Address: (not public, used to send notifications on further comments)


Comments:



Enter the text above, except for the 1st and last character:


NerdyHearn - Latest tech news relating to C#, ASP.NET, SharePoint, PHP, general development, and more. DocuTerminal - Online Filing Cabinet solution. Scan, search and archive your paper documents. SaveMySerials - Protect yourself from theft, fire, natural disasters and more by recording your serial numbers My Music To Me - Stream your subsonic music collection to your Sonos wireless system TypeCount - Count how often you type across different computers! ServrTrackr - Monitor your SSL certificates, website uptime, and DNS configurations