dsp77 Posted May 27, 2010 Share Posted May 27, 2010 i have stored in the db some images and when i try to call them for download i get corrupt file here is the code for display and download <?php // Connect to the database $dbLink = new mysqli('127.0.0.1', 'root', 'qazwsx123', 'diaspora_v2'); if(mysqli_connect_errno()) { die("MySQL connection failed: ". mysqli_connect_error()); } // Query for a list of all existing files $sql = 'SELECT `id`, `nume`, `prenume`, `name`, `mime`, `size`, `created` FROM `inregistrari`'; $result = $dbLink->query($sql); // Check if it was successfull if($result) { // Make sure there are some files in there if($result->num_rows == 0) { echo '<p>There are no files in the database</p>'; } else { // Print the top of a table echo '<table width="100%"> <tr> <td><b>Nume</b></td> <td><b>Prenume</b></td> <td><b>Name</b></td> <td><b>Mime</b></td> <td><b>Size (bytes)</b></td> <td><b>Created</b></td> <td><b> </b></td> </tr>'; // Print each file while($row = $result->fetch_assoc()) { echo " <tr> <td>{$row['nume']}</td> <td>{$row['prenume']}</td> <td>{$row['name']}</td> <td>{$row['mime']}</td> <td>{$row['size']}</td> <td>{$row['created']}</td> <td><a href='download.php?id={$row['id']}'>Download</a></td> </tr>"; } // Close table echo '</table>'; } // Free the result $result->free(); } else { echo 'Error! SQL query failed:'; echo "<pre>{$dbLink->error}</pre>"; } // Close the mysql connection $dbLink->close(); ?> <?php // Make sure an ID was passed if(isset($_GET['id'])) { // Get the ID $id = intval($_GET['id']); // Make sure the ID is in fact a valid ID if($id <= 0) { die('The ID is invalid!'); } else { // Connect to the database $dbLink = new mysqli('127.0.0.1', 'root', 'qazwsx123', 'diaspora_v2'); if(mysqli_connect_errno()) { die("MySQL connection failed: ". mysqli_connect_error()); } // Fetch the file information $query = " SELECT `mime`, `name`, `size`, `data` FROM `inregistrari` WHERE `id` = {$id}"; $result = $dbLink->query($query); if($result) { // Make sure the result is valid if($result->num_rows == 1) { // Get the row $row = mysqli_fetch_assoc($result); // Print headers header("Content-Type: ". $row['mime']); header("Content-Length: ". $row['size']); header("Content-Disposition: attachment; filename=". $row['name']); // Print data echo $row['data']; } else { echo 'Error! No image exists with that ID.'; } // Free the mysqli resources @mysqli_free_result($result); } else { echo "Error! Query failed: <pre>{$dbLink->error}</pre>"; } @mysqli_close($dbLink); } } else { echo 'Error! No ID was passed.'; } ?> i dont know why the headers are not correctly sent all files are JPG, or how can i display them not download. Quote Link to comment Share on other sites More sharing options...
Sergey Popov Posted May 27, 2010 Share Posted May 27, 2010 What is the type of field `data`? It must be blob or longblob... If all the images are Jpeg, try to set header explicitly: header("Content-type: image/jpeg"); Or, double check what is stored in the database field `mime`. Quote Link to comment Share on other sites More sharing options...
dsp77 Posted May 27, 2010 Author Share Posted May 27, 2010 the data field is mediumblob and the mime is image/jpeg i changed the data field in blob and longblob and i get the same thing Quote Link to comment Share on other sites More sharing options...
dsp77 Posted May 27, 2010 Author Share Posted May 27, 2010 strange thing i found...sometimes it parses download.php for download but only on text/plain file even if it has his mime type Quote Link to comment Share on other sites More sharing options...
mattal999 Posted May 27, 2010 Share Posted May 27, 2010 the data field is mediumblob and the mime is image/jpeg i changed the data field in blob and longblob and i get the same thing Well maybe the mediumblob cut off the end of the data (I think it can hold less data than the others), so even when you changed it you still only have the majority of the data. Try setting it to longblob, then re-adding the data. Quote Link to comment Share on other sites More sharing options...
dsp77 Posted May 28, 2010 Author Share Posted May 28, 2010 now i have longblob the only thing that is readable is .txt everything else i get error or corrupt. Quote Link to comment Share on other sites More sharing options...
dsp77 Posted May 28, 2010 Author Share Posted May 28, 2010 still not working i tried some other stuff with no luck. please help Quote Link to comment Share on other sites More sharing options...
mattal999 Posted May 28, 2010 Share Posted May 28, 2010 What is the value of $row['size']? I think it may be trying to send the wrong filesize for the data. Other than that I have no idea what is wrong... Quote Link to comment Share on other sites More sharing options...
dsp77 Posted May 31, 2010 Author Share Posted May 31, 2010 it has the value of the uploaded file the field is bigint(20) Attributes UNSIGNED Null YES Default 0 i have no idea why its not working maybe someone could help me build another way for display/download. Quote Link to comment Share on other sites More sharing options...
ashwood Posted May 31, 2010 Share Posted May 31, 2010 in your title i see "display" ok well im not exactly the best at php im less than amature but i have had to code the display part this is off the top of my head <?php mysql_connect("localhost","",""); mysql_select_db("database"); $query = mysql_query("SELECT * FROM yourtable"); while($row = mysql_fetch_assoc($query)) { $showimage = $row['image']; } echo "<img src='$showimage' height='50px' width='50px'>"; ?> Change $showimage = $row['image']; to $showimage = $row['WHATEVER YOUR IMAGE NAME IS']; Quote Link to comment Share on other sites More sharing options...
dsp77 Posted May 31, 2010 Author Share Posted May 31, 2010 tried this and still not working:( 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.