Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get file extension by file_get_contents

Tags:

php

I want to upload a video file into my server from url. Here is my code.

<?php
    set_time_limit(0);
    $file_name = "upload/myvideo.mp4";
    $video_file_url = "http://example.com/somevideo.mp4";
    file_put_contents($file_name, file_get_contents($video_file_url));
?>

Now the problem is the $video_file_url contain different type of video like .mp4, .3gp or .flv . I dont know how to get file extension from file_get_contents and $file_name always may not same what I have mentioned in my code.

Should I save this file with tmpFile.tmp name and when copied from other server just rename it with valid name and extension?

like image 984
Ashis Biswas Avatar asked Jul 23 '16 08:07

Ashis Biswas


Video Answer


1 Answers

pathinfo() and parse_url() are what you need to get the extension.

$extension = pathinfo(parse_url($video_file_url, PHP_URL_PATH), PATHINFO_EXTENSION);

By the way, for the part of the code that you use to download the file, I would recommend to have a look at this question because file_get_contents() is extremely inefficient in this case: Download File to server from URL

like image 76
Bert Avatar answered Oct 12 '22 18:10

Bert