Link to home
Start Free TrialLog in
Avatar of starhu
starhu

asked on

Php file upload doesn't work

Hello,

I have a large blob field in a Mysql table.

When upload a file with phpmyadmin into this field, it works fine.

But when i use the script below to upload, the file is broken when downloading.
I tested with small and big files too.

Upload script :
$tmpName = $_FILES["file"]["tmp_name"];
$fileName = $_FILES["file"]["name"];
$fileType = $_FILES["file"]["type"];
$fileSize = $_FILES["file"]["size"];
$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
$insert = "INSERT INTO bizt_fileok (ajanlat_id, file_name, file_size, file_type, content)  VALUES ('$maxId', '$fileName', '$fileSize', '$fileType', '$content')";

Database Structure: id (int 11 auto_inc), ajanlat_id (int 11),  file_name(varchar45), file_size (int 11), file_type (varchar45), content (longblob).

Download script:
header('Content-Type: '.$file["file_type"].'');
header('Content-Disposition: attachment; filename="'.$file["file_name"].'"');
echo $file["content"];

The script above worked a few weeks ago and then it stopped working properly.
Can it be a server side problem or my code is bad?

Thank you
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

I have a large blob field in a Mysql table.
Don't do that.  Instead store the "large blob" in the server file system.  Data bases are not well designed to store blobs.
Avatar of Marco Gasi
Maybe you have simply forgot to write this part in your question, but I don't see a $maxId definition in your code: if that is missing, your query won't work. Have you tried to run your script using mysql_error() function?

$insert = "INSERT INTO bizt_fileok (ajanlat_id, file_name, file_size, file_type, content)  VALUES ('$maxId', '$fileName', '$fileSize', '$fileType', '$content')";

mysql_query($uinsert) or die("Problem with the query :" . mysql_error() . "<br>The query was:<br>" . $insert);

This could show you where is the problem...

Cheers
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of starhu
starhu

ASKER

base64_encode() solved the problem