简体   繁体   English

如何在PHP中验证通过URL上传的文件

[英]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. 我通常知道如何验证文件上传,但是我正在寻找一种有关验证通过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. //我必须假定它是图像,并添加.jpeg,因为我不知道如何在不上传的情况下验证文件。

$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. 在建议任何库之前,请记住我正在使用laravel框架。

Use cURL to check for the file mime-type. 使用cURL检查文件的mime类型。 http://php.net/manual/en/function.curl-getinfo.php http://php.net/manual/zh/function.curl-getinfo.php

You can use get-headers() to obtain information such as content type and filesize before actually downloading the file contents. 在实际下载文件内容之前,可以使用get-headers()获取信息,例如内容类型和文件大小。

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. 要验证文件是否确实如它所说的那样,例如,检查.pdf文件是否实际上是变相的.zip文件,现在情况完全不同了,恐怕没有一种通用的方法可以检查该文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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