Jump to content

Insert textfile as blob into MYSQL - CONVERT to OCTET Before


mickey23

Recommended Posts

I have ran into an interesting problem. What I am trying to do is read in a file from the filesystem and upload it into the mysql database. I am able to do this with no problem, but I have notice that whenever I do read the file in and upload it I am uploading the actual text of the file. The file is sensitive, so it can not be mangled in any way or else the application that uses it will complain. I need to be able to do something like phpmyadmin does. PHPmyadmin converts the file into an octet and then uploads it. It genereates a SQL statement like so:

 

INSERT INTO table (id, file) VALUES (58,0x4d79206c6963656e73652066696c652e2e2e2e).

 

I am also able to echo this octet and download the file is pristine condition.

 

$query = "SELECT file, FROM table WHERE id='$id'";
$result = @mysqli_query($dbc,$query);
list($file) = mysqli_fetch_array($r);	
header("Content-type: myfile");
header("Content-Disposition: attachment; filename='myfilename'");
echo $file;

 

Whenever I read the file in and generate my SQL statement it looks like:

 

INSERT INTO table (id, file) VALUES (58,Some text here test test) where "Some text here test test" is the actual text in the file.

 

Whenever I try and download this the file appears to have been tampered with and is no good to application that uses the file.

 

My Question is how do I get the file contents in an octet representation e.g "0x4d79206c6963656e73652066696c652e2e2e2e" before I insert the info into mysql?

 

What I am doing to insert the blob:

include(dbconn.php)
$file = fopen($license,'r');//$file is the actual location to the file e.g /var/www/sandb/myfile.txt
$file_content = fread($file,filesize($license));
$file_content = addslashes($file_content);
fclose($file);

$query = INSERT INTO [table] (id, file) VALUES (58,$file_content)
$result = mysqli_query($dbc, $query);

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.