sandbudd Posted June 21, 2008 Share Posted June 21, 2008 I have a php mysql that uploads a file to the database. It uploads great and displays the link but when clicked on it is garbaged. Here is the link so you can see what I am getting. http://www.sandbudd.com/uploads/download.php and here are the codes. upload.php <?php if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0 ) { $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($tmpName)); $content = addslashes($content); fclose($fp); if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); } include 'config.php'; include 'opendb.php'; $query = "INSERT INTO upload (name, size, type, content ) ". "VALUES ('$fileName', '$fileSize', '$fileType', '$content')"; mysql_query($query) or die('Error, query failed'); include 'library/closedb.php'; echo "<br>File $fileName uploaded<br>"; } ?> <form method="post" enctype="multipart/form-data"> <table width="350" border="0" cellpadding="1" cellspaci ng="1" class="box"> <tr> <td width="246"> <input type="hidden" name="MAX_FILE_SIZE" value="2000000"> <input name="userfile" type="file" id="userfile"> </td> <td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td> </tr> </table> </form> download.php <? if(isset($_GET['id'])) { include 'config.php'; include 'opendb.php'; $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; include 'library/closedb.php'; exit; } ?> <html> <head> <title>Download File From MySQL</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <? include 'config.php'; include 'opendb.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)) { ?> <a href="download.php?id=<?=$id;?>"><?=$name;?></a> <br> <? } } include 'library/closedb.php'; ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 21, 2008 Share Posted June 21, 2008 You aren't sending the correct headers. Go to your database and make sure everything is being inserted properly (the type, especially). Quote Link to comment Share on other sites More sharing options...
sandbudd Posted June 21, 2008 Author Share Posted June 21, 2008 DarkWater I am sorry I am new at php mysql. Here is a copy of the database as close as I can copy and past it. row(s) starting from record # in mode and repeat headers after cells Sort by key: Full Texts id name type size content Edit Delete 2 Hawaii2008 002.jpg image/pjpeg 236490 [bLOB - 230.9 KB] Edit Delete 3 blue_box_03.gif image/gif 157 [bLOB - 157 Bytes] Edit Delete 4 author.jpg image/jpeg 20379 [bLOB - 19.9 KB] Edit Delete 5 color_fade.jpg image/jpeg 13063 [bLOB - 12.8 KB] Edit Delete 6 killawatt.pdf application/pdf 696243 [bLOB - 679.9 KB] Edit Delete 7 Hawaii2008 039.jpg image/jpeg 1315558 [bLOB - 1.3 MB] With selected: Check All / Uncheck All With selected: Change Delete Export row(s) starting from record # in mode and repeat headers after cells Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 21, 2008 Share Posted June 21, 2008 On my personal file-download script, I sent the Content-type header FIRST, then the Content-Disposition and Content-Length headers. Try changing the order. D: Quote Link to comment Share on other sites More sharing options...
sandbudd Posted June 21, 2008 Author Share Posted June 21, 2008 I am sorry but the same results. I changed to this <?php if(isset($_GET['id'])) { include 'config.php'; include 'opendb.php'; $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-type: $type"); header("Content-Disposition: attachment; filename=$name"); header("Content-length: $size"); echo $content; include 'library/closedb.php'; exit; } ?> <html> <head> <title>Download File From MySQL</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <? include 'config.php'; include 'opendb.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)) { ?> <a href="download.php?id=<?=$id;?>"><?=$name;?></a> <br> <? } } include 'library/closedb.php'; ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 21, 2008 Share Posted June 21, 2008 I noticed that you used addslashes on the content before inserting it, but you don't stripslashes when removing it. Quote Link to comment Share on other sites More sharing options...
sandbudd Posted June 21, 2008 Author Share Posted June 21, 2008 DarkWater thanks for the input but where do I put the stripslashes? Could you please give me an example and I am sorry for being such a novice. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 21, 2008 Share Posted June 21, 2008 stripslashes($content); Add it before you send the headers but after you get $content from the db. Quote Link to comment Share on other sites More sharing options...
Wolphie Posted June 21, 2008 Share Posted June 21, 2008 $content = stripslashes($content); $name = stripslashes($name); Edit: Dark got there before me. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 21, 2008 Share Posted June 21, 2008 Heh, yeah. =P But I forgot to tell him about stripping the slashes from $name. Do that too. Quote Link to comment Share on other sites More sharing options...
Wolphie Posted June 21, 2008 Share Posted June 21, 2008 Just out of curiosity, would it be possible to just stripslashes inside the list construct? list(stripslashes($name)) = $othernames; Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 21, 2008 Share Posted June 21, 2008 I don't think so. Quote Link to comment Share on other sites More sharing options...
sandbudd Posted June 21, 2008 Author Share Posted June 21, 2008 still not working here is what I did and thanks for all the help <?php if(isset($_GET['id'])) { include 'config.php'; include 'opendb.php'; $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); $content = stripslashes($content); $name = stripslashes($name); header("Content-type: $type"); header("Content-Disposition: attachment; filename=$name"); header("Content-length: $size"); echo $content; include 'library/closedb.php'; exit; } ?> <html> <head> <title>Download File From MySQL</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <? include 'config.php'; include 'opendb.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)) { ?> <a href="download.php?id=<?=$id;?>"><?=$name;?></a> <br> <? } } include 'library/closedb.php'; ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 21, 2008 Share Posted June 21, 2008 I'm not getting your headers at all: http://www.sandbudd.com/uploads/download.php?id=2 GET /uploads/download.php?id=2 HTTP/1.1 Host: www.sandbudd.com User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9b5) Gecko/2008050509 Firefox/3.0b5 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip,deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive: 300 Connection: keep-alive Cache-Control: max-age=0 HTTP/1.x 200 OK Server: Microsoft-IIS/5.0 Date: Sat, 21 Jun 2008 16:28:34 GMT X-Powered-By: ASP.NET, PHP/4.4.7 Connection: close Content-Type: text/html ---------------------------------------------------------- =/ Strange. Quote Link to comment Share on other sites More sharing options...
sandbudd Posted June 21, 2008 Author Share Posted June 21, 2008 DarkWater what does that mean please? and how would I correct it? Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 21, 2008 Share Posted June 21, 2008 I'm going to make sure your image data is correct. Do me a favor. Change that stripslashes line to: $content = base64_encode(stripslashes($content)); Then I'm going to write up a quick script on my box to verify the file is good. Okay? Quote Link to comment Share on other sites More sharing options...
sandbudd Posted June 21, 2008 Author Share Posted June 21, 2008 Here is what I have done <?php if(isset($_GET['id'])) { include 'config.php'; include 'opendb.php'; $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); $content = base64_encode(stripslashes($content)); $name = stripslashes($name); header("Content-type: $type"); header("Content-Disposition: attachment; filename=$name"); header("Content-length: $size"); echo $content; include 'library/closedb.php'; exit; } ?> <html> <head> <title>Download File From MySQL</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <? include 'config.php'; include 'opendb.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)) { ?> <a href="download.php?id=<?=$id;?>"><?=$name;?></a> <br> <? } } include 'library/closedb.php'; ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 21, 2008 Share Posted June 21, 2008 Okay, put it back to the way you had it before. Something seems to be going wrong with uploading it. The image isn't even putting itself together on my box when I save it. I think you should just upload the image and store a path name in the db. By the way, you have the field as a BLOB in the database, right? Quote Link to comment Share on other sites More sharing options...
sandbudd Posted June 21, 2008 Author Share Posted June 21, 2008 I do have it as a blob and how do I store a path name? here is my database structure Field Type Collation Attributes Null Default Extra Action id int(11) No auto_increment Browse distinct values Change Drop Primary Unique Index Fulltext name varchar(30) latin1_swedish_ci No Browse distinct values Change Drop Primary Unique Index Fulltext type varchar(30) latin1_swedish_ci No Browse distinct values Change Drop Primary Unique Index Fulltext size int(11) No 0 Browse distinct values Change Drop Primary Unique Index Fulltext content mediumblob BINARY No Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 21, 2008 Share Posted June 21, 2008 Don't store the actual image in the database, just SAVE the image and store the path to it in the database instead of the entire image. Then use readfile() to echo it to the browser. Quote Link to comment Share on other sites More sharing options...
sandbudd Posted June 21, 2008 Author Share Posted June 21, 2008 DarkWater thank you so much for all your help but I am sorry but I do not know how to do what you are saying. If you have a script that would work that you would not mind letting go of that would be a blessing. Thank You Quote Link to comment Share on other sites More sharing options...
sandbudd Posted June 23, 2008 Author Share Posted June 23, 2008 If anyone can figure this out it would be greatly appreciated 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.