简体   繁体   English

cURL使用Mime类型检查将文件保存到服务器

[英]cURL Save File to Server With Mime Type Check

I am creating an internal tool for my team that will allow trusted team members to save remote files to our server via php and curl. 我正在为团队创建一个内部工具,该工具将允许受信任的团队成员通过php和curl将远程文件保存到我们的服务器。 I have the open, write, and close working perfectly, but I would like to add a check to make sure the file is of a certain mime type before it creates and writes the local file. 我的打开,写入和关闭操作均正常,但是在创建和写入本地文件之前,我想添加一个检查以确保该文件具有某种mime类型。

How could I do this, based on an array of mime types? 基于一系列的mime类型,我该怎么做?

$ch = curl_init();
$fp = fopen($local_file, 'w+');
$ch = curl_init($remote_file);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_exec($ch);
curl_close($ch);
fclose($fp);

I solved this by checking the mime via fileinfo after the file has been transfered. 我通过在文件传输后通过fileinfo检查mime来解决此问题。 If it is not a valid mime type, then I remove it. 如果这不是有效的mime类型,则将其删除。

$ch = curl_init();
$fp = fopen($local_file, 'w+');
$ch = curl_init($remote_file);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_exec($ch);
curl_close($ch);
fclose($fp);

$finfo = new finfo(FILEINFO_MIME);
$mime_type = $finfo->file($local_file);

if (strpos($mime_type, 'application/xml') === false) {
    unlink($local_file);
}

For example: 例如:

$ch = curl_init('http://static.php.net/www.php.net/images/php.gif');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);

$mime = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);

in $mime has mime type of file. $mime中的mime文件类型。

Tricky! 整rick You could download the whole file first (into memory, or a temporary folder.) If you want to stream it, you may have to: 您可以先下载整个文件(到内存或临时文件夹中)。如果要流式传输它,则可能必须:

  1. set CURLOPT_HEADER to include the HTTP response headers in the data you get back 设置CURLOPT_HEADER以在返回的数据中包含HTTP响应标头

  2. set CURLOPT_WRITEFUNCTION instead of CURLOPT_FILE, read and parse the http headers to see the mime type, and then decide if you're going to create/write a file or not. 设置CURLOPT_WRITEFUNCTION而不是CURLOPT_FILE,读取并解析http标头以查看mime类型,然后确定是否要创建/写入文件。

Obviously this is quite a bit of work, as you'll have to do some basic parsing of the HTTP headers, and possibly buffering to get the whole HTTP headers at once. 显然,这是相当多的工作,因为您必须对HTTP标头进行一些基本的解析,并可能需要进行缓冲才能立即获取整个HTTP标头。

Hopefully someone will post an easier solution. 希望有人会发布更简单的解决方案。

pseudocode: 伪代码:

state = headers
buf = ''
fd = null
func writefunc(ch, data)
   if state is headers
      buf .= data
      div = buf.strpos "\r\n\r\n"
      if div !== false
         mime = get_mime buf, div
         if mime_ok mime
            fd = fopen ...
            fd.write buf.substr div+4
            state = saving
         else
            # returns other than data.length() abort connection
            return 0
  else
     fd.write data
  return data.length()

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

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