Forum Moderators: coopster

Message Too Old, No Replies

problem with this code on windows

         

laura2k

1:20 am on Apr 17, 2004 (gmt 0)



hello everyone,

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);

ergophobe

4:08 am on Apr 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Does the file exist and is the path valid?

If so, maybe repost your question with just the relevant lines of code. The warning should give you a specific line number.

Tom

laura2k

10:49 am on Apr 17, 2004 (gmt 0)



i dont know if the path exists, but in the script i dont have the option to change the path.

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.

coopster

12:33 pm on Apr 17, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Are you sure that the Windows path shown here is the actual path that will contain the file(s) after an upload?

Files will by default be stored in the server's default temporary directory, unless another location has been given with the

upload_tmp_dir
directive 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.

laura2k

3:36 pm on Apr 17, 2004 (gmt 0)



hi,

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.

ergophobe

6:25 pm on Apr 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




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.

laura2k

6:29 pm on Apr 17, 2004 (gmt 0)



C:\WINDOWS\TEMP does seem to exist but the script just cant get access to it and i think that is why it is getting the error.

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>";

ergophobe

6:36 pm on Apr 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



[edit] Deleted because Laura2K anticipated my suggestion from php.net[/edit]

Okay, why can't you integrate it? I mean, is it because you don't know how, or because you keep getting some sort of error and can't get it to work?

laura2k

6:42 pm on Apr 17, 2004 (gmt 0)



its because i dont know how to :( any help would really help. thanx

ergophobe

6:50 pm on Apr 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Okay. Fair enough. We'll get to that if we have to, but I think it's unlikely that you will be able to move the file if you can't read the file.

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

ergophobe

6:57 pm on Apr 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



PS

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.

laura2k

6:59 pm on Apr 17, 2004 (gmt 0)



thanx tom,

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

ergophobe

8:38 pm on Apr 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Run phpinfo() again and look for the following

- What version of PHP?
- is GD available (search on "GD Support").

laura2k

8:46 pm on Apr 17, 2004 (gmt 0)



the PHP version is: 4.3.2 and i cannot seem to find anything mentioning GD in the information

ergophobe

8:56 pm on Apr 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



imagecreatefromstring() only runs if your server has GD compiled into it.

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

laura2k

8:58 pm on Apr 17, 2004 (gmt 0)



I dont think i have GD Support but as far as i am aware. GD Support is needed because my code tries to adjust the image and create a resized thumbnail image and save it under with the prefix of tn_

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

ergophobe

9:03 pm on Apr 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Sorry, don't have time to do that this weekend.

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.