Retrieve the complete details of a remote file without downloading it - php

How could I extract the METADATA / METAINFO / everything that would describe the REMOTE FILE without downloading it in the first place?
do you have some snippets in PHP?
how about in cURL?
or any language?
thanks

You can't get all the metadata from a file without downloading the entire file, as you have no way of knowing how much of the file you need before you've captured its metadata block, some file formats store metadata at the end of the file (meaning you'll need the entire file anyway), some files don't have any metadata embedded in them at all, and so on.
If you do a HEAD request, you will usually be able to get some basic metadata on the file in question in the form of the content-type and content-length headers returned by the server, but this is typically limited to the MIME type and the file size (and if you're dealing with a script that serves the file and that script doesn't set the necessary headers, you might not even get that).

Related

Detecting partially uploaded files?

I have a webapp which reads from the php://input and it streams from it and than stores that data in a new file, however, because of this, a person might upload a file partially and it'll still be saved, so my question is: how can I make sure that every file which was uploaded this way was infact fully uploaded?
I guess it is something to do with content-length, but I haven't found any resources which talk about this.
Yes, you should be able to retrieve the content length from the HTTP header. After that it is just binary data. You cannot determine if the file was fully uploaded from the file information on the file system, of course.
See also: Get size of POST-request in PHP

Download file in browser from CDN and set downloaded file name

I've got call recordings stored on a CDN (Rackspace CloudFiles) with names like:
KPOWIEJFIE2034020SVN10ASKZALBMRI.mp3
(They're Twilio Call SIDs).
My application (CakePHP) displays a list of these recordings, and uses an <audio> tag to allow them to be played on the page right from the CDN. I need to add the option to download the file directly as well, but I want to download the file named something more like this:
Call from Alex to Firm, Inc, 8/19/2011 4:00pm.mp3
I know how to do this with PHP and readfile() to set the name to whatever I'd like, but that requires the file data be streamed through my VPS. Besides being billed twice for the bandwidth (once through the CDN and once through my VPS), this would defeat the purpose of having my files on a CDN (speed and availability). I have the files named only with the call SID on the CDN for security.
Can I do this with browser-side somehow? Can JavaScript change the name of a downloaded file on the fly?
I appreciate any help!
The filename is based on the URL being requested and the headers attached to the response when the URL is fetched. Unless you proxy it, or can arrange for a "friendly" url to point at the resource, you have no control over how the browser will download the file, since the request will be handled by the rackspace servers, not your code.
JS cannot control the fetch/download process, other than possibly mangling the url that goes into the <audio> tag. but then you're limited to whatever mangling still allows the URL to be relevant to the cloudfiles servers.
You should be able to set an object's Content-Disposition when creating the file. For example,
PUT /<api version>/<account>/<container>/<object> HTTP/1.1
Host: storage.clouddrive.com
X-Auth-Token: 01234567-89ab-cdef-0123-456789abcdef
Content-Type: audio/mpeg
Content-Disposition: attachment; filename=Whatever_Filename_I_Want.mp3
The file will still be stored as whatever your <object> name is, but when retrieved it will appear as Whatever_Filename_I_Want.mp3.
Edit: According to the documentation, you can also update a file's metadata using this method.
From what I understand of the documentation, you're able to build a directory structure on the CDN for your purposes. So instead of relying on the security with the filename, you could put the security into the directory name. That way, http://cdn.example.com/KPOWIEJFIE2034020SVN10ASKZALBMRI.mp3 becomes http://cdn.example.com/KPOWIEJFIE2034020SVN10ASKZALBMRI/Call_from_Alex_to_Firm,_Inc,_8-19-2011_4:00pm.mp3, which will download exactly as you want it to be, without sacrificing the security.

How do I create a PHP page which can accept file uploads?

I want to upload a file to my server from some application. How can I code a PHP page to accept this file?
The application should just include the file in an HTTP POST request as would an HTML form with an input of type file. In the PHP code, the posted file contents would be available in the $_FILES array.
Lots more information here.
Keep in mind that the concept of a "file" doesn't mean the same thing over HTTP that it does on a local computer or on the target server. In HTTP, the "file" is just a stream of data wrapped in an HTTP request or response with a given content type and various other headers, no different from a web page or an image or any other request/response.
I would suggest reading the PHP manual chapter on handling file uploads

File uploads without $_FILES

Is it possible in PHP to configure it to somehow not save files to disk at all? As a matter of fact, the best thing would be to get the script going before even reading the entire POST body. (Keeping my hopes high ;))
You can turn off file uploads via a configuration setting in PHP.
http://php.net/manual/en/ini.core.php#ini.file-uploads
PHP needs a place to temporarily store the files content for you to be able to interact with it through PHP - although, you don't have to do anything else other then access the temporary file to get the data:
$content = file_get_contents($_FILES["user_file"]["tmp_name"]);
From here on you can manipulate with the files content without having to move the uploaded file to another location before accessing it.
You can use HTTP PUT requests to directly upload a file. PHP will not handle the upload directly (e.g. set it up in $_FILES). Instead, you have to read the raw bytes from the php://input pseudo-url and from there can do whatever you want.
There's some details and examples here.

Serving files from SQL Server through Zend

I am storing files into an image field in SQL server storing the string in hex after converting into using:
unpack("H*hex", $datastring);
If I read from the database and write to a file, then open the file locally I can open it up just fine. But if I try to send the file as an attachment to the browser the file becomes corrupted and unreadable. Currently, the files in question are PDF and MSWord documents.
I am setting content-type to the appropriate MIME type for the files and setting content-disposition to attachment. I've tried various ways of streaming the file including dumping the string directly from the database and writing first to a file then reading the file (either line by line or with readfile()).
I am using a slightly customized version of the Zend framework, but I'm not sure if that is causing any issues. What should I do to send files to the browser?
How do you serve them?, in theory if you are using MVC, you need to disable the view at the end of your controller to avoid extra content being inyected at the bottom of your file.

Categories