oliflorence Posted May 25, 2011 Share Posted May 25, 2011 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. Quote Link to comment Share on other sites More sharing options...
gizmola Posted May 25, 2011 Share Posted May 25, 2011 You have a mysql table... why in gods name are you trying to use odbc? mysql Quote Link to comment Share on other sites More sharing options...
oliflorence Posted May 25, 2011 Author Share Posted May 25, 2011 Failry new to PHP, migrating from ASP. I recently had a problem with a straight connection and turned to ODBC which worked, can't imagine this would be the problem there?? Quote Link to comment Share on other sites More sharing options...
gizmola Posted May 25, 2011 Share Posted May 25, 2011 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. Quote Link to comment Share on other sites More sharing options...
oliflorence Posted May 25, 2011 Author Share Posted May 25, 2011 Thank you. I am connecting to MySQL directly and it has sorted the problem. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.