Quantcast
Channel: Nginx Forum - How to...
Viewing all articles
Browse latest Browse all 4759

X-Accel-Redirect & Sendfile conf help

$
0
0
Hello,

Recently i make some website for download file ,but got some problem , maybe someone can help me here, thank you very much !

-----------------------------------------------------------------------
Server : nginx/1.11.6
PHP: FPM-FCGI / 5.3.29

i have root access.

-----------------------------------------------------------------------
Download path:

/home/users/domains/myweb.com/public_html/downloads

-----------------------------------------------------------------------
Here is the php code:

/**
* downloadXFile()
*
* @return
*/
function downloadXFile($fileLocation, $fileName, $maxSpeed = 1024)
{
if (connection_status() != 0)
return (false);
$extension = strtolower(substr($fileName, strrpos($fileName, '.') + 1));

/* List of File Types */
$fileTypes['swf'] = 'application/x-shockwave-flash';
$fileTypes['pdf'] = 'application/pdf';
$fileTypes['exe'] = 'application/octet-stream';
$fileTypes['zip'] = 'application/zip';
$fileTypes['doc'] = 'application/msword';
$fileTypes['xls'] = 'application/vnd.ms-excel';
$fileTypes['ppt'] = 'application/vnd.ms-powerpoint';
$fileTypes['gif'] = 'image/gif';
$fileTypes['png'] = 'image/png';
$fileTypes['jpeg'] = 'image/jpg';
$fileTypes['jpg'] = 'image/jpg';
$fileTypes['rar'] = 'application/rar';
$fileTypes['ra'] = 'audio/x-pn-realaudio';
$fileTypes['ram'] = 'audio/x-pn-realaudio';
$fileTypes['ogg'] = 'audio/x-pn-realaudio';
$fileTypes['wav'] = 'video/x-msvideo';
$fileTypes['wmv'] = 'video/x-msvideo';
$fileTypes['avi'] = 'video/x-msvideo';
$fileTypes['asf'] = 'video/x-msvideo';
$fileTypes['divx'] = 'video/x-msvideo';
$fileTypes['mp3'] = 'audio/mpeg';
$fileTypes['mp4'] = 'audio/mpeg';
$fileTypes['mpeg'] = 'video/mpeg';
$fileTypes['mpg'] = 'video/mpeg';
$fileTypes['mpe'] = 'video/mpeg';
$fileTypes['mov'] = 'video/quicktime';
$fileTypes['swf'] = 'video/quicktime';
$fileTypes['3gp'] = 'video/quicktime';
$fileTypes['m4a'] = 'video/quicktime';
$fileTypes['aac'] = 'video/quicktime';
$fileTypes['m3u'] = 'video/quicktime';

$contentType = $fileTypes[$extension];
header('X-Accel-Redirect: '. $fileLocation);
header("Content-Transfer-Encoding: binary\n");
header('Content-Type: $contentType');
$contentDisposition = 'attachment';


if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) {
$fileName = preg_replace('/\./', '%2e', $fileName, substr_count($fileName, '.') - 1);
header("Content-Disposition: $contentDisposition;filename=\"$fileName\"");
} else {
header("Content-Disposition: $contentDisposition;filename=\"$fileName\"");
}

header("Accept-Ranges: bytes");
$range = 0;
$size = filesize($fileLocation);

if (isset($_SERVER['HTTP_RANGE'])) {
list($a, $range) = explode("=", $_SERVER['HTTP_RANGE']);
str_replace($range, "-", $range);
$size2 = $size - 1;
$new_length = $size - $range;
header("HTTP/1.1 206 Partial Content");
header("Content-Range: bytes $range$size2/$size");
} else {
$size2 = $size - 1;
header("Content-Range: bytes 0-$size2/$size");
}

if ($size == 0) {
die('Zero byte file! Aborting download');
}
exit;

return ((connection_status() == 0) and !connection_aborted());
}

-----------------------------------------------------------------------

nginx.conf

location /downloads/ {
alias /home/users/domains/myweb.com/public_html;
}

-----------------------------------------------------------------------

My problem:

When i download the file via php , i always get 404 page error. looks the fileLocation did not get correctly.


My question:

A. how can i know my server already support X-Accel-Redirect & Sendfile or not ?
B. how to fix above issue.

Br.,

Denis

Viewing all articles
Browse latest Browse all 4759

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>