Login | Register | FAQ
Anonymous

Unable to download video/mp4 file with php

+ Post a reply

Page 1 of 1


Unable to download video/mp4 file with php

by mahajan_php » Mon Jun 25, 2012 4:19 am

Hello,

I am forcing php to download the video mp4 file, but it not completely downloaded from server, after 202 bytes it stopped. Please help me to solve my problem. here is the code i am using.

$filename = urldecode($_POST['file_path']);
$rename = $_POST['renamefile'];
// converting url to local path so Apache can find the file.
// force download:
// required for IE, otherwise Content-disposition is ignored
if (ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');

$parsed_url = parse_url($filename);
$fileinfo = pathinfo($filename);
$parsed_url['extension'] = $fileinfo['extension'];
$parsed_url['filename'] = $fileinfo['basename'];
$parsed_url['localpath'] = $_SERVER['DOCUMENT_ROOT'] . $parsed_url['path'];
// just in case there is a double slash created when joining document_root and path
$parsed_url['localpath'] = preg_replace('/\/\//', '/', $parsed_url['localpath']);

if (!file_exists($parsed_url['localpath'])) {
die('File not found: ' . $parsed_url['localpath']);
}
$allowed_ext = array('mp4', 'wmv', 'pdf', 'png', 'jpg', 'jpeg', 'zip', 'doc', 'xls', 'gif', 'exe', 'ppt');
if (!in_array($parsed_url['extension'], $allowed_ext)) {
die('This file type is forbidden.');
}

switch ($parsed_url['extension']) {

case "mp4": $ctype = "video/mp4";
break;
case "wmv": $ctype = "video/x-ms-wmv";
break;
case "pdf": $ctype = "application/pdf";
break;
case "exe": $ctype = "application/octet-stream";
break;
case "zip": $ctype = "application/zip";
break;
case "doc": $ctype = "application/msword";
break;
case "xls": $ctype = "application/vnd.ms-excel";
break;
case "ppt": $ctype = "application/vnd.ms-powerpoint";
break;
case "gif": $ctype = "image/gif";
break;
case "png": $ctype = "image/png";
break;
case "jpeg":
case "jpg": $ctype = "image/jpg";
break;
default: $ctype = "application/force-download";
}
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false); // required for certain browsers
header("Content-Type: $ctype");
//header("Content-Disposition: attachment; filename=\"".$rename.".".$parsed_url['extension']. "\";");

header("Content-Disposition: attachment; filename=\"".$parsed_url['filename']. "\";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($parsed_url['localpath']));
readfile($parsed_url['localpath']);
exit();



Thanks
Ramesh
User avatar

mahajan_php

  • Posts: 13
  • Joined: Mon Feb 20, 2012 1:09 pm

Re: Unable to download video/mp4 file with php

by XainPro » Mon Jun 25, 2012 6:33 am

do learn about mime types !
User avatar

XainPro

  • Posts: 3308
  • Joined: Fri Feb 17, 2012 8:10 pm


+ Post a reply

Page 1 of 1