Solved

Php file upload doesn't work

Posted on 2012-04-04
4
450 Views
Last Modified: 2012-04-05
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
0
Comment
Question by:starhu
  • 2
4 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 37805705
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.
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 37805724
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
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 37805725
There are many things that can go wrong with the script, and they are largely dependent on the external data that the script receives.  You may want to try getting rid of addslashes and using mysql_real_escape_string() instead.  You may want to use base64_encode() to make the external data safe for binary transport.  You may want to read about the risks associated with file uploads, particularly since the code does not have any checks to see if the information is actually an uploaded file!
http://us.php.net/manual/en/function.is-uploaded-file.php
0
 

Author Closing Comment

by:starhu
ID: 37810617
base64_encode() solved the problem
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question