Force / Ask for video download on iPhone using PHP - php

I'm going crazy with this. Been looking here and at Google for a solution. The code I copied works fine under Android, but it does nothing when I try to run it on an iPhone 6. I read that it used to work on iPhone 5 at least, but seems that there is no way to force video download on iPhone 6 and 7.
Is there any way to force or ask for a file to be downloaded on an iPhone using PHP? There's something wrong with this code or is just a pain in the ass provided by Apple?
<?php
ob_start();
if(!empty($_GET['file'])) {
$fileName = basename($_GET['file']);
$fileName = getcwd() . '/uploads/videos/' . $fileName;
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $fileName);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
ob_clean();
flush();
readfile($fileName);
exit;
}

Related

PHP cant download image with readfile

This is my code:
$file = 'test.jpg';
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
test.jpg is a file on my system. I can open it and nothing is wrong with it. The download gets started but when i try to open the downloaded image, it says that it cant be opened. Im testing it on Chrome. Does someone have a solution? Thanks a lot.
Edit: the downloaded .jpg is only 23kb where as the file i want is 135kb.
Try to set header
header('Content-Type: image/jpeg');
ob_clean();
exit;
try replacing readfile with echo file_get_contents($file)

How do I force download of a file through Flysystem?

I may be missing something exceptionally obvious here, but I'm using yii2-flysystem along with Dropbox to read and write files.
I can upload and write them to Dropbox with no problem but then, when reading like this:
$file = Yii::$app->dropboxFs->read($fn);
..all that gives me is a string (/tmp/phpQkg8mJ).
How do I actually force the download of the file that I'm reading? I'm not sure what that temporary file location actually relates to.
Try function readfile().
According to example, your code should be looks something like this:
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}

readfile downloads corrupt files

I am facing a problem when i am trying to download a certain file from my localhost. Everytime i download a file, either a .docx, a .pdf or a .png it is always corrupt. I have spend multiple hours on finding a solution but nothing seems to work.
This is the script im running :
$file_download="loonadministratie/{$loon_id}/{$file}";
if (file_exists($file_download)) {
header('Content-Description: File Transfer');
header("Content-Type: application/octet-stream/loonadministratie/{$loon_id}/");
header('Content-Disposition: attachment; filename='.basename($file_download));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file_download));
readfile($file_download);
exit;
}
Thanks in forehand
Try
<?php
if (ob_get_level()) ob_end_clean();
?>
It may be the issue

ModX Evo: PHP readfile() in snippet?

I have a script that downloads a large file (1.3gb) using readfile().
If I create a .php page with just the script on it it works fine, but if I place the same script in a Snippet and place it on a page nothing happens.
Is ModX blocking the download some how? Any advice would be great thanks!
EDIT code:
$file = $_SERVER['DOCUMENT_ROOT']."/movie.mov";
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
};
Modx does have a maximum file size setting [Maximum upload size upload_maxsize] but that is for the file manager. I doubt that is your problem.
let's see the script and error logs.
UPDATE
just tested your little snippet out [with a couple of minor changes] ~ it works fine.
$base_path = $modx->config['base_path'];
$movie = 'frankenweenie-mrwhiskers_r640s.mov';
$file = $base_path.$movie;
if (file_exists($file)) {
echo 'file exists '.filesize($file);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
return true;
}else{
echo 'file does not exist';
return false;
}
using the $modx->config['base_path'] is not your problem, it worked using the server vars as well, it's just a good habit. as are the returning true/false modx expects it's snippets to return something whether true, false or $output ... also, not your problem it worked without.
Start looking at your php settings, I think possibly memory limit could be the problem. Check the php docs & see if it needs to have enough memory available to read a file that size. [even though it indicates 'size doesn't matter']
enable error logging in the script itself & check the server error logs.
It works with small files? Then look here: PHP readfile() and large downloads
Good luck!

Can I serve MP3 files with PHP?

In the same way that it's possible to serve up images with php, for use in CAPTACHAS and such, is it possible to do the same with audio files?
I've tried this
<?php
$track = "sometrack.mp3";
if(file_exists($track)) {
header('Content-type: audio/mpeg');
header('Content-length: ' . filesize($track));
header('Content-Disposition: filename="sometrack.mp3"');
header('X-Pad: avoid browser bug');
header('Cache-Control: no-cache');
print file_get_contents($track);
} else {
echo "no file";
}
I'm using Safari, which can play MP3 files. It's kicking Safari into the right mode, I get the Quicktime controls for a few seconds, and then "No Video".
I'm trying to protect files from unauthorized download in case you're wondering why I'd want to do this.
Your Content-Disposition should be:
header('Content-Disposition: attachment; filename="sometrack.mp3"');
Not sure if that's the problem though. I would also recommend using readfile to output the file:
readfile($rSong);
Also, it can't hurt to use an exhaustive Content-Type header, and set the Content-Transfer-Encoding:
header("Content-Transfer-Encoding: binary");
header("Content-Type: audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3");
try using This Class it supports download resume and speed limit believe me u need it as an owner of mp3 downloads website
Per the discussion here, plus readfile() from the PHP site for PHP 4, 5, 7 & 8, I came up with this:
if (file_exists($file)) {
header('Content-Description: File Transfer');
header("Content-Transfer-Encoding: binary");
header('Content-Type: audio/mpeg');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: '.filesize($file));
readfile($file);
exit;
}

Categories