6

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?

Ashis Biswas
  • 747
  • 11
  • 28

1 Answers1

8

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

Community
  • 1
  • 1
Bert
  • 2,318
  • 1
  • 12
  • 9
  • 1
    You are right but sorry for my plain url format where url contain extension. It's useful when the url contain file extension. – Ashis Biswas Jul 23 '16 at 09:00