0

我正在尝试将tcp图像从我的 linux php ec2 实例转移到另一台服务器。
当我回显内容时fopenfread我可以看到图像已处理但只处理了一半
请问有人知道是什么原因造成的,谢谢。

$imageURL = 'http://ec2-**-***-**-**.compute-1.amazonaws.com/New_Era_For_NASA_2.jpg';
$ch = curl_init($imageURL);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$data = curl_exec($ch);
//echo $data;
curl_close($ch);
if ($data === false) {
        die('cURL failed');
}
if ( preg_match('/Content-length: (\d+)/', $data, $matches) || preg_match('/Content-Length: (\d+)/', $data, $matches) ) {
        $size = (int)$matches[1];
}   

$fileHandle = fopen($imageURL, 'rb'); //r or rb
$fileData = fread( $fileHandle, $size );
//echo $fileData;
fclose( $fileHandle );
$data = $fileData;

header('Content-type: image/jpeg'); 
echo $data;

在此处输入图像描述

4

2 回答 2

4

为什么要取两次?卷曲可以一步完成这项工作。IE

$imageURL = 'http://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Tibia_insulaechorab_transparent.png/320px-Tibia_insulaechorab_transparent.png';
$ch = curl_init($imageURL);    

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

$data = curl_exec($ch);
curl_close($ch);

if ($data === false) {
        die('cURL failed');
}

header('Content-Type: image/png');
header('Content-Length: ' . curl_getinfo( $ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD ) );
echo $data;
于 2012-11-30T17:08:56.590 回答
0
$imageURL = 'http://ec2-**-***-**-**.compute-1.amazonaws.com/New_Era_For_NASA_2.jpg';
$ch = curl_init($imageURL);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$data = curl_exec($ch);
echo $data;
curl_close($ch);

就够了

file_get_content( $imageURL );

可以工作到 0,但要注意 memory_limit,fread 可能是一个更好的选择。

于 2012-11-30T17:06:07.430 回答