Code: Select all
<?php
/**
* @Author : Jonathon Hibbard
*
* Provides File Management Services (Upload, Copy, Create, MkDIR, etc.)
*/
class File_Upload_Manager {
/**
* Uploads the file and returns the result
*
* @param string $img_name //name of image
* @param string $path //path we want to use for uploading
* @param string $postFile //name of image field in form
* @return $pic //image information
*/
function upload_image($path = NULL, $postFile = NULL) {
global $HTTP_POST_FILES;
$img_name =& basename($HTTP_POST_FILES[$postFile]['name']);
$max_size=1000000;
if(isset($HTTP_POST_FILES[$postFile]['name']) && !empty($HTTP_POST_FILES[$postFile]['name'])) {
$file_check = $path.$HTTP_POST_FILES[$postFile]['name'];
if(file_exists($file_check)) {
$temp_filename = basename($file_check,'.jpg');
$temp_filename .= date("YmdHis").'.jpg';
$new_path = $path.$img_name.'/'.$temp_filename;
rename($file_check,$new_path);
}
}
if(!is_dir($path.$img_name))
mkdir($path.$img_name,0775);
$path = $path.$img_name;
if(is_uploaded_file($HTTP_POST_FILES[$postFile]['tmp_name'])) {
if($HTTP_POST_FILES[$postFile]['size'] > $max_size)
die($this->jsAlert("The file '.$HTTP_POST_FILES[$postFile]['name'].' is too big. It must be smaller than 1mb. Upload Failed!"));
$this->copyImage(&$postFile, &$path);
}
if($HTTP_POST_FILES[$postFile]['name'])
$pic = $path.$img_name.'/'.basename($HTTP_POST_FILES[$postFile]['name']);
else
$pic = 'No Picture';
return $pic;
}
/**
* Copies the Image and removes any images remaining that may have the same name...
*
* @param string $field_name //form's field name of image
* @param string $path //Path to directory
*/
function copyImage($field_name = NULL,$path = NULL) {
global $HTTP_POST_FILES;
if($HTTP_POST_FILES[$field_name]['type']=="image/jpeg" || $HTTP_POST_FILES[$field_name]['type']=="image/pjpeg" || $HTTP_POST_FILES[$field_name]['type']=="image/jpg" || $HTTP_POST_FILES[$field_name]['type']=="image/gif") {
if(file_exists($path.$HTTP_POST_FILES[$field_name]['name'])) {
unlink($path.$HTTP_POST_FILES[$field_name]['name']);
}
$res = copy($HTTP_POST_FILES[$field_name]['tmp_name'], $path.$HTTP_POST_FILES[$field_name]['name']);
if(!$res)
die($this->jsAlert("Upload Failed on line ".__LINE__."!"));
} else
die($this->jsAlert($field_name.' : Wrong File Type. The image must be a JPEG'));
}
function jsAlert($message) {
echo '<script language="javascript">
alert("'.$message.'");
</script>';
}
}
?>Code: Select all
$fileMan = new File_Upload_Manager();
$image_path_result = $fileMan->('name_of_image_on_form');