Php 卷曲的图像保留0字节
我使用cURL检索图像,重命名它并将其存储在本地。 图像显示为0字节文件,不管我是否使用cURL,如下所示:Php 卷曲的图像保留0字节,php,curl,Php,Curl,我使用cURL检索图像,重命名它并将其存储在本地。 图像显示为0字节文件,不管我是否使用cURL,如下所示: $strImageUrl = curl_init($strImageUrlSource); $fp = fopen($strTargetImage, 'wb'); curl_setopt($strImageUrl, CURLOPT_FILE, $fp); curl_setopt($strImageUrl, CURLOPT_HEADER, 0); curl_exec($strImageUr
$strImageUrl = curl_init($strImageUrlSource);
$fp = fopen($strTargetImage, 'wb');
curl_setopt($strImageUrl, CURLOPT_FILE, $fp);
curl_setopt($strImageUrl, CURLOPT_HEADER, 0);
curl_exec($strImageUrl);
curl_close($strImageUrl);
fclose($fp);
或文件放入/获取。像这样:
file_put_contents($strImageName, file_get_contents($strImageUrlSource));
我正在检索的URL是:
我可以手动正确保存此图像。
查看FireFox中的属性时,会显示三个条目:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFgAAABYBAMAAACDuy0HAAAAG1BMVEX+/v4BAQH///8KCgoDAwN/f3/19fWAgID8... etc
我做错了什么?使用var\u dump()进行调试。当你离开时,你看到了什么
var_dump(file_get_contents('http://i1.au.reastatic.net/150x112/73fa6c02a92d60a76320d0e89dfbc1a36a6e46c818f74772dec65bae6959c62f/main.jpg'));
这项工作:
使用文件获取内容
$image = 'http://i1.au.reastatic.net/150x112/73fa6c02a92d60a76320d0e89dfbc1a36a6e46c818f74772dec65bae6959c62f/main.jpg';
$imageName = pathinfo( $image, PATHINFO_BASENAME );
file_put_contents( $imageName, file_get_contents( $image ) );
使用卷曲
$image = 'http://i1.au.reastatic.net/150x112/73fa6c02a92d60a76320d0e89dfbc1a36a6e46c818f74772dec65bae6959c62f/main.jpg';
$imageName = pathinfo( $image, PATHINFO_BASENAME );
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $image );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$source = curl_exec( $ch );
curl_close( $ch );
file_put_contents( $imageName, $source );
希望这能有所帮助。可能有超时、重定向、热链接保护或类似的情况。您可能希望使用其他允许重定向的curl选项,等等。请参阅我的解决方案。顺便说一句,这将删除Linux中所有大小为零的图像:find/home/某处/images-type f-size 0-exec rm{};
$image = 'http://i1.au.reastatic.net/150x112/73fa6c02a92d60a76320d0e89dfbc1a36a6e46c818f74772dec65bae6959c62f/main.jpg';
$imageName = pathinfo( $image, PATHINFO_BASENAME );
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $image );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$source = curl_exec( $ch );
curl_close( $ch );
file_put_contents( $imageName, $source );