Adding another URL into a download script - php

I have a video streaming website which also allows users to download videos. This runs over a download script that loads the videos id on the server and initiates a direct file transfer.
Now I want to put another URL in front of it instead of having the direct file transfer. I tried to make a mod_rewrite rule, but it causes a loop and redirects on that URL forever. Its a link shortening service which allows monetarization.
So, how do I archieve this? Is it possible to archieve it with a mod_rewrite rule?
Trying the mod_rewrite rule, realizing I can't change the base_url variable in PHP because it would not find the file anymore.
$vid = intval($_GET['id']);
$label = intval($_GET['label']);
$sql = "SELECT * FROM video WHERE VID = ".$vid." LIMIT 1";
$rs = $conn->execute($sql);
$formats = $rs->fields['formats'];
$server = $rs->fields['server'];
$formats_arr = explode(',', $formats);
if ($server != '') {
$sql = "SELECT * FROM video v, servers s WHERE v.VID = ".$vid." AND v.server = s.video_url LIMIT 1";
$rs = $conn->execute($sql);
$video_root = $rs->fields['video_url'];
}
if (!$video_root) {
$video_root = $config['BASE_DIR']."/media/videos";
}
foreach ($formats_arr as $format) {
$f = explode('.', $format);
if ($label == $f[1]) {
if ($f[0] >= 481) {
$condition = $new_permisions['hd_downloads'];
} else {
$condition = $new_permisions['sd_downloads'];
}
$file = $video_root.'/h264/'.$vid.'_'.$f[1].'.'.$f[2];
$file_name = $vid.'_'.$f[1].'.'.$f[2];
break;
}
}
if ($condition == 1) {
ini_set('memory_limit', '-1');
if (!$server) {
if (file_exists($file) && is_file($file) && is_readable($file)) {
$conn->execute("UPDATE video SET download_num = download_num+1 WHERE VID = ".$vid." LIMIT 1");
#ob_end_clean();
if(ini_get('zlib.output_compression')) {
ini_set('zlib.output_compression', 'Off');
}
header('Content-Type: application/force-download');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
header('Cache-control: private');
header('Pragma: private');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-Length: ' .filesize($file));
readfile($file);
exit();
} else {
VRedirect::go($config['BASE_URL']. '/error');
}
} else {
$conn->execute("UPDATE video SET download_num = download_num+1 WHERE VID = ".$vid." LIMIT 1");
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: chunked');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
$stream = fopen('php://output', 'w');
$ch = curl_init($file);
curl_setopt($ch, CURLOPT_READFUNCTION, function($ch, $fd, $length) use ($stream) {
return fwrite($stream, fread($fd, $length));
});
The download button should open URL/download.php with my link shortener URL in front of it, so it will load and redirect to the file download URL.

Related

Blank Download Page [PHP]

Recently moved a site over to a new server and now for some reason the download page is no longer downloading the purchased files.
When the download page is reached it is simply blank. I have changed the filename directory to use home/mastetu8 instead of home/mastvoic which was the last directory. Any idea on why the page is blank when trying to download?
Here is the download_process.php file currently running this piece:
<?php ob_start(); ?>
<?php require_once('../Connections/admin.php'); ?>
<?php
$orders_id = $_GET['orderid'];
$id = $_GET['id'];
mysql_select_db($database_admin, $admin);
$query_rsOrdersDetail = "SELECT * FROM orders_detail LEFT JOIN media ON orders_detail.product_id = media.sortorder WHERE orders_detail.orders_id = '$orders_id' AND orders_detail.id = '$id'";
$rsOrdersDetail = mysql_query($query_rsOrdersDetail, $admin) or die(mysql_error());
$row_rsOrdersDetail = mysql_fetch_assoc($rsOrdersDetail);
$totalRows_rsOrdersDetail = mysql_num_rows($rsOrdersDetail);
// CHECK DOWNLOAD COUNT
if ($row_rsOrdersDetail['downloadcount'] <= 3) {
$filename_hires = '/home/mastetu8/public_html'.$row_rsOrdersDetail['filename_hires'];
if (file_exists($filename_hires)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($filename_hires));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($filename_hires));
ob_clean();
flush();
readfile($filename_hires);
// UPDATE DOWNLOAD COUNTER
$downloadcount_new = $row_rsOrdersDetail['downloadcount'] + 1;
mysql_select_db($database_admin, $admin);
mysql_query("UPDATE orders_detail SET downloadcount='$downloadcount_new' WHERE id = '$id' AND orders_id = '$orders_id'", $admin) or die(mysql_error());
}
}
?>

Not able to download files with php in android browsers

When using computer files are downloaded normally.
Problem start on android browsers.
When we download a file from android default browser, the file is downloaded but it is of 0.00 bytes, so not technically present there(corrupt file).
When we download file from any third party application like or opera it gives error that "file could not be downloaded." not the header error. And files could be downloaded via UC Browser and Chrome and Firefox. But still gives the error in default browser.
Here is the code I am using:
<?php
require 'php/db.php';
if(isset($_POST['file_id'])&&!empty($_POST['file_id'])){
download_file($_POST['file_id']);
}
function download_file($id){
global $con;
$id = mysqli_real_escape_string($con,htmlentities($id));
$file="SELECT file_name,file_title,file_size,down FROM files WHERE file_id= $id";
$result = mysqli_query($con,$file);
$row = mysqli_fetch_assoc($result);
$name = $row['file_name'];
$title = $row['file_title'];
$size = $row['file_size'];
$ext = strtoupper(ext($name)); // Function defined bellow
$down = $row['down'];
$newname = $title.'.'.$ext;
$olddir = "files/".$name;
$down++;
if(is_file($olddir)) {
$update_down = "UPDATE files SET down = $down WHERE file_id = '$id'";
$update_down_result = mysqli_query($con,$update_down);
header('Pragma: public'); // required
header('Expires: 0'); // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($olddir)).' GMT');
header('Cache-Control: private',false);
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$newname.'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.$size); // provide file size
header('Connection: close');
readfile($olddir);
exit();
}else header("Location: index.php?msg=Sorry!+File+could+not+be+downloaded");
}
function ext($name){
$rut = strrev($name);
$erut = explode('.', $rut);
return strrev($erut[0]);
}
We give file id to this function and it downloads the file on PC.
Can anyone tell me how to remove the error? So that users can download files from their android phones too.
Probably $size == 0;
$size = $row['file_size'];
...
$olddir = "files/".$name;
change to
$olddir = "files/".$name;
$size = filesize($olddir);
And change
else header("Location: index.php?msg=Sorry!+File+could+not+be+downloaded");
to
else header("Location: index.php?msg=Sorry!+File+could+not+be+found+on+server: " . $olddir);
I got the solution.
Mainly I changed two things:
Used GET method instead of Post Method.
Used Flush and ob_clean functions to clear the buffer.
New code for downfile.php is like this:
<?php
require '../php/db.php';
ob_start();
if(isset($_GET['file_id'])&&!empty($_GET['file_id'])){
download_file($_GET['file_id']);
}else die("There was an error in downloading file. Please try again later.");
function download_file($id){
global $con;
$id = mysqli_real_escape_string($con,htmlentities($id));
$file="SELECT file_name,file_title,file_size,down FROM files WHERE file_id= $id";
$result = mysqli_query($con,$file);
$row = mysqli_fetch_assoc($result);
$name = $row['file_name'];
$title = $row['file_title'];
$ext = ext($name);
$down = $row['down'];
$newname = $title.'.'.$ext;
$size = $row['file_size'];
$down++;
if(is_file($name)) {
$update_down = "UPDATE files SET down = $down WHERE file_id = '$id'";
$update_down_result = mysqli_query($con,$update_down);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$newname.'"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: '.$size);
ob_clean();
flush();
readfile($name);
exit;
}else header("Location: index.php?msg=Sorry!+File+could+not+found!");
}
function ext($name){
$rut = strrev($name);
$erut = explode('.', $rut);
return strrev($erut[0]);
}
?>
Through this code I am able to download files in android browsers too.
Hope this may help. :)

Trying to do so that you can download. With sftp in php

I try to do so that I can download files but I have a problem and don't know how to fix it can you help me find the error?
Nothing happens. And help me to get this to work. I don't know how to fix this so need all the help I can get.
<?php
require("configuration.php");
require("include.php");
require_once('./libs/phpseclib/SFTP.php');
require_once("./libs/phpseclib/Crypt/AES.php");
if(!isset($_SESSION['clientid']))
{
//DON'T KNOW HOW THE REQEUSTOR IS!!
die();
}
$clientid = $_SESSION['clientid'];
$serverid = '';
$extendedPath = '';
$action = '';
if(!isset($_GET['serverid']) or !isset($_GET['path']) or !isset($_GET['action']))
{
die();
}
$serverid = $_GET['serverid'];
$extendedPath = $_GET['path'];
$action = $_GET['action'];
$boxDetailsSQL = sprintf("SELECT box.boxid, box.ip, box.login, box.password, box.sshport, srv.path
FROM %sbox box
JOIN %sserver srv ON box.boxid = srv.boxid
JOIN %sgroupMember grpm ON (grpm.groupids LIKE CONCAT(srv.groupid, ';%%')
OR grpm.groupids LIKE CONCAT('%%;', srv.groupid, ';%%'))
WHERE srv.serverid = %d
AND grpm.clientid = %d;", DBPREFIX, DBPREFIX, DBPREFIX, $serverid, $clientid);
$boxDetails = mysql_query($boxDetailsSQL);
$rowsBoxes = mysql_fetch_assoc($boxDetails);
$aes = new Crypt_AES();
$aes->setKeyLength(256);
$aes->setKey(CRYPT_KEY);
$sftp= new Net_SFTP($rowsBoxes['ip'], $rowsBoxes['sshport']);
if(!$sftp->login($rowsBoxes['login'], $aes->decrypt($rowsBoxes['password'])))
{
echo 'Failed to connect';
die();
}
//ACTION SELECTOR
if($action == 'list')
{
getlist($rowsBoxes, $extendedPath, $sftp);
}
if($action == 'fileUpload')
{
fileUpload($rowsBoxes, $extendedPath, $sftp);
}
if($action == 'download')
{
delete($rowsBoxes, $extendedPath, $sftp);
}
//ACTION FUNCTIONS
function download($rowsBoxes, $extendedPath, $sftp)
{
$remoteFile = dirname($rowsBoxes['path']).'/'.trim($extendedPath.'/');
$downloadfile $sftp->put($remoteFile);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($downloadfile));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($downloadfile));
readfile($downloadfile);
}
This code I need help to fix.
function download($rowsBoxes, $extendedPath, $sftp)
{
$remoteFile = dirname($rowsBoxes['path']).'/'.trim($extendedPath.'/');
$downloadfile $sftp->put($remoteFile);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($downloadfile));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($downloadfile));
readfile($downloadfile);
}
You're calling getList, fileUpload or delete, depending on the value of $_GET['action'] but you're not calling download at all. You've defined that function but you aren't calling it in your code snippet. And in your download function...
$downloadfile $sftp->put($remoteFile);
You should probably be doing this instead:
$downloadfile = $sftp->get($remoteFile);
Also, instead of doing filesize($downloadfile) do strlen($downloadfile) and instead of readfile($downloadfile) do echo $downloadfile;. If you want to save to a local file do $sftp->get($remoteFile, $downloadfile);
Updated code:
function download($rowsBoxes, $extendedPath, $sftp)
{
$remoteFile = dirname($rowsBoxes['path']).'/'.trim($extendedPath.'/');
$downloadfile = $sftp->get($remoteFile);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($downloadfile));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . strlen($downloadfile));
echo $downloadfile;
}
If you want to do something like display an image do this:
function download($rowsBoxes, $extendedPath, $sftp)
{
$remoteFile = dirname($rowsBoxes['path']).'/'.trim($extendedPath.'/');
$downloadfile = $sftp->get($remoteFile);
$file_extension = strtolower(substr(strrchr($filename,"."),1));
switch( $file_extension ) {
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpeg":
case "jpg": $ctype="image/jpg"; break;
default:
}
header('Content-type: ' . $ctype);
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
echo $downloadfile;
}

X-Sendfile notification after finish download

I would need to set the download status to 0 when download has finished. I am using x-sendfile, but after finish download, dont set status.
My code:
header("X-Sendfile: $file");
header("Content-type: application/octet-stream");
header('Content-Disposition: attachment; filename="' . $name . '"');
mysql_query("UPDATE `downloads` SET `download_status` = '1' WHERE `id` = '" . $last_down . "'");
Thanks for help
You can't detect when the client finished downloading the file if you using X-Sendfile.
Simplest way is stream file by script.
$handle = fopen($filepath, 'rb');
if ($handle !== false) {
#flock($handle, LOCK_SH);
$size = filesize($filepath);
$filename = basename($filepath);
$old_max_execution_time = ini_get('max_execution_time');
ini_set('max_execution_time', 0);
$old_cache_limiter = session_cache_limiter();
session_cache_limiter(false);
if (ob_get_length()) ob_clean();
header('Content-Type: application/octet-stream');
header('Content-Transfer-Encoding: binary');
header('Content-disposition: attachment; filename="'. $filename .'"');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Accept-Ranges: bytes');
header('Cache-control: private');
header('Pragma: private');
set_time_limit(0);
while (! feof($handle) && (connection_status() === 0)) {
if (! $buffer = #fread($handle, $interval)) {
// Error
exit;
}
print($buffer);
flush();
}
#flock($handle, LOCK_UN);
#fclose($handle);
ini_set('max_execution_time', $old_max_execution_time);
session_cache_limiter($old_cache_limiter);
// ----------------------------------------------------
// Downloading complete!
// Here you can update your table row in DB...
// ----------------------------------------------------
exit;
}

Php downloading all file types

i am just wondering if there is a way of setting up different "content-type" when downloading through php? like .mp3 AND .pdf etc.. instead of having to specify just one file type. My problem is that i have 2 file types to be downloaded, one type is pdf and the other type is mp3, but if i change the "content-type" to audio/mpeg, then it doesn't show the extension for the .pdf... i hope you understand? please help!
If you mean your user is downloading some content that's sent from a PHP script, which is also sending the Content-type HTTP header, can you not set that header with a different value for each type of file ?
Something like this (pseudo-code) :
if (file is a PDF) {
header('Content-type: application/pdf');
} else if (file is a MP3) {
header('Content-type: audio/mpeg');
}
And a "default" case might be useful, if you also have some other files you have not thought about just yet.
This function works great for me:
function Download($path, $speed = null)
{
if (is_file($path) === true)
{
set_time_limit(0);
while (ob_get_level() > 0)
{
ob_end_clean();
}
$size = sprintf('%u', filesize($path));
$speed = (is_null($speed) === true) ? $size : intval($speed) * 1024;
header('Expires: 0');
header('Pragma: public');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Type: application/octet-stream');
header('Content-Length: ' . $size);
header('Content-Disposition: attachment; filename="' . basename($path) . '"');
header('Content-Transfer-Encoding: binary');
for ($i = 0; $i <= $size; $i = $i + $speed)
{
echo file_get_contents($path, false, null, $i, $speed);
while (ob_get_level() > 0)
{
ob_end_clean();
}
flush();
sleep(1);
}
exit();
}
return false;
}
You can also optionally specify the max speed at which the file is delivered.

Categories