File uploading, validation and security is such a pain for programmer. To simplify the problem I created a class called ‘File Upload and validation’. The class is currently hosted in phpclasses.org site and in Hotscript. For the people who are using or will be using the file this documentation will come in handy and useful to use the class efficiently.
File to Include:
You only need to include the main class file in your back-end server side form processing file. the main class file is ‘ MazUploadClass.php’.
Example Files:
Other two file, one is html file called ‘index.html’ and another back-end example PHP processing file namely ‘uploader.php’, in the package I included is to test out the class and and understand the way it works.
Functions:
uploadPDF($test, $siz, $fletype);
This function is responsible for checking the uploaded file’s type and MIME type as well. This function will validate the uploaded file. The parameters all are mandatory. $test variable is the example variable of holding the uploaded file. Such as:
$test= $_FILES['pdf'];
$siz This variable is responsible for the uploaded file size, developer can give the file size limitation here so that the file exceeding the given file size won’t be uploaded and will show the error message instead.
$fletype is the variable for file type that developer want his user to upload. For example, if I want to allow user to upload image then my code will look like this:
$fletype= "image";
currently the class allows image, pdf, audio and video file type. The string $fletype variable should be holding exactly “image”(for Image), “pdf”(for pdf files), “audio”(for audio files) and “video”(for video files) those strings.
The function return an array of strings. The return value and error of files can be judged by calling this variable:
errorFlag['fileFlag'] == "yes"; /*if it is true than the uploaded file MIME type is not correct or file type is not valid Give user an error message. if false you are good to upload your files*/
Second Function:
createPath($fletype);//any string that you want to be named as a folder name.
This function will return a valid path created on the server while uploading the file. The path will be “user_uploads/$fletype/year/month/day/” from your root. only changeable variable is $fletype, other than that other string and variable are predefined and cannot be changed unless you hack the class itself.
Random Code:
randomCode();
This function ill create random code to add before the file name of uploaded file to make every filename unique.
Uploaded Function:
uploadToServer($tmpFileName, $originalPath);
This is the function to call to move the temporary uploaded file to the original path. If the file is uploaded properly then it will return true. $tmpFileName is the variable for temporary uploaded file and $originalPath is the path where the file will be stored, developer will be use this path to store in the database as a string.
Usages:
Using this class developer can allow users to upload image, pdf files, audio files and video files.
The class package file can be downloaded from Here.
Any feedback or bug report can be submitted in this blogs comment section. I will try to update as early as I can. If you want to buy me a coffee *that will be great* Donate.














