简体   繁体   中英

How to validate files uploaded by URL in PHP

How to validate files before moving/uploading in the system.

I know how to validate file uploads in general, but I am looking for a solution about validating files that are uploaded by URL.

Here, have a look at my code, which refers to get the file and save it in my system :-

$file_url = 'https://www.something.com/images/Image.png'; // URL of the image/File
$destination_url = '/pictures/pics/image.jpeg';

// I have to assume that it's an image, and adding .jpeg since I don't know how to validate files without uploading it.

$data = file_get_contents($file_url); //gets the content of the file.
file_put_contents($destination_url, $data); //finally moves the file to system

And now I can do all the things, like getting it's name,size,type and so on, but that's not the point, Is there any way to validate the file before moving it to the system, since this procedure is really bad in security perspective, considering anyone can upload any size/type of file.


And before suggesting any library, please keep in mind I am using laravel framework.

Use cURL to check for the file mime-type. http://php.net/manual/en/function.curl-getinfo.php

You can use get-headers() to obtain information such as content type and filesize before actually downloading the file contents.

To validate if the file is really what it says it is, eg to check that a .pdf file is not actually a .zip file in disguise, now that is a whole different story and I'm afraid there's no universal way to check that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM