Jump to content

Problems inserting and retireving documents stored in MySQL DB


oliflorence

Recommended Posts

Hello,

First post here!

 

I am using a script to store small files into a database. I know it is not highly recommended but this is the only solution I can find in this case.

 

All seems fine when inserting but when retrieving all documents are corrupt.

 

I am working with PHP 5 on Windows server.

 

Part script after upload: (the document is uploaded to the server first and this is working fine)



if (!empty($_FILES)) {

$folder = "kenya/docs_temp/";//including last /
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = dirname(__FILE__);
$targetPath = substr($targetPath,0,strrpos($targetPath, "uploadify")). $folder;


$targetFile =  str_replace('\\','/',$targetPath) . $_FILES['Filedata']['name'];


$fileName = $_FILES['Filedata']['name'];
    $tmpName  = $_FILES['Filedata']['tmp_name'];
    $fileSize = $_FILES['Filedata']['size'];
    $fileType = $_FILES['Filedata']['type'];





$conn=odbc_connect('xxx','xxx','xxxx');


   $content = file_get_contents($tmpName,true);
    $content = addslashes($content);


if(!get_magic_quotes_gpc()){
        $fileName = mysql_real_escape_string($fileName);
     }
                   
    $file_info = pathinfo($_FILES['Filedata']['name']); 



$addDoc = "INSERT INTO tblfiles (file_name,file_type,file_size,file_content,file_extension) VALUES ('".addslashes($fileName)."','".$fileType."','".$fileSize."','".$content."','".$file_info['extension']."')";
odbc_exec($conn,$addDoc);


}

 

The the script to retrieve and output to the browser:

 


$conn=odbc_connect('xxxx','xxx','xxxx');



	//get the document
	$getF = "SELECT * FROM tblfiles WHERE userId = '".$_GET['userId']."' AND  fileId = '".$_GET['fileId']."'";
	$rsF = odbc_exec($conn,$getF);


		$size = floor(odbc_result($rsF,'file_size'));
$type = odbc_result($rsF,'file_type');
$name = odbc_result($rsF,'file_name');
$content = odbc_result($rsF,'file_content');


ob_end_clean();
header("Content-length: ".$size."");
header("Content-type: ".$type."");
header('Content-Disposition: attachment; filename="'.$name.'"');
echo $content;




 

And finally here is the structure for the table:

 

CREATE TABLE `tblfiles` (
  `fileId` int(11) NOT NULL auto_increment,
  `userId` int(11) NOT NULL,
  `file_name` varchar(100) NOT NULL,
  `file_type` varchar(100) default NULL,
  `file_size` int(11) default NULL,
  `file_content` mediumblob,
  `file_extension` varchar(50) NOT NULL,
  PRIMARY KEY  (`fileId`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

 

many thanks for any assistance.

 

Of course there could be a problem there and probably is.  ODBC is a lowest common denominator technology.  It was designed so people with excel could suck out some data from some funky datasource into their spreadsheet.  It is slow and lacks functionality by definition. 

 

Figure out what your issue is with the native mysql driver.  I'm sure I don't have to tell you that native db drivers are always the way to go especially when you're trying to store blobs or do anything else that is highly database specific.  There's probably millions of websites running php/mysql and not a one uses odbc. 

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.