开发者

Downloaded file not opening in correct format

I have two php files

1. List_files ->show all uploaded files

2. Get_file ->to download files from listed files...

Now when i download any image or any text document and try to open it it is not getting opened,

Error is format not supported everytime.

My image is .jpeg

I am using Windows Vista with Firefox.

No problem in downloading, problem is in opening the file..

get_file.php

<?php
                                                                        // Make sure an ID was passed
    if(isset($_GET['id'])) 
    {
                                                                      // Get the 开发者_Python百科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
           $con=mysql_connect("localhost","root","");

                if(!$con)
                {
                    die('Could Not Connect:'.mysql_error());
                }    

                mysql_select_db("tcs",$con);

                                                                                                        // Fetch the file information
           $query = "SELECT `mime`, `name`, `size`, `data` FROM `file` WHERE `id` = {$id}";
           $result=mysql_query($query,$con);

           if($result) 
           {

                $count = mysql_num_rows($result);                                                           // Make sure the result is valid
                if($count == 1) 
                {
                                                                                        // Get the row
                   $row = mysql_fetch_array($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
               //mysql_free_result($result);
           }
           else 
           {
               echo "Error! Query failed: <pre>".mysql_error()."</pre>";
           }
           mysql_close();
       }
   }
   else 
   {
       echo 'Error! No ID was passed.';
   }
   ?>


You have posted the wrong file in your question, this is a list of files, but the problem most likely is in the other file, the get_files.php.

Most likely, you are not sending the "content-type" header. If you say it is an JPEG image, then it should read as

header("Content-Type: image/jpeg");

If you want it as a download (and not opening it in browser), you could add one more header

header("Content-Disposition: attachment;filename=your_current_filename.jpg");


Your problem is in get_files.php. Most likely it does not set a correct content-type header.


I'm guessing the problem is in this: header("Content-Disposition: attachment; filename=".$row['name']); Filenames are restricted to US-ASCII and should not contain spaces. So you might need to do some cleanup on the filename. (Some people do use spaces, and enclose the filename in quotation marks. This works, but it's non-standard.)

http://www.ietf.org/rfc/rfc2183.txt


add this to your headers

header("Content-type: application/octet-stream");

this might solve your problem.


Okay. We need to do some debugging. Use a tool which shows you the actual PHP headers being sent. (I like Web-Sniffer.net. There are also some Firefox plugins which do this.)

Remove the Content-Length header: it's nice to have for progressive download bars, but it's unnecessary; and if it's wrong it could be buggering things up.

Remove the Content-Disposition header. See if the right file is being served at all.

Basically, check each small part of the code individually, before stitching it all together. That way, you have some idea where the bugs are.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜