Forum Moderators: coopster

Message Too Old, No Replies

php + mysql + blob

         

enginaar

10:36 am on Dec 7, 2004 (gmt 0)

10+ Year Member



Hi,

I've to write a swf file to mysql to a longblob field. The code to read file and write to table is below.

function asc2hex ($temp) {
$len = strlen($temp);
for ($i=0; $i<$len; $i++) $data.=sprintf("%02x",ord(substr($temp,$i,1)));
return $data;
}

$dosya = "video.swf";
$data = fread(fopen($dosya, 'rb'), filesize($dosya));
$data = asc2hex($data);

mysql_query("INSERT INTO tbl_swf(mov) VALUES '$data')");

Original file size is 1.580.894 bytes and the size after asc2hex() function is 3.161.788 bytes. After running script nothing changes in table.

As well, i can write a file that has only 15K size to table.

I though LONGBLOB types limit is 4MB, why can't i write a 3 MB file.

Salsa

12:18 pm on Dec 7, 2004 (gmt 0)

10+ Year Member



One problem is that you're missing a parenthasis in your query:

mysql_query("INSERT INTO tbl_swf(mov) VALUES ('$data')");

enginaar

1:46 pm on Dec 7, 2004 (gmt 0)

10+ Year Member



:) :) :)
I guess, it was deleted while i was modifying it to write this message.

It's included in the code that i'm using. The script is working with files that has small sizes and not working with files near 1.5 MB (hex = 3 MB) although longblob type limit is 4MB.

Salsa

3:02 pm on Dec 7, 2004 (gmt 0)

10+ Year Member



Wouldn't it have been nice had it been that simple?

But I don't think your issue is with the MySQL blob type column. If you try to insert data that is too long for the column type, MySQL should truncate the data, not refuse to insert it at all.

It seems more likely to look into stuff like upload_max_filesize in your php.ini, or similar limitations on your server. What kind of error checking are you doing?

[edited]
Try adding in some error checking in appropriate places, like:

if (!file_exists($temp)) die("The file, $temp, does not exist")); 
if (!$data) die("\$data is empty"));
elseif (strlen($data) > 4294967300) die("\$data will be truncated"));
if (!mysql_query("INSERT INTO tbl_swf(mov) VALUES ('$data')"))
die("The query failed: ".mysql_error());

[/edited]

jatar_k

5:23 pm on Dec 7, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld enginaar

enginaar

7:21 am on Dec 8, 2004 (gmt 0)

10+ Year Member



Thanks for greeting.

I've done the change:

mysql_query("INSERT INTO tbl_swf(mov) VALUES('$data')") or die("The query failed: ".mysql_error());

The error message is.

The query failed: MySQL server has gone away

:):):) It's the most different error messages i've ever seen.

enginaar

7:54 am on Dec 8, 2004 (gmt 0)

10+ Year Member



Besides of that, there is no problem when i write the read data to a file. The problem occurs only writing to database.

I did a length check and realized that i can only write data to database which has a max 1047515bytes length not 1047516.

enginaar

8:02 am on Dec 8, 2004 (gmt 0)

10+ Year Member



OK. I dudit. :)

There is a line in my.ini file that defines the max allowed packet size. Default was 1M, i raised it to 8M like below.

set-variable = max_allowed_packet=8M

Thanks everyone.

jatar_k

5:20 pm on Dec 8, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



ah nice catch enginaar

I have to admit that the most entertaining and deflating error message is

MySQL server has gone away

I guess it doesn't want to play anymore ;)