1
In my PHP script I have a line that reads a file with fopen
, using the url "https://api.modarchive.org/downloads.php?moduleid=".$i
($i
is an accountant of a loop) where I save the file in the variable $file
, and I want to save it on my computer using file_put_contents()
, only that for this, I need to set the path which I want to save, including the file extension.
The problem is that you can not know the file extension downloaded only by URL, so you can not use pathinfo()
, nor explode
, nor any kind of function that uses the file path as parameter, I can actually download 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 that I try to run currently 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 any function that returns the file name or extension, perhaps passing as parameter the file itself, instead of a string representing his path, or some better solution?
I don’t think I need it anymore, I found this function get_headers() that the position [7] gives me the name of the file and the extension,I can use this to get the file extension without problems
– vilok600
has better things: var_dump($http_response_header); that’s what I’m putting in the answer
– Bacco
The ideal is the response loop, because nothing guarantees that the order and quantity of the headers is the same in all calls. It worked pretty well the question code, test here: http://files.box.net.br/file.htm?i=7 (Change i to see the name change)
– Bacco