strange problem with var holding file contents

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bbotts
Forum Newbie
Posts: 3
Joined: Wed Aug 20, 2003 5:19 pm

strange problem with var holding file contents

Post 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
bbotts
Forum Newbie
Posts: 3
Joined: Wed Aug 20, 2003 5:19 pm

Post 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?
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Doesn't that figure? After you have added slashes, the string is bound to be longer!?
bbotts
Forum Newbie
Posts: 3
Joined: Wed Aug 20, 2003 5:19 pm

Post 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.
User avatar
greenhorn666
Forum Commoner
Posts: 87
Joined: Thu Aug 14, 2003 7:14 am
Location: Brussels, Belgium

Post by greenhorn666 »

Where you reading from a file off the fs?
or an url over network?
User avatar
greenhorn666
Forum Commoner
Posts: 87
Joined: Thu Aug 14, 2003 7:14 am
Location: Brussels, Belgium

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