Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can i store any kind of files to a blob field in a MySQL server?

Status
Not open for further replies.

gxydas

Programmer
Jan 15, 2005
76
GR
Hi all,
i use this script in PHP 5 to upload the file into a blob field:
Code:
...
$binfile= $_FILES["binfile"];
$binfile_name= addslashes($binfile["name"]);
$binfile_tmp_name= addslashes($binfile["tmp_name"]);
$binfile_size= $binfile["size"];
$binfile_type= $binfile["type"];
$filedata = base64_encode(fread(fopen($binfile_tmp_name, "rb"), filesize($binfile_tmp_name)));
...
$query= "insert into `downloads`(`fileid`,`description`,`filename`,`filesize`,`filetype`,`filedata`,`uploaddate`) "
       ."values ('', '$description','$binfile_name','$binfile_size','$binfile_type','$filedata',NOW());";
...

And i use this script to download the file:
Code:
...
$fileid= $_GET["fileid"];
$query= "select * from downloads where fileid= $fileid;";
$result= mysqli_query($db, $query);
$row= mysqli_fetch_array($result);
$data = base64_decode($row["filedata"]);
$name = $row["filename"];
$size = $row["filesize"];
$type = $row["filetype"];
...
This sample code does not work with jpg or exe files. Works fine with doc, xls, txt.
Any help on that?

thanks anyway.
 
What I STRONGLY suggest is that you upload to the server rather than into the dbase and only insert the path to the file into the dbase. i.e. The pic or program sits in a folder and the path to it sits in the dbase.

Reality is built on a foundation of dreams.
 
Thanks overyde.
I'll do that.
But what am i doing wrong in my code?
 
How are you uploading the file? Is it from a form?
If so did you add the following to the form?
Code:
<form method="post" action="some_process_page.php" enctype="multipart/form-data">
[\code]
When declaring the form and:
[code]
<input type="file" name="file_upload">
I know this seems stupid but some people do seem to forget it!


Reality is built on a foundation of dreams.
 
I've done this overryde.

I solved my issue by doing the way you describe above.
But i'm curious to find out what's the wrong with my code.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top