Forum Moderators: coopster
i have the following code that seems to work perfectly on a linux webserver with PHP but when i use it on a Windows server with PHP i get an error saying:
Warning: filesize(): Stat failed for C:\WINDOWS\TEMP\phpC683.tmp (errno=2 - No such file or directory)
Can anyone please help me solve the problem?
Many thanx in advanced
$photoFileName = $_FILES['photo']['name']; // get client side file name
if ($photoFileName) // file uploaded
{
$fileNameParts = explode(".", $photoFileName);
$fileExtension = end($fileNameParts); // part behind last dot
if ($fileExtension!= "jpg"&& $fileExtension!= "JPEG"&& $fileExtension!= "JPG") die ("Choose a JPG for the photo");
$photoSize = $_FILES['photo']['size']; // size of uploaded file
if ($photoSize == 0) die ("Sorry. The upload of $photoFileName has failed.");
}
// read photo
$tempFileName = $_FILES['photo']['tmp_name']; // temporary file at server side
$tempFile = fopen($tempFileName, "r");
$binaryPhoto = fread($tempFile, fileSize($tempFileName));
$fullSize=$binaryPhoto;
// Try to read image
$src_img = imagecreatefromstring($binaryPhoto); // try to create image
$width = imagesx($src_img); // get original source image width
$height = imagesy($src_img); // and height
// create small thumbnail
$dest_width = 206;
$dest_height = round($height*206/$width);
$dest_img = imagecreatetruecolor($dest_width, $dest_height);
//$dest_img = imagecreate($dest_width, $dest_height);
/*$result = imagecopyresampled(
$dest_img, $src_img,
0, 0, 0, 0,
$dest_width, $dest_height,
$width, $height); // resize the image
*/
imagecopyresized(
$dest_img, $src_img,
0, 0, 0, 0,
$dest_width, $dest_height,
$width, $height); // resize the image
ob_start();
imageJPEG($dest_img);
$binaryThumbnail = ob_get_contents();
ob_end_clean();
if (!$fp=fopen("./data/$photoFileName","w")) die("Check /data/ permissions set to 777");
fwrite($fp,$fullSize);
fclose($fp);
$fp=fopen("./data/tn_$photoFileName","w");
fwrite($fp,$binaryThumbnail);
fclose($fp);
i get the following error on the following line:
Warning: filesize(): Stat failed for C:\WINDOWS\TEMP\phpDE21.tmp (errno=2 - No such file or directory)
$binaryPhoto = fread($tempFile, fileSize($tempFileName));
and below that i also get another error on the following line:
Fatal error: Call to undefined function: imagecreatefromstring()
$src_img = imagecreatefromstring($binaryPhoto);
Many thanx in advanced.
Files will by default be stored in the server's default temporary directory, unless another location has been given with the
upload_tmp_dirdirective in
php.ini.
Resource:
Handling file uploads [php.net]
The second half of your message may be related to the different library extensions that may be needed to process certain file types. The PHP Image functions [php.net] pages offer more information and troubleshooting guidance.
i just done a phpinfo(); check and it seems that i do not have a value set for upload_tmp_dir, i cannot edit the php.ini file as I am on a shared server. Is there any other way to sort that out.
As for the second error, I am very confused about that, can anyone please tell me how i can fix that.
thanx in advanced.
i dont know if the path exists, but in the script i dont have the option to change the path.
This is a common problem running scripts on Windows that were designed for *nix.
If you can't change the path, what you can do, however, is create the path. So in other words, if there is no C:\WINDOWS\TEMP directory, you may be able to create one, depending on the privileges you have on the server. So you don't change the path, you just make sure that the default path is there.
I downloaded the following code from PHP.net and this seems to work as it has the option to specify upload directory, but i cannot seem to get it incorporated into my code.
<?php
$uploaddir = 'd:/path/to/htdocs/';
$uploadfile = $uploaddir . $_FILES['userfile']['name'];
print "<pre>";
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
print "File is valid, and was successfully uploaded. ";
print "Here's some more debugging info:\n";
print_r($_FILES);
} else {
print "Possible file upload attack! Here's some debugging info:\n";
print_r($_FILES);
}
print "</pre>";
Let's just back up a bit and find out why the file read is failing. The problem is that you are getting the file size via filesize() and that is failing.
You don't have to do this, since you already have the file size. It's in
$_FILES['photo']['size']
by default. So on the line that's blowing up, let's rewrite that. Instead of
$binaryPhoto = fread($tempFile, fileSize($tempFileName));
try
$binaryPhoto = fread($tempFile, ($_FILES['photo']['size'] + 1024));
The + 1024 part is because I find that *nix and Win generally report slightly different file sizes and this gives you 1KB of slack so that it doesn't stop reading before the eof.
See if that works.
Tom
If the above doesn't work go to the line one above that and do like so:
if (!$tempFile = fopen($tempFileName, "rb")) {
echo "File open failed for file $tempFileName";
exit();
}
Then you can find out whether it's really opening the file or not. Basically, there are two changes there.
1. it puts it in an "if" and exits if the file open fails (obvious).
2. easier to miss, it sets the "b" flag, which the manual says you should always do in Windows when reading binary files.
we're now one error down. but the second error still remains. the error is:
Fatal error: Call to undefined function: imagecreatefromstring() on the following line
$src_img = imagecreatefromstring($binaryPhoto); // try to create image
any ideas?
thanx once again, really appreciated
With PHP for Win, it's available as an option (you don't have to compile your own), but you do need access to the php.ini file. As I understand it, you don't. In that case you will have to beg the server admin to uncomment (i.e. remove the semicolon) the line:
;extension=php_gd2.dll
and verify that php_gd2.dll is available in the php extensions path as defined by the
extension_dir
directive in php.ini.
Barring that, you'll have to find another script or modify the existing one so that it uses an image library that you have access too.
Tom
if there is no way to do that without having GD suport, can you please just tell me how i can get the code to work without it and get the code just to use the original image as the thumbnail.
thanx
Basically, you just skip most of what you are doing and send the original image to the screen and use html image tag to set the size.
Major drawbacks:
- your thumbnail will look terrible
- your file size will be large. If you have a lot of thumbnails on a page, it will take forever to load the whole page.