uladk Posted October 9, 2007 Share Posted October 9, 2007 Hello everyone, I found a tutorial online (http://www.php-mysql-tutorial.com/php-mysql-upload.php) for uploading files into mysql and then downloading them again but only certain files are being downloaded properly. Images and pdf's refuse to open once downloaded but I have had success with text files and partial success with word files (word had to recover it). Are there any errors or missing code to process files properly? Thanks Database table: CREATE TABLE upload ( id INT NOT NULL AUTO_INCREMENT, name VARCHAR(30) NOT NULL, type VARCHAR(30) NOT NULL, size INT NOT NULL, content MEDIUMBLOB NOT NULL, PRIMARY KEY(id) ); The upload.php: <?php //MY CONNECTION INFO $action = $_REQUEST['action']; switch ($action) { case 'update' : $fileName = $_FILES['userfile']['name']; $tmpName = $_FILES['userfile']['tmp_name']; $fileSize = $_FILES['userfile']['size']; $fileType = $_FILES['userfile']['type']; $fp = fopen($tmpName, 'r'); $content = fread($fp, $fileSize); $content = addslashes($content); fclose($fp); if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); } if(isset($_REQUEST['upload'])) { $query = "INSERT INTO upload (name, size, type, content ) "."VALUES ('$fileName', '$fileSize', '$fileType', '$content')"; mysql_query($query) or die('Error, query failed'); echo "<br>File $fileName uploaded<br>"; header("location:uploadform.php"); } if(isset($_REQUEST['cancel'])) { header("location:uploadform.php"); } break; } ?> <form action="?action=update" method="post" enctype="multipart/form-data" name="uploadform"> <input name="userfile" type="file" class="box" id="userfile"> <input name="upload" type="submit" class="box" id="upload" value="Upload"> </form> The download.php: <?php //MY CONNECTION INFO if(isset($_GET['id'])) { $id = $_GET['id']; $query = "SELECT name, type, size, content FROM upload WHERE id = '$id'"; $result = mysql_query($query) or die('Error, query failed'); list($name, $type, $size, $content) = mysql_fetch_array($result); header("Content-Disposition: attachment; filename=$name"); header("Content-length: $size"); header("Content-type: $type"); echo $content; exit; } ?> <html><body> <?php $query = "SELECT id, name FROM upload"; $result = mysql_query($query) or die('Error, query failed'); if(mysql_num_rows($result) == 0) { echo "Database is empty <br>"; } else { while(list($id, $name) = mysql_fetch_array($result)) { echo "<a href='download.php?id=$id'>$name</a> <br>"; } } ?> </body></html> Quote Link to comment https://forums.phpfreaks.com/topic/72419-solved-only-partial-success-with-mysql-file-uploaddownload/ Share on other sites More sharing options...
uladk Posted October 9, 2007 Author Share Posted October 9, 2007 Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/72419-solved-only-partial-success-with-mysql-file-uploaddownload/#findComment-365740 Share on other sites More sharing options...
MadTechie Posted October 9, 2007 Share Posted October 9, 2007 try changing header("Content-type: $type"); echo $content; exit; to header("Content-type: $type"); echo stripslashes($content); exit; Quote Link to comment https://forums.phpfreaks.com/topic/72419-solved-only-partial-success-with-mysql-file-uploaddownload/#findComment-365744 Share on other sites More sharing options...
uladk Posted October 10, 2007 Author Share Posted October 10, 2007 Figured it out! Stripslashes didn't make a difference but thank you for your help. The problem was I had an echo after my database connection (to verify I was connected): $DB_LINK = @mysql_connect($db_host, $db_user, $db_pass) or die("error connecting"); echo "<h2>Connection Successful</h2>"; That echoed line was joining on to any file data being retrieved from the database. I noticed that after uploading/downloading a CSS file, Dreamweaver picked up the <h2>... etc. Removed the echo and all is well. Quote Link to comment https://forums.phpfreaks.com/topic/72419-solved-only-partial-success-with-mysql-file-uploaddownload/#findComment-365933 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.