简体   繁体   English

如何在PHP中使用curl复制远程图像(不带文件扩展名)

[英]How to copy a remote image(without file type extension) using curl in PHP

I have this code working fine with the images that having file extension. 我的这段代码可以很好地处理带有文件扩展名的图像。

$ch = curl_init($remoteImageUrl);
$outputImagePath = dirname(__FILE__)."/saveto/thumbnail.jpg";

$fp = fopen($outputImagePath, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);

But this code does not work when $remoteImageUrl has no file extension. 但是,当$ remoteImageUrl没有文件扩展名时,此代码不起作用。 It creates an image which is 0 bytes. 它创建一个0字节的图像。 What is the way to solve the problem? 解决问题的方法是什么?

Try using FOLLOWLOCATION when dealing with extension-less files as some servers may get confused. 处理无扩展名的文件时,请尝试使用FOLLOWLOCATION ,因为某些服务器可能会感到困惑。 Use the RETURNTRANSFER constant to retrieve the image contents as string. 使用RETURNTRANSFER常量以字符串形式检索图像内容。

curl_setopt($ch, CURLOPT_RETURNTRANSFER , 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

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

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