SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Member natalalaa's Avatar
    Join Date
    May 2007
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy file download problem using readfile()

    Hi,

    I'm having problems with downloading files from a server.
    The .txt files are being downloaded correctly, but types like .doc or .jpg are not being displayed correctly, but appear as a page of symbols, e.g.

    ╨╧ рб▒ с > ■  Q S ■    P                                                                                                                                                                                                                                                                                                                                                                                                                                                 ье┴ q` ° ┐ М

    My code:

    $downloaddir = '/Snap429640/captprimary/hostingSlowRep/admin/support/uploads/';
    $fname = urldecode($fname);
    $downloadfile = $downloaddir . $fname;

    $size = filesize($downloadfile);
    $type = filetype($downloadfile);

    header("Content-Disposition: attachment; filename=$fname");
    header("Content-Length: $size");
    header("Content-Type: $type");
    header("Content-Transfer-Encoding: binary");

    readfile($downloadfile);

    Could anyone help?

    Thanks,
    Natalia

  2. #2
    SitePoint Evangelist hexburner's Avatar
    Join Date
    Jan 2007
    Location
    Belgium
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or you use application/octet-stream, or you leave the type empty.
    If the content-type is empty, the browsers tries to identify the mimetype.

    Code PHP:
    <?
        $downloaddir = '/Snap429640/captprimary/hostingSlowRep/admin/support/uploads/';
        $fname = urldecode($fname);
        $downloadfile = $downloaddir . $fname;
     
        $size = filesize($downloadfile);
        $type = "application/octet-stream"; // For direct download
        $type = "" // Empty for use in browser, eg an image
     
        header("Content-Disposition: attachment; filename=$fname");
        header("Content-Length: $size");
        header("Content-Type: $type");
        header("Content-Transfer-Encoding: binary");
     
        echo readfile($downloadfile);
    ?>
    FOR SALE: 1 set of morals, never used, will sell cheap

  3. #3
    SitePoint Evangelist barbara1712's Avatar
    Join Date
    Apr 2007
    Location
    India
    Posts
    508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this.
    Hope it will help.

    PHP Code:

    $downloaddir 
    '/Snap429640/captprimary/hostingSlowRep/admin/support/uploads/';
        
    $fname urldecode($fname);
        
    $downloadfile $downloaddir $fname;
     
        
    $size filesize($downloadfile);
        
    $type "application/octet-stream"// For direct download
        
     
        
    header("Cache-Control: no-store, no-cache, must-revalidate");
    header("Cache-Control: post-check=0, pre-check=0"false);
    header("Cache-Control: private",false);
    header("Pragma: no-cache");
    header('Content-Type:'$type);
    header ("Content-Disposition: attachment; filename=\""basename($downloadfile)."\";");
    header("Content-Transfer-Encoding: binary\n");
    header ("Content-Length: " filesize($downloadfile));
     
        echo 
    readfile("$downloadfile"); 
    Barbara

  4. #4
    SitePoint Member natalalaa's Avatar
    Join Date
    May 2007
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Still doesn't work.

    I'm getting the pop up window prompting me to choose the program for opening the file OK. It showes the correct file format, but still doesn't want to display the file

  5. #5
    SitePoint Evangelist barbara1712's Avatar
    Join Date
    Apr 2007
    Location
    India
    Posts
    508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok.Try for your $type variable

    PHP Code:
    $file_extension strtolower(substr(strrchr($downloadfile,"."),1));
    switch( 
    $file_extension )
    {
        case 
    "pdf"$type="application/pdf"; break;
        case 
    "zip"$type="application/zip"; break;
        case 
    "doc"$type="application/msword"; break;
        case 
    "xls"$type="application/vnd.ms-excel"; break;
        case 
    "ppt"$type="application/vnd.ms-powerpoint"; break;
        case 
    "gif"$type="image/gif"; break;
        case 
    "png"$type="image/png"; break;
        case 
    "jpg"$type="image/jpg"; break;
        case 
    "mp3"$type="application/iTunes"; break;
        default: 
    $type="application/force-download";

    Barbara

  6. #6
    SitePoint Member natalalaa's Avatar
    Join Date
    May 2007
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It looks like the problem is not in the $type, but in the way the file is being downloaded from the server. The type is being recognised correctly (I can see it on the pop up screen) and the program to open it if being suggested correcty by windows, but what's in the file is somehow corrupted. I checked if files on the server and they are fine there. Any ideas?

  7. #7
    SitePoint Member
    Join Date
    May 2007
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have similar problem with readfile function.
    I tried to show image with simple code:
    <?php
    $myfile = '2.gif';
    header("Content-Type: image/gif");
    header("Content-Length: " . filesize($myfile));
    readfile($myfile);
    ?>
    Instead of the image I received string:
    GIF89ab
    Then I tried to see what data is actually read, with such code:

    $s = file_get_contents($myfile) ;
    echo filesize($myfile)." ".strlen($s);
    echo ($s);

    I received this:
    39509 39509
    The file size and length of $s are equal.
    I tried to display data in $s. But I still receive only GIF89ab string.
    So the problem somewhere in the way the data is sent to the browser.
    I am not good at PHP. Any ideas?

  8. #8
    SitePoint Member natalalaa's Avatar
    Join Date
    May 2007
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I found a simple solution for that: instead of read file I just made a link <a href="myfilepath">open the file<a> on the site, that leads to the correct file on the server. Works fine. If the file if gif or jpg it simply opens in a new window; if it's doc or xls or anything that can't be open in a browser I'm getting a prompt asking what program I would like to usu to open the file.

    Hope it helps.
    Natalia

  9. #9
    SitePoint Evangelist barbara1712's Avatar
    Join Date
    Apr 2007
    Location
    India
    Posts
    508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah,

    But,I think it not secure.
    i.e. anyone can download the file by typing the url in browser.

    So writing a php script to download the file is a good option.
    Barbara

  10. #10
    SitePoint Member
    Join Date
    May 2007
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Unfortunaly this solution is not applicable in my case.
    I have problem with code that calls my function.
    <img src="download.php"/>
    Where download.php is my script.
    And the purpose of my function is to display image.
    Any ideas?

  11. #11
    SitePoint Member natalalaa's Avatar
    Join Date
    May 2007
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try to put fopen('your full file path'); in your script.

  12. #12
    SitePoint Member maksimovic's Avatar
    Join Date
    Jul 2006
    Location
    Novi Sad, Serbia
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This one works like a dream

    PHP Code:
    // give file to download
    download("/path/to/your/file.zip");


    /**
         * Download file
         *
         * @param string $path
         * @param string $type
         * @param string $name
         * @param bool $force_download
         * @return bool
         */
        
    function download($path$name ''$type 'application/octet-stream'$force_download true) {

            if (!
    is_file($path) || connection_status() !== 0);

            if(
    $force_download) {
                
    header("Cache-Control: public");
            } else {
                
    header("Cache-Control: no-store, no-cache, must-revalidate");
                
    header("Cache-Control: post-check=0, pre-check=0"false);
                
    header("Pragma: no-cache");
            }

            
    header("Expires: ".gmdate("D, d M Y H:i:s"mktime(date("H")+2date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT");
            
    header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
            
    header("Content-Type: $type");
            
    header("Content-Length: ".(string)(filesize($path)));

            
    $disposition $force_download 'attachment' 'inline';

            if(
    trim($name) == '') {
                
    header("Content-Disposition: $disposition; filename=" basename($path));
            } else {
                
    header("Content-Disposition: $disposition; filename=\"" trim($name)."\"");
            }

            
    header("Content-Transfer-Encoding: binary\n");

            if (
    $file fopen($path'rb')) {
                while(!
    feof($file) and (connection_status()==0)) {
                    print(
    fread($file1024*8));
                    
    flush();
                }
                
    fclose($file);
            }

            return((
    connection_status() == 0) && !connection_aborted());
        } 
    First parameter is required, $name is automatically detected from $path (if you want to use the same name, otherwise you can override it with custom one), $force_download - pretty straightforward what it means, as well as $type


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •