tags:

views:

79

answers:

8

Hi everybody

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 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.';
   }
   ?>
+1  A: 

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

Michael Borgwardt
+2  A: 

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");
naivists
A: 

Did you try open files with Notepad? It happened to me, that instead content all that been saved were warnings and errors.

Dolfa
A: 

HI I KNOW I should not write these words here these should be in cooments.BUt i am not sure whether if i add any comment my question will be updated or not.Plz also tell me about this.I will nor rewind this type of mistake in assure

A: 

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

TRiG
where i have given spaces will u plz edit the code or tell writing that particular line where i am getting wrongi have not used spaces uin [].
I'm just saying that you should be careful sending back user-generated content (such as the filename) without sanitising it first. $row['name'] could be anything, couldn't it? So make sure it's US-ASCII with no spaces.
TRiG
A: 

add this to your headers

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

this might solve your problem.

Ashish Rajan
Sometimes works, but is non-standard. The proper way to force a download is the Content-Disposition header.
TRiG
A: 

i have tried these all but nothing is working for me....

header("Content-Type: ".$row['mime']);
header("Content-Disposition: attachment;filename=".$row['name']);
header("Content-Length: ".$row['size']);
header("Content-type: application/octet-stream");


id  name  mime  size  data  created
1  Oryx Antelope.jpg  image/jpeg  297834  [BLOB - 21B] 2010-01-24 17:15:55
2  Image0138.jpg  image/jpeg  156368  [BLOB - 21B] 2010-01-25 09:54:29
3  Image0140.jpg  image/jpeg  158139  [BLOB - 21B] 2010-01-25 12:37:15
4  Image0140.jpg  image/jpeg  158139  [BLOB - 21B] 2010-01-25 12:37:54
5  GoogleDesktop.exe  application/octet-stream  30192  [BLOB - 21B] 2010-01-26 10:07:18

this is my database record of file my table name is file and database name is tcs..

Deepak Narwal
any help for this
Deepak Narwal
You have two Content-Type headers overwriting each other. application/octet-stream is for when you don't know the MIME type. It's sometimes used to force a download, but that's not the correct use. To force a download, we use the Content-Disposition header. Drop the octet-stream stuff.
TRiG
i also tried by droping this header but still problem is as it is.yar if u want i can post my files here but i dont know what i sgetting wrong
Deepak Narwal
A: 

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.

TRiG