phpreadfilesafe-modephp-safe-mode

Is there an alternative to php readfile() in safe mode server?


I host my site on a shared hosting, which lately changed the server to safe mode (without even notifying that). I use a feature that download files from the server, using the readfile() function (I use php). Now, in safe_mode, this function is no longer available. Is there a replacement or a workaround to handle the situation that the file will be able to be downloaded by the user?

Thanks


Solution

  • As I wrote in comments, readfile() is disabled by including it in disable_functions php.ini directive. It has nothing to do with safe mode. Try checking which functions are disabled and see if you can use any other filesystem function(-s) to do what readfile() does.

    To see the list of disabled functions, use:

    var_dump(ini_get('disable_functions'));
    

    You might use:

    // for any file
    $file = fopen($filename, 'rb');
    if ( $file !== false ) {
        fpassthru($file);
        fclose($file);
    }
    
    // for any file, if fpassthru() is disabled
    $file = fopen($filename, 'rb');
    if ( $file !== false ) {
        while ( !feof($file) ) {
            echo fread($file, 4096);
        }
        fclose($file);
    }
    
    // for small files;
    // this should not be used for large files, as it loads whole file into memory
    $data = file_get_contents($filename);
    if ( $data !== false ) {
        echo $data;
    }
    
    // if and only if everything else fails, there is a *very dirty* alternative;
    // this is *dirty* mainly because it "explodes" data into "lines" as if it was
    // textual data
    $data = file($filename);
    if ( $data !== false ) {
        echo implode('', $data);
    }