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-Dispositio n: attachment; filename="'.$file["file_na me"].'"');
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
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-Dispositio
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
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
$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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
base64_encode() solved the problem