I’m using this file uploader and decided to validate actual content of the uploaded file to make sure that it is indeed an image. So I wanna use The PHP getimagesize() function which takes filename.
But there is no $_FILES['xxx']['tmp_name'] so I pass it to getimagesize function. What do you recommend to validate actual content type in my upload process?
/**
* Handle file uploads via XMLHttpRequest
*/
class qqUploadedFileXhr {
/**
* Save the file to the specified path
* @return boolean TRUE on success
*/
function save($path) {
$input = fopen("php://input", "r");
$temp = tmpfile();
$realSize = stream_copy_to_stream($input, $temp);
fclose($input);
$target = fopen($path, "w");
fseek($temp, 0, SEEK_SET);
stream_copy_to_stream($temp, $target);
fclose($target);
return true;
}
function getName() {
return $_GET['qqfile'];
}
function getImageInfo() {
return getimagesize($_GET['qqfile']); /* no such file or directory */
}
}
Update
if ($this->file->save($uploadDirectory . $filename . $ext)){
// if only images are allowed to upload
if ($imgOnly)
{
// get image size and if $imgInfo were false delete uploaded file
$imgInfo = getimagesize($uploadDirectory . $filename . $ext);
if (!$imgInfo)
{
unlink($uploadDirectory . $filename . $ext);
return array('error' => 'The file uploaded is not actual image file.');
}
}
return array('success'=>true);
} else {
return array('error'=> 'Could not save uploaded file.' .
'The upload was cancelled, or server error encountered');
}
The function
savetakes a string parameter and saves the uploaded image to a file with that path. So just open whatever filename you are passing tosave().