Page 1 of 1

Problems with "readfile();"

Posted: Thu Dec 18, 2008 2:45 am
by semas
Hello everyone, i have some problems with readfile(); it worked before i reinstalled windows and now... it dosent.

Here's the code:

Code: Select all

<?php
include("config.php"); //there is connection to mysql server and so on...
 
$query = mysql_query("SELECT * FROM files WHERE file_code=\"{$_GET['file']}\""); //selects a file from mysql server with hash that is in url
$result = mysql_fetch_array($query); //puts everything in array
 
/* im not sure but i think there is one more problem with headers */
if($result['file_type'] == "Image"){
    $header = "Content-type: image/jpg";
    $dir = "images";
    $dl_header = "Content-Disposition: attachment; filename=\"$site_linkimages/{$result['file_name_full']}\"";
}else if($result['file_type'] == "Flash"){
    $header = "Content-type: application/x-shockwave-flash";
    $dir = "flash";
    $dl_header = "Content-Disposition: attachment; filename=\"$site_linkflash/{$result['file_name_full']}\"";
}else if($result['file_type'] == "Games"){
    $header = "Content-type: application/x-shockwave-flash";
    $dir = "games";
    $dl_header = "Content-Disposition: attachment; filename=\"$site_linkgames/{$result['file_name_full']}\"";
}else if($result['file_type'] == "Music"){
    $header = "Content-type: audio/mpeg";
    $dir = "music";
    $dl_header = "Content-Disposition: attachment; filename=\"$site_linkmusic/{$result['file_name_full']}\"";
}
 
if($_GET['type'] == "download"){
    if(mysql_num_rows($query) > 0){
        /* i can download the file */
        header($header);
        header($dl_header);    
        header("Content-Type: application/download");
        header("Content-Length: ".filesize("$dir/{$result['file_name_full']}"));
        readfile("$dir/{$result['file_name_full']}");
    }
}else if($_GET['type'] == ""){
    if(mysql_num_rows($query) > 0){
        /* download works but readinng and displayng in site dosent work, i have no idea why. */
        header($header);
        readfile("$dir/{$result['file_name_full']}");
    }else {
        header("Location: $site_link");
    }
}
?>
P.S. When i open files direcly trouth browser i can see them, but when using files.php and this code it does not work

examples:
http://78.63.197.71/?page=Files&type=Im ... b3b238336b
http://78.63.197.71/images/funny-hellos-172.jpg

Re: Problems with "readfile();"

Posted: Sat Dec 20, 2008 12:29 pm
by GreenCore
I see that you give every image a header like:

Code: Select all

header “Content-type: image/[b][color=#FF0000]jpg[/color][/b]”;
the default header for JPG/JPEG is :

Code: Select all

header('Content-Type: image/[b][color=#00BF40]jpeg[/color][/b]');
also not every image is an image/jpeg for example the png header is like:

Code: Select all

header('Content-Type: image/png');
Good Luck!