Page 1 of 1

strange problem with var holding file contents

Posted: Wed Aug 20, 2003 5:19 pm
by bbotts
I am having this strange problem with a script that I wrote. It was working for about a year until I updated my server last night. The script takes the contents of an image file and stores it in a database for later use.

Code: Select all

// Set the maximum file size
$max_file_size = (1024 * 1024 * 2);  // 2MB

// Read the file contents into the variable
$bin_data = fread($fp, $max_file_size);
fclose($fp);		

// Escape special characters in the file
$bin_data = addslashes($bin_data);
if (!unlink($new_img_location))
	echo "Unable to unlink $img.<br><br>\n";
for some reason, it is only taking 16KB of the file now. I've done a few tests using strlen() and commenting out the unlink() to see what is happening and it seems to just stop reading the file at 16KB (actually 16,008 characters). The entire file makes it to the temporary directory. I've downloaded them from there and opened them to verify that.

It is very perplexing to me that updating the server would stop this script from working. Do any of you know what could be causing this? Did I set something wrong in php.ini or something in httpd.conf or something crazy like that?

Please help. My boss is a little upset about this.

Thanks in advance,
Ben

Posted: Thu Aug 21, 2003 8:46 am
by bbotts
I noticed something else. After dumping the contents of the file into the variable, I am able to addslashes() to it. After that, the lenght of the string is greater. Is there something screwy with fread() in php 4.3.2?

Posted: Thu Aug 21, 2003 8:52 am
by JayBird
Doesn't that figure? After you have added slashes, the string is bound to be longer!?

Posted: Thu Aug 21, 2003 9:34 am
by bbotts
Doesn't that figure? After you have added slashes, the string is bound to be longer!?
Yes, but I thought it was a problem with my php installation. Thanks for being arrogant though. I really appreciate it. If you would have read my post, you would have seen these few lines.
for some reason, it is only taking 16KB of the file now. I've done a few tests using strlen() and commenting out the unlink() to see what is happening and it seems to just stop reading the file at 16KB (actually 16,008 characters). The entire file makes it to the temporary directory. I've downloaded them from there and opened them to verify that.

It is very perplexing to me that updating the server would stop this script from working.
Perplexing means confusing. I was confused. I'm sorry I was unable to express myself monosyllabically enough for you to understand what my problem was.

I thought I was asking for help. After rereading my posts, I guess I was asking for smart-ass comments.

Anyway, I have figured out the problem. It was with my script. Apparently, in php 4.3.2, they "fixed" fread() so it stops at one block. If any of you care, file_get_contents() does most of what the code I previously posted does.

This problem is solved.

Thank you to those of you who actually thought about trying to help me.

Posted: Thu Aug 21, 2003 9:49 am
by greenhorn666
Where you reading from a file off the fs?
or an url over network?

Posted: Thu Aug 21, 2003 9:52 am
by greenhorn666
Well I indeed just saw too they "fixed" fread()...
... and noticed the "temporary directory" part :P
Anyhow, I am not quite sure reading a max 2mb chunk at once is pretty wise... Now I'm just trying to be a smart ass ;)