0

I have MySQL table with LONGBLOB column to save file (although I know saving files directly in database is not a good practice.) Saving works fine! concluding (C:\ProgramData\MySQL\MySQL Server 5.7\Data\dogsport) size of dogsport has increased to the new file saved.

I have written download script part. File downloads to the computer fine (not displaying image in the webpage).

Problem: Downloaded file shows only 4 bytes although it should be 4.83 MB. When I open the image, image is not shown also (see screenshot)

enter image description here

What is the reason?

Updated code for @Andrew:

            $query = "SELECT Id,Name,Type,SizeMB FROM Upload"; // Good practice. Do not process much data here. leave Content field
        $result = mysqli_query ( $con, $query) or die ("Couldn't execute SELECT query: ". mysqli_error($con));
        $nrows = mysqli_num_rows($result);
        echo '<table>
                    <tr>
                        <td>Id</td>
                        <td>Name</td>
                        <td>Type</td>
                        <td>Size (MB)</td>
                    </tr>';
        $selfpg = $_SERVER['PHP_SELF'];
        while( $row = mysqli_fetch_assoc($result) ){
            extract($row);
            echo "<tr>
                        <td>$Id</td>
                        <td>$Name</td>
                        <td>$Type</td>
                        <td>$SizeMB</td>
                        <td> <a href='$selfpg?id=$Id'> Download </a> </td>
                  </tr>";       
        }
        echo '</table>';

        //----------------------------------------------------------------------
        if ( isset($_GET['id']) ){

            $result_id = "SELECT Content FROM Upload WHERE Id=". $_GET['id'];

            $row = mysqli_fetch_assoc($result_id);
            extract($row);
            $size_bytes = $SizeMB * 1024 * 1024;
            header("Content-Type: ". $Type);
            header("Content-Length: ". $size_bytes);
            header("Content-Disposition: attachment; filename= ". $Name);

            echo $Content;
        }
        else{
            echo "<br>No id received to download a file</br>";
        }

Still there is the problem

UPDATE !!! - Please download my code with SQL

http://s000.tinyupload.com/index.php?file_id=88155436688298142745

Amali
  • 63
  • 1
  • 8

1 Answers1

1

Your problem can be caused by this

header("Content-Length: ". $SizeMB);

Content-Length header must contain size in bytes, not megabytes.

Also, SELECT * FROM Upload - this is bad practice. Your query will return all fields from table, including longblob column. You don't need to process so much data. In script you are using only Id, Name, Type and SizeMB, so select only those fields. And instead

mysqli_data_seek($result,0);

you can perform second query:

SELECT Content FROM Upload WHERE Id = $Id
Andrew
  • 1,858
  • 13
  • 15
  • I made changes. thanks great best practice. But still the same problem. :( – Amali Apr 23 '16 at 08:48
  • SizeMB field in the table, I am saving records after converting to MB from bytes using $fsize = ($_FILES['photoUpload']['size']/1024)/1024; then I this it is rounded and saved because my SizeMB field is SizeMB DECIMAL(9,2) NOT NULL ... Is there any impact of this? – Amali Apr 23 '16 at 08:54
  • Content-length is optional. Try dropping it, see if that helps? – Rob G Apr 23 '16 at 11:01
  • @Rob Gudgeon still the same problem exist. :( – Amali Apr 23 '16 at 11:12
  • Can you provide some more info? Maybe download your file [with curl](http://stackoverflow.com/a/3121175/6209665) and show here verbose response? With all request/response headers as described in answer by link. – Andrew Apr 23 '16 at 13:25
  • Ohh completely new topic for me CURL. I have to study it first. Also I have no web hosting. I can attach the file to my post. Then you can easily do a check. – Amali Apr 24 '16 at 04:12
  • @Andrew I have uploaded my code with SQL script. please check it. Thanks. – Amali Apr 24 '16 at 04:17
  • Just use Firefox or chrome, open the developer console, inspect network traffic & post the response headers & the actual response contents. No need for curl – Rob G Apr 24 '16 at 17:27