简体   繁体   中英

Forcing Download of file using php script on CentOs 5.6

I need to authenticate user to view/download some of the files in my site. My files are located in a folder say A inside public_html/mySiteName/FoldesName(A)/subFolder/sample.(*.*) . I used .htaccess to check that the user is authenticated or not.


MY .htaccess code :

RewriteEngine on   # Enable rewrite
RewriteCond %{REQUEST_FILENAME} \.*pdf$|.*psd$|.*indd$ [NC]  
RewriteRule (.*) http://MySiteIp/admin_panel/auth.php?file=$1 [NC,L]

My download script looks like :

if($authed){

    if(file_exists("../".$_GET['file'])) {
        //Create the pointer to our file and open it as read-only binary data
        $fp = fopen($file,'rb');

        // Send headers telling browser to download our passed data
        header("Content-Type: application/force-download");
        header("Pragma: no-cache");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Content-Length: " . filesize($file));
        header("Accept-Ranges: bytes");
        header("Content-Disposition: attachment; filename=\"$file\"");
        while (!feof($fp)) {
            echo(@fgets($fp, 8192));
        } 

        //Here comes the data 
        fclose($fp);
        //and quit
        exit;
    } else {
        echo "No file exists !!!";
    }
}

When I test it on my local machine with Xampp Server installed on it. It works. But on my host I get the error message "NO file exists".

Please help me to find where I am wrong...

I'm going to guess you removed the echo "here"; die(); echo "here"; die(); before asking this question.

Anywho, is the file structure the same on your local machine and the web host?

Is the file actually in ../? Maybe try using the full path to the file? /home/youruser/public_html/ or whatever the host's directory structure is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM