Read the name or type of a remote file opened with "fopen"

1

In my PHP script I have a line that reads a file with fopen , using url "https://api.modarchive.org/downloads.php?moduleid=".$i ( $i is a counter of a loop ) where I save the file in I want to save it to my computer using $file , but for this, I need to define the path which I want to save, including the file extension.

The problem is that you can not know the file extension just downloaded by the URL, so you can not use file_put_contents() , nor pathinfo() , nor any type of function that uses the file path as parameter, I can actually downloading the file, but setting a default extension, or not setting.

The problem is that the original files have extensions and types that vary.

The code I'm currently trying to run is this:

for($i = 1; $i < 5; $i ++){
    $file = fopen("https://api.modarchive.org/downloads.php?moduleid=".$i, 'r');
    file_put_contents("mods/mod".$i.".extensaodoarquivo", $file);
}

Is there a function that returns the file name or file extension, perhaps by passing the file itself instead of a string representing its path, or some better solution?

>     
asked by anonymous 31.08.2016 / 00:02

1 answer

3

Simplest solution, $http_response_header :

The variable $http_response_header is a big help in the URL of your question.

When you use fopen() on remote resources, it is populated automatically with the request header.

To see the format of the data, just do this:

$file = fopen("https://api.modarchive.org/downloads.php?moduleid=".$i, 'r');
var_dump($http_response_header);

A line is of special interest to us:

Content-Disposition: attachment; filename=nomedoarquivo.it

As the name is already listed, just iterate the returned array, to find the desired line, and to extract the name:

$file = fopen("https://api.modarchive.org/downloads.php?moduleid=".$i, 'r');
$filename = '';
foreach( $http_response_header as $header ) {
    if( strtolower( substr( $header, 0, 20 ) ) === "content-disposition:" ) {
        $filename = substr( strstr( $header, '=' ), 1 );
    }
}

echo 'o nome original é: '.$filename

If the desired header exists, $filename will be populated with the original name.


Other possibilities:

A possible solution if we did not have the name in the headers, would be the function

mime_content_type( $file )

It will return the mime-type of the file by doing a file analysis based on the magic.mime file.

The return format is this:

image/gif
image/png
application/x-7z-compressed
... etc ...

In the documentation itself, you have some sample functions to make a array of extensions and types with ease.

Important: For better performance and bandwidth utilization, you can test the file once it has been received and then rename it, avoiding two unnecessary remote accesses.

Manual:

  

link


Alternatively you can use fileinfo functions:

<?php
  $fi = new finfo( FILEINFO_MIME,'/usr/share/file/magic' );
  $mime_type = $fi->buffer( file_get_contents($file) );
?>

Manual:

  

link


Both functions are based on the mime.magic file, which is commonly available on Linux servers, and also distributed with PHP if needed on other systems.

The mime.magic contains some strategic position and byte maps, which serve to characterize the files by their actual content.

    
31.08.2016 / 00:11