I use the code below to download files. Everything works fine, but in the final version, the extension of the downloaded file may be different, is it possible to somehow find out the file extension when downloading it?

code:

$url = 'http://mysite.ru/myfile'; $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $data = curl_exec($ch); curl_close($ch); file_put_contents("TendersDocs/1.xlsx", $data); 
  • Do you want to get an extension using the php script and the $ FILES array? - Artur Han
  • no matter, the main thing to get the extension - evseygames
  • although it would be more convenient for me through $ _FILES - evseygames

3 answers 3

With this link, the only thing that comes to mind is to check the mime type and assign the most frequently used extension to it. But, unfortunately, the exact extension will not work out this way .. If you believe the list of office document types from small-scale ones, confusion can only be with outdated document extensions https://blogs.msdn.microsoft.com/vsofficedeveloper/2008/05/08/office -2007-file-format-mime-types-for-http-content-streaming-2 /

    If convenient through $ _FILES (for a file named 'file')
    Method 1.

     $_FILES['file']['type'] 

    Method 2

     mime_content_type($_FILES["file"]["tmp_name"]) 

    Method 3.

     pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION) 

    Method 4.

     $finfo = finfo_open(FILEINFO_MIME_TYPE); // возвращает mime-тип foreach (glob("*") as $filename) { echo finfo_file($finfo, $filename) . "\n"; } finfo_close($finfo); 
    • You did not understand the essence of the question - evseygames

    For example, for a file named $ filename

      $arr=explode('.',$_FILES[$filename][name]); $rashireniye=$arr[count($arr)-1]; 
    • what will play the role of filename here? - evseygames
    • path to the uploaded file along with the name. For example, /local/images/img.jgp - Artur Han