0

I have this code triggered when user download mp3 file

$file = $_GET['file'];
$ext = pathinfo($file, PATHINFO_EXTENSION);
$match_array =array('pdf','mp3','mpa','ra','wav','wma','mid','m4a','m3u','iff','aif');
if(in_array($ext,$match_array)){
  header("Content-type: application/".$ext);
  header("Content-Disposition: attachment; filename=". $file);
  readfile($file);
}

but downloaded file is 0 byte so what is the problem ?

File exists and $file contain this value

https://mysite/upload/1/myfile.mp3

zac
  • 4,495
  • 15
  • 62
  • 127
  • If you put that URL directly in your browser does it download the file? Have you checked that your `if` statement worked? Have you checked whether you've got the right wrappers enabled to allow putting URLs as a parameter to the readFile method? (See http://php.net/manual/en/function.readfile.php for a note on that last question) – ADyson Mar 22 '19 at 22:34
  • 1
    Possible duplicate of [PHP Force Download Causing 0 Byte Files](https://stackoverflow.com/questions/4706073/php-force-download-causing-0-byte-files) – ArSeN Mar 22 '19 at 22:34
  • @ADyson no it open/stream the file – zac Mar 22 '19 at 22:36
  • Ok. But that confirms the URL is correct at least. Now you need to check my other two points – ADyson Mar 22 '19 at 22:37
  • @ADyson checked now it is `1` – zac Mar 22 '19 at 22:46
  • @Wel your code works in my xampp server, it complete down load a mp3 file – Kevin Bui Mar 22 '19 at 22:52
  • It is not clear what your are doing. Do you want to upload the file on your server? or to download from the server. Seem you want to download. But then I can't get `$file = $_GET['file'];` this code expects get parameter `file` to exist. For example `https://mysite/upload/?file=myfile.mp3` but you said *File exists and $file contain this value https://mysite/upload/1/myfile.mp3* which make no sense. If `$file = 'https://mysite/upload/?file=myfile.mp3'` then it means that you are trying to read `readfile($file);` from remote domain? – Alex Mar 22 '19 at 22:53
  • Should I pass also `filesize` header ? – zac Mar 22 '19 at 22:55
  • "checked now it is 1 "...what is? And I asked you two questions – ADyson Mar 23 '19 at 05:56
  • Also, is the file actually on another server, or your own? Do you _need_ to use a URL here or could you replace it with a filesystem path? – ADyson Mar 23 '19 at 06:01
  • I found the problem that the server set php version to 5 when I set it to 7 it was solved. I posted as an answer but a moderator deleted my answer !!! – zac Mar 24 '19 at 10:59

2 Answers2

2

Your issue might be a configuration problem.

Remember allow_url_fopen has to be = on in php.ini From Here

Another alternative might be to use the below code instead and save it to your local filesystem.

$localPath = "tmp/foo.mp3";
$contents = file_get_contents($file);
$save = file_put_contents($localPath, $contents);
matt
  • 1,680
  • 1
  • 13
  • 16
  • I get this warning: Warning: file_put_contents(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone seems timezone not set in php.ini – zac Mar 22 '19 at 23:06
  • 1
    @Wel Add `date_default_timezone_set('America/Los_Angeles');` to the top of your file or config file if you're including one. Lookup what your own timezone is [HERE](http://php.net/manual/en/timezones.php) – matt Mar 22 '19 at 23:18
  • I get failed to open file or directory in `file_put_contents` – zac Mar 22 '19 at 23:42
  • @Wel That means `$localPath` isn't right you have to make sure the folder path you're putting the file into is correct. `/foo.mp3` should put it in the current working directory (where your script is located). – matt Mar 22 '19 at 23:50
  • still 0 byte file. is it a server problem ? – zac Mar 22 '19 at 23:56
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/190536/discussion-between-matt-and-wel). – matt Mar 23 '19 at 00:01
  • How is file_put_contents relevant to this situation? The objective is to read a file, not write one. I know you're explaining about the fopen options but it was would be better to stick to the example code in the question using readFile. It seems Wel is now trying to run file_put_contents and failing...but it's not necessary to do so – ADyson Mar 23 '19 at 05:58
  • Your suggestion to save it to the local filesystem doesn't, as far as I can see, remove the need to fetch the file from the remote URL first, for which we still need the fopen flags. Saving it locally instead of streaming directly back to the client is just an extra step which uses more CPU and leaves clutter on the server – ADyson Mar 23 '19 at 06:00
1

It was server problem and PHP was set on old 5 version so when I changed the version to 7 it worked fine.

zac
  • 4,495
  • 15
  • 62
  • 127