PHP output internal server file? - php

On my server I have a directory with music files, generally in .mp3 format. I'm writing a web application to search for and play these tracks.
All the files are stored, with their absolute server path, artist, album and title info in a MySQL database.
What I want to do is have a PHP file that "outputs" an mp3 file on the server that would normally be inaccessible from the web. So, this is what I want to achieve:
client requests play.php?id=10
PHP gets absolute server path from MySQL database where ID = 10
PHP outputs the file (which would really be at e.g. '/home/user/files/no_web/mp3/Thing.mp3')
To the client, it looks like there is an mp3 file called http://myserver.com/play.php?id=10 and it starts to play.
I'm sure this sort of thing is possible, just not sure how. Thanks in advance :)

You need to send correct content-type header and then just output the file:
header('Content-type: audio/mpeg3');
readfile('filename.mp3');

For reading the file and sending it, you can use the readfile function.
For setting the mime-type, so the browser actually knows what type of file is sent by the webserver, use the header function like:
header('Content-Type: audio/mpeg');
Additionally, you may also want to set the Content-Length HTTP header.
header('Content-Length: ' . filesize($filepath) );

If all you're trying to do is let the user download the mp3, just use the readfile command which will read the mp3 file and pass it along to the client. However you need to make sure to set the mime-type correctly.

Related

Save base64 encoded pdf on server with php

I have a call to a web service that gives me a response back in the format of a base64 encoded pdf file. I need to save the pdf to a folder on the server.
I can't get the document saved in the folder. It always saves the file locally on the computer and not to the server.
Here is the code i have at this moment.
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="doc-'.$document.'.pdf"');
file_put_contents('~/pdf/'.$document.'.pdf', $wc->out->document);
Does someone have a suggestion where i can find the solution?
thx
Headers are for serving data to the client - you don't need these. Just use file_put_contents
As for the file save location, you know that will save into a folder within the user profile? If you want the current folder, use . instead of ~.

How indirect file urls works?

A url is the direct link towards a resource as per my knowledge. I have experience with Apache and PHP, and I know that using .htaccess file, one may set a default file (like index.php) for a location where no need to provide end file name.
But for file links, one need to provide a direct link, having filename and extension like somedomain.com/file.txt. But recently I found some links, especially download links, that dont have a url with filename and extension.
For example, i tried to grab direct .mp4 file url for this youtube video www.youtube.com/watch?v=W1- L58y2uf4 , of resolution 1080p HD. Then I got a url (using clipconverter.cc) like;
http://r9---sn-25g7sne7.c.youtube.com /videoplayback?source=youtube& ip=2001:41d0:8:1f2b:3a0e:6049:6b4f:92 e9&expire=1378905654& sparams=cp,id,ip,ipbits,itag,ratebypass,sou rce,upn,expire&ipbits=48& upn=BgsjQ8lS424& cp=U0hWTVlLU19KTkNONl9RRVdHOkZIZ0 diYTFXLWRJ&key=yt1& id=5b5f8be7ccb6b9fe&mt=1378881529& ratebypass=yes&itag=37&sver=3& mv=m&fexp=903309,919391,910207,91 4071,916612,924606,929117,929121,92 9906,929907,929922,929127,929129,92 9131,929930,936403,925726,936310,92 5720,925722,925718,925714,929917,90 6945,929933,920302,906842,913428,92 0605,919811,913563,919373,930803,90 8536,938701,931924,936308,909549,90 0816,912711,904494,904497,939903,90 0375,900382,934507,907231,936312,90 6001&ms=au& signature=1DDD3BB4A46816E27075ADF1 3C84B810AD1DF72D.C9B4290CE7F0806A 3174E65DE3920F3AFDB06833& title=Kilimanjaro+-+ROBOT+%282010%29 +%2AHD%2A+1080p+%2ABluRay%2A+ Music+Video
The browser downloads the file perfectly. How this works? Where is the filename and extension in this link?
Please repeat:
URLs !== files
URLs !== files
URLs !== files
When requesting a URL, your browser/HTTP client/whatever is sending an HTTP request to a web server, requesting the URL. The web server is free to respond to this request in any way it pleases. URLs have nothing at all to do with files on a hard disk. It's just a convenient default configuration that web servers look for files of the same name as the requested URL and serve those. But it could do anything else it wanted as well. It can start up a shell script which gets the requested URL passed as an argument, which in turn can output anything it wanted. The web server may be a Java application which processes the requested URL internally and responds with some content. The server could be anything and everything at all and it can respond by doing anything it wants to. A web server is just an application that listens on port 80 (or elsewhere) and answers incoming HTTP requests. The file system doesn't have to be involved at all.
You're probably calling a script which depending on your parameters finds the file you want and before sending it, it modifies the headers to make your browser treat the file as a video file (and download it) and not as a regular html document
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename="'.$filename.'"');
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");

PHP mime type not working for MP3

I am having an issue when using mime types in PHP.
As a security feature, I am placing any content uploaded to my server by users in a directory outside of public_html. When a user wants to access their uploaded files in the browser, I use php script to pull the file from the secure folder using readfile and header('content-type'), the issue I am having so far only pertains to mp3s.
When I try and use this for a mp3 file, the file doesn't actually play in the browser, but M4As work fine. And it isn't every mp3 file too, it just seems that when I get the mime-type of some mp3 files php can't tell that they are mp3s. The mp3s also playback fine if I download them, so it isn't an issue with the file per say (unless the file has the wrong mime-type somehow).
How do I get it to tell that they ARE mp3s?
My getFile code is here: codepad.org
Any help would be appreciated greatly, I'll even give you some of my tracking cookies!
For mp3s, did you try:
header("Content-Type: audio/mpeg");

Browser won't download file correcty

I've looked through the site and can't find an answer to my question. I'm trying to make a winrar file (which is 1GB) downloadable from my server and whenever I try, it gives me a winrar file with the same name that is only like 9kb. Here's what I have for the headers. I'm really new to downloadable content so, don't make fun of me. :D
header('Content-Disposition:attachment; filename="java.rar" ');
I'm assuming that I need more. hahaha!
I keep seeing people use header('Content-type: application/pdf'); above their disposition. Does this support rar format, or do I need to use it as zip?
Probably your script is timing out and only a small part from 1GB file is being sent. You may try increasing time limit but IMO for such a big file you'll have to link directly to make it downloadable.
If you want to count file downloads you may link to a php script which will increase the counter and redirect browser directly to the file afterwards.

Renaming File on another server as user downloads it [2] - using PHP

I have asked this question today already but this time I want to know if I can achieve this via PHP since Javascript wasn't up to it.
I have a link to a file on another server. If i provide this link to my users the headers are pushed out to download that file from that server.
Is there a way for me to capture those headers and file and redirect the download to the user? I would like to do this so that I can change the filename of the download since it is
always 'file.zip'.
Is this possible with PHP?
Thank you for any help.
You can download the file to your server using curl and serve it correctly(with a Content-Disposition header). As long as you are using HTTP, there's no way to send just the header and let another server stream the content directly to the client.
You could do this, and you can do it in several ways.
1) (simple) copy the file to your server, and rename it. Point your download links to this copy.
2) (harder) Create a stub php file, called , read the file from the remote server within php, and stream the content to the script output. This will need you to set appropriate headers, etc. as well as setting up your webserver to parse through PHP.
Seriously, I'd go with option 1. (assumes you have a legal right to serve the content, etc.)
Maybe you can use a script similar to the following one:
<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.example.com/the_path/file.zip");
header('Content-Disposition: attachment; filename="alternate_filename.zip"');
exit();
?>

Categories