Devlopers Friend
Devlopers Friend

Reputation: 439

retrieve file information from url using php

i am trying to retrieve information of file from the url containing the file. but how can i get the information of file before downloading it to my server. i need file information like file size,file type etc

i had found the code to validate and download file but how to get information from it before downloading file actually to server

<?php
function is_url_exist($url)
    {
    $ch = curl_init($url);    
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_exec($ch);
    $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    if($code == 200)
        {
        $status = "true";
        }
    curl_close($ch);
    if ( $status == true)
        {
        $name = "abc.png";
        if (file_put_contents("uploads/$name", file_get_contents($url)))
            echo "file uploaded";
        else
            echo "error check upload link";
        }
    }
$url = "http://theonlytutorials.com/wp-content/uploads/2015/06/blog-logo1.png";
echo is_url_exist($url);
?>

Upvotes: 2

Views: 1904

Answers (1)

Ranjit Shinde
Ranjit Shinde

Reputation: 1130

you can get all information of remote file by get_headers function. Try following code to find out type, content length etc.

$url = "http://theonlytutorials.com/wp-content/uploads/2015/06/blog-logo1.png";
$headers = get_headers($url,1);
print_r($headers);

Know more about get_headers click http://php.net/manual/en/function.get-headers.php

Upvotes: 1

Related Questions