Ashis Biswas
Ashis Biswas

Reputation: 767

How to get file extension by file_get_contents

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?

Upvotes: 8

Views: 10576

Answers (1)

Bert
Bert

Reputation: 2456

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

Upvotes: 9

Related Questions