views:

78

answers:

5

I have a script that gets the raw binary image data via url request. It then takes the data and puts it into mysql.

Pretty simple right? Well It's I'm inserting some 8,000 decent sized 600x400 jpegs and for some odd reason some of the images are getting cut off. Maybe the part of my script that iterates through each image it needs to get is going to fast?

When I do a straight request to the URL I can see all the raw image data, but on my end, the data is cut off some way down the line.

Any ides why?

+1  A: 

Is something in the chain treating the binary data as a string, in particular a C style null-terminated string? That could cause it to get cut off at the first null byte ('\0').

John Kugelman
i mean what other way could i do it without using a string?
John
but other than that i think you might be on to something...
John
A: 

Have you tried simply call your script that pulls the binary image, and dump it out. If you see the image correctly then its not pulling part, might be something to do with inserting.

Darkerstar
$this->query("insert into images (db,pid,raw) values('".$s."','".$valid_pid."','".mysql_real_escape_string($new_image)."')");that's how i'm inserting it
John
have you tried add_slashes?
Darkerstar
in addition to, inside or outside of my "mysql_real_escape_string()", or what? and what would the difference be if any?
John
A: 

Are you setting the headers correctly?

ie:

header('Content-Length: '.strlen($imagedata));
header('Content-Type: image/png');
...
SeanJA
is the content length necessary?
John
A: 

A string datatype would definitely not be the optimum for storing images in a DB.

In fact I've seen several recommendations that the image should go in a folder somewhere in your filesystem and the DB contains only the address/file path.

This is a link to a page about inserting images.

It contains the suggestion about the filepath and that a blob datatype is better if the images must go in the database.

If it's a blob, then treating it as a string won't work.

pavium
Ok I'm not sure what you mean by string. If you refering to a PHP string, or a MySQL string, if it's the latter I've never heard of that.The images must go in the db, I am using blob, and it's set to binary. Could it be that my script is iterating through the loop to fast, the part that does the http request for the data and inserts it into the db?
John
Ok, so the MySQL 'string' datype would be callled VARCHAR or CHAR, but all that means is a series of characters. In most programmming languages, and the other answers, that would be called a string. As John Kugelman said, treating binary data like a string might cause problems. Since his answer, you've commented that you're using a blob. That's good, but I thought John's suggestion was good at the time and I upvoted it. Now it seems the answer lies somewhere else. I wouldn't blame your loop because I haven't seen it.
pavium
A: 

If you make repeated requests to the same url, does the image eventually load?

If so that points to a networking issue. Large packet support is enabled in your kernal (assuming linux) which doesn't work correctly for a lot of windows clients. I've seen a similar issue with large(1+MB) javascript libraries served from a linux machine.

http://en.wikipedia.org/wiki/TCP%5Fwindow%5Fscale%5Foption

http://support.microsoft.com/kb/314053

txyoji
It loads the same regardless of how many times you look at it, or ctrl+f5 the hell out of it, lol. Trust me, I'm lazy I tried that already.
John