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
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.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.