class.upload.php is a powerful and mature PHP class to manage uploaded files, and manipulate images in many ways. The script is available under a GPL license.
ini_set("memory_limit", "40M");
include_once('classes/upload/class.upload.php');
// as it is multiple uploads, we will parse the $_FILES array to reorganize it into $files
$files = array();
foreach ($_FILES['uploadedfile'] as $k => $l) {
foreach ($l as $i => $v) {
if (!array_key_exists($i, $files)) {
$files[$i] = array();
}
$files[$i][$k] = $v;
}
}
// now we can loop through each $files, and feed each element to the class
/*
* The program flows like this
* 1)Upload and resize Thumbnail to thumbnails folder;
* 2)Upload the actual image to photos folder;
* 3)Insert image info into the DB;
* 4)Redirect to success.
*/
// TODO - Files upload but are not rezised to thumbnails;
// TODO - Temp files are not cleaned up;
foreach ($files as $file) {
// we instanciate the class for each element of $file
// we use the Brazilian Portuguese messages.
$handle = new Upload($file, 'pt_BR');
// First upload the thumbnails
if ($handle->uploaded) {
// now, we start the upload 'process'. That is, to copy the uploaded file
// from its temporary location to the thumb's location
// It could be something like $handle->Process('/home/www/my_uploads/');
/*
* Set variables to resize image to the thumbnails size
*/
$target_thumb_path = $_POST['thumb_url'];
echo '$target_thumb_path = '.$target_thumb_path.' ';
$handle->image_resize = true;
$handle->image_x = 125;
$handle->image_y = 75;
// this line does the actual upload and resizing actions.
$handle->Process($target_thumb_path);
/*
* Set variables to resize image to the full size
*/
$target_path = $_POST['pic_url'].' ';
echo '$target_path = '.$target_path;
$handle->image_resize = true;
$handle->image_x = 640;
$handle->image_y = 480;
$handle->Process($target_path);
if ($handle->processed) { // If Thumbnail successfully uploaded than start processing the main image
$insertSQL = sprintf("INSERT INTO tbl_p_pic (comp_id, owner_id, pic_typ, prop_id, pic_name, pic_desc, pic_url) VALUES (%s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($_SESSION['_compID'], "int"),
GetSQLValueString($_POST['owner_id'], "int"),
GetSQLValueString('image', "text"),
GetSQLValueString($_POST['prop_id'], "int"),
GetSQLValueString($handle->file_dst_name, "text"),
GetSQLValueString($_POST['pic_desc'], "text"),
GetSQLValueString($handle->file_dst_name, "text"));
mysql_select_db($database_ajmedia, $ajmedia);
$Result1 = mysql_query($insertSQL, $ajmedia) or die(mysql_error());
$insertGoTo = "adminpage.php";
if (isset($_SERVER['QUERY_STRING'])) {
// redirect to the main page everything was fine !
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
// next line for debug
// echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded to ".$target_path." and the info was entered into the DB."
} // End redirect to the main page everything was fine !
echo $handle->log;
$handle->clean();
} else
// If Image isn't successfully uploaded than an error message should be returned.
// next line for debug should be replaced by error handling procedure.
// TODO - There is the need for error handling here.
echo "There was an error uploading the file, the database was not updated, please try again!
";
}
}
echo $handle->log;
$handle->clean();
}
Here is my Log: system information - class version : 0.26 - GD version : GD not present - supported image types : none - open_basedir : no restriction - language : pt_BR source is an uploaded file - upload OK - file name OK - MIME type detected as image/jpeg by PHP getimagesize() function - source variables file_src_name : diassis.jpg file_src_name_body : diassis file_src_name_ext : jpg file_src_pathname : D:\WINDOWS\Temp\phpE.tmp file_src_mime : image/jpeg file_src_size : 64538 (max= 2097152) file_src_error : 0 process file to ../content/vivaverde/images/pictures/6/5/44/thumbnails/\ - file size OK - file mime OK : image/jpeg - file name safe format - destination variables file_dst_path : ../content/vivaverde/images/pictures/6/5/44/thumbnails/\ file_dst_name_body : diassis file_dst_name_ext : jpg - no image operation, keep extension - checking for auto_rename - destination file details file_dst_name : diassis.jpg file_dst_pathname : ../content/vivaverde/images/pictures/6/5/44/thumbnails/\diassis.jpg - diassis.jpg doesn't exist already - attempting creating a temp file: file created temp file is: ../content/vivaverde/images/pictures/6/5/44/thumbnails/\d115f65210d4d1ba5873ae0f344e7679.jpg - no image processing wanted - process OK process file to ../content/vivaverde/images/pictures/6/5/44/photos/ \ - file size OK - file mime OK : image/jpeg - file name safe format - destination variables file_dst_path : ../content/vivaverde/images/pictures/6/5/44/photos/ \ file_dst_name_body : diassis file_dst_name_ext : jpg - no image operation, keep extension - checking for auto_rename - destination file details file_dst_name : diassis.jpg file_dst_pathname : ../content/vivaverde/images/pictures/6/5/44/photos/ \diassis.jpg - diassis.jpg doesn't exist already - use the temp file instead of the original file since it is a second process - ../content/vivaverde/images/pictures/6/5/44/photos/ \ doesn't exist. Attempting creation: failed
Now after an upload attempt this is what happens: The Thumbs folder have 3 files none re sized(69a111f37f681a904626ab5623ce601c.jpg size = 0, d115f65210d4d1ba5873ae0f344e7679.jpg size 63Kb and the actual files: diassis.jpg size 53Kb) The photos folder has 0. Am I doing something wrong? what I'm missing? where the '\' are coming from in some of the paths on the log?Reply
Re: Upload does not resize images and doen't clean new!
Firstly, you cannot resize images, your system doesn't have GD installed. So at best, the class will copy your images, but not resize them.
The error maybe also coming from that '\', you need to clean up your variables $target_thumb_path and $target_path (as well as not sending a path through a POST, it is very insecure!).
The class is written for Unix systems, but should be working on Windows, although I offer no support for Windows.Reply
Re: Upload does not resize images and doen't clean new!
Great, I have enabled the GD module on the server and it's now it re sizes the images. The '\' is coming from the class.upload.php :( ? Still it's cleaning up the mess leaving behind those long number file names any idea there? Instead of POST what would have me do?Reply
here my Paths:
Here is my Log:
system information
- class version : 0.26
- GD version : GD not present
- supported image types : none
- open_basedir : no restriction
- language : pt_BR
source is an uploaded file
- upload OK
- file name OK
- MIME type detected as image/jpeg by PHP getimagesize() function
- source variables
file_src_name : diassis.jpg
file_src_name_body : diassis
file_src_name_ext : jpg
file_src_pathname : D:\WINDOWS\Temp\phpE.tmp
file_src_mime : image/jpeg
file_src_size : 64538 (max= 2097152)
file_src_error : 0
process file to ../content/vivaverde/images/pictures/6/5/44/thumbnails/\
- file size OK
- file mime OK : image/jpeg
- file name safe format
- destination variables
file_dst_path : ../content/vivaverde/images/pictures/6/5/44/thumbnails/\
file_dst_name_body : diassis
file_dst_name_ext : jpg
- no image operation, keep extension
- checking for auto_rename
- destination file details
file_dst_name : diassis.jpg
file_dst_pathname : ../content/vivaverde/images/pictures/6/5/44/thumbnails/\diassis.jpg
- diassis.jpg doesn't exist already
- attempting creating a temp file: file created
temp file is: ../content/vivaverde/images/pictures/6/5/44/thumbnails/\d115f65210d4d1ba5873ae0f344e7679.jpg
- no image processing wanted
- process OK
process file to ../content/vivaverde/images/pictures/6/5/44/photos/
\
- file size OK
- file mime OK : image/jpeg
- file name safe format
- destination variables
file_dst_path : ../content/vivaverde/images/pictures/6/5/44/photos/
\
file_dst_name_body : diassis
file_dst_name_ext : jpg
- no image operation, keep extension
- checking for auto_rename
- destination file details
file_dst_name : diassis.jpg
file_dst_pathname : ../content/vivaverde/images/pictures/6/5/44/photos/
\diassis.jpg
- diassis.jpg doesn't exist already
- use the temp file instead of the original file since it is a second process
- ../content/vivaverde/images/pictures/6/5/44/photos/
\ doesn't exist. Attempting creation: failed
Now after an upload attempt this is what happens:
The Thumbs folder have 3 files none re sized(69a111f37f681a904626ab5623ce601c.jpg size = 0, d115f65210d4d1ba5873ae0f344e7679.jpg size 63Kb and the actual files: diassis.jpg size 53Kb)
The photos folder has 0.
Am I doing something wrong? what I'm missing?
where the '\' are coming from in some of the paths on the log?
The error maybe also coming from that '\', you need to clean up your variables $target_thumb_path and $target_path (as well as not sending a path through a POST, it is very insecure!).
The class is written for Unix systems, but should be working on Windows, although I offer no support for Windows.
The '\' is coming from the class.upload.php :( ?
Still it's cleaning up the mess leaving behind those long number file names any idea there?
Instead of POST what would have me do?