Jump to content

Storing files in BLOB


karigarh

Recommended Posts

Hi,

 

I am having a wierd problem and I dont know what is causing it - PHP/MySql.?

 

Server  - Apache on Fedora

Php - 5.3.2

MySql - 5.1.47

 

In the same server config I have implemented storing files into blob fields any type of file by using mysql as well as mysqli.

The one implemented by mysql is working just fine.

 

But the one using mysqli is not working for anything other than text files.

For image files the following error is coming -

Error interpreting JPEG image file (Not a JPEG file: starts with 0x0a 0xff)

For word/pdf etc the files are getting corrupted in such a way that the concerned application is not recognising it as a valid document.

 

 

 

/*MySQL
using mysql_connect
Storing*/
$name=trim($_POST['fname']);
$desc=trim($_POST['description']);
$file=$_FILES['formfile']['name'];
$filetype=$_FILES['formfile']['type'];
$filename=$_FILES['formfile']['tmp_name'];
$fileerror=$_FILES['formfile']['error'];
$filesize=$_FILES['formfile']['size'];

$fp = fopen($filename, 'r');
$content = fread($fp, filesize($filename));
fclose($fp);
if (!get_magic_quotes_gpc())
{
$content = addslashes($content);
$name=addslashes($name);
}

$query="insert into forms_master (name, description, data, filename, filesize, filetype, entrydate) values (";
$query.="'$name', '$desc', '$content', '$file', '$filesize', '$filetype', now())";

/*Retreiving*/
$id=$_GET[sha1('id')];
$form=mysql_fetch_array(mysql_query("select filename, filesize, filetype, data from forms_master where sha1(id)='$id'", $link));
header("Content-length: ".$form['filesize']);
header("Content-type: ".$form['filetype']);
header("Content-Disposition: attachment; filename=".$form['filename']);
print $form['data'];

 

Works absolutely fine

 

/*MySQL using mysqli_connect
Storing
*/
$file=$_FILES['fname'];
$name=$file['name'];
$tmp_name=$file['tmp_name'];
$size=$file['size'];
$type=$file['type'];
$fp = fopen($tmp_name, 'r');
$content = fread($fp, filesize($tmp_name));
fclose($fp);
$content = mysqli_real_escape_string($link, $content);
$name=mysqli_real_escape_string($link, $name);

$query="insert into document_master (name, size, filetype, data) values ('$name', '$size', '$type', '$content')";

//Retreiving
$id=$_GET[sha1('id')];
$file=mysqli_fetch_array(mysqli_query($link, "select name, size, filetype, data from document_master where sha1(id)='$id'"));
header("Content-type: ".$file['filetype']);
header("Content-length: ".strlen($file['size']));
header("Content-Disposition: attachment; filename=".$file['name']);
print $file['data'];

 

Note.

I uploaded an image file through phpMyAdmin.

Then tried to retreive it via the mysqli code above. The same error came.

 

Any help will be greatly appreciated.

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/209528-storing-files-in-blob/
Share on other sites

After a lot of searching and debugging and splitting hairs i found what was wrong.

 

It had nothing to do with all the code I pasted, nor anything to do with database.

 

I had written a class which i use to connect to the database.

I include the file, in which only one class is defined, throughout the project and call a static function defined inside it.

 

Unfortunately after the end of the php section i.e. after ?> I had pressed enter.

Basically a new line and the moment I removed that everything started working properly.

 

Totally weird and that is why I decided to post this so that some other poor bloke might not face the same problem.

 

 

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.