Reputation: 18103
Please take a look at this: http://phpbin.net/x/1841090478
Now I am trying to expand this code and add more checkers, like exif_imagetype() as you can see on line 139.
I cant get it to work, Im just receiving that exif_imagetype failed to open stream, no such... ( that the image doesnt exists ). I also tried $this->file->getName() inside the parameter, but still the same error.
Now I am questioning the whole thing. I cant see anywhere the image gets uploaded before it gets to the if($this->file->save .. but how can it retrieve pathinfo() then? And why wont ['dirname'].['basename'] to find the file not work? for the dirname i just get a . but the basename i get the correct image file name i am trying to upload.
So how does it work and where should i implement this exif_imagetype checker into this code?
Thank you in forward.
Upvotes: 1
Views: 2076
Reputation: 83632
Just a quick and dirty implementation for the XHR case (as discussed in the chat):
/**
* Handle file uploads via XMLHttpRequest
*/
class qqUploadedFileXhr {
protected $_tempFile;
public function __construct() {
$input = fopen("php://input", "r");
$this->_tempFile = tempnam(sys_get_temp_dir(), 'xhr_upload_');
file_put_contents($this->_tempFile, $input);
fclose($input);
}
public function checkImageType() {
switch(exif_imagetype( $this->_tempFile )) {
case IMAGETYPE_GIF:
case IMAGETYPE_JPEG:
case IMAGETYPE_PNG:
return true;
break;
default:
return false;
break;
}
}
/**
* Save the file to the specified path
* @return boolean TRUE on success
*/
function save($path) {
if (filesize($this->_tempFile) != $this->getSize()){
return false;
}
rename($this->_tempFile, $path);
return true;
}
function getName() {
return $_GET['qqfile'];
}
function getSize() {
if (isset($_SERVER["CONTENT_LENGTH"])){
return (int)$_SERVER["CONTENT_LENGTH"];
} else {
throw new Exception('Getting content length is not supported.');
}
}
}
Upvotes: 3