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.
Thank you - it works. Just as reference to other users, the core content of my ajax_upload_image.php is as follows. This responds to the AJAX query from the above code. The AJAX code passes the tmp-file name that the Upload class uses to upload the image, and the actual file name.
I'm then using PHP to format the image file name (remove .extension). And then $handle->file_new_name_body = $orig_file; command to set the file name for the saved image.
As Colin said there is probably XMLhttpRequest method to pass the information automatically but I could not get it working. But the AJAX way works now.
// Destination directory for images $dir_dest = '../product/images/';
// we create an instance of the class, giving as argument the PHP object // corresponding to the file field from the form // All the uploads are accessible from the PHP object $_FILES $handle = new Upload($filename); $return_arr = array();
// then we check if the file has been uploaded properly // in its *temporary* location in the server (often, it is /tmp) if ($handle->uploaded) {
// yes, the file is on the server // below are some example settings which can be used if the uploaded file is an image. $handle->image_convert = 'jpg'; $handle->image_resize = true; $handle->image_ratio_fill = true; $handle->image_x = 64; $handle->image_y = 64; $handle->file_new_name_body = $orig_file;
// now, we start the upload 'process'. That is, to copy the uploaded file // from its temporary location to the wanted location // It could be something like $handle->Process('/home/www/my_uploads/'); $handle->Process($dir_dest);
// we check if everything went OK if ($handle->processed) { // everything was fine ! echo 'File uploaded with success and formatted to 64x64 pixels (Small Image)'; echo '<img src="'.$dir_pics.'/' . $handle->file_dst_name . '" />'; $info = getimagesize($handle->file_dst_pathname); echo ' File: <a href="'.$dir_pics.'/' . $handle->file_dst_name . '">' . $handle->file_dst_name . ' '; echo ' (' . $info['mime'] . ' - ' . $info[0] . ' x ' . $info[1] .' - ' . round(filesize($handle->file_dst_pathname)/256)/4 . 'KB)'; } else { // one error occured echo 'File not uploaded to the wanted location'; echo 'Error: ' . $handle->error; }
// we delete the temporary files $handle-> Clean();
} else { // if we're here, the upload file failed for some reasons // i.e. the server didn't receive the file echo 'File not uploaded on the server'; echo 'Error: ' . $handle->error; }
I'm then using PHP to format the image file name (remove .extension). And then $handle->file_new_name_body = $orig_file; command to set the file name for the saved image.
As Colin said there is probably XMLhttpRequest method to pass the information automatically but I could not get it working. But the AJAX way works now.