图像损坏后php curl传输FTP [英] Image corrupted after php curl transfer FTP
问题描述
我使用下面的代码来传输图像,它是工作除了jpg在传输后损坏。是无效的图像格式,并显示模糊的图像。
I am using the following code to transfer an image and it is working except the jpg is corrupted after the transfer. Is says invalid image format and shows a blurred image.
我尝试使用无curl的普通php,并得到相同的结果。
I tried using regular php without curl and get the same results.
但会破坏image.jpg
Does anyone know why whatever I try works but corrupts the image.jpg
$curl = curl_init();
$fh = fopen("test.jpg", 'w');
curl_setopt($curl, CURLOPT_URL, "ftp://{$serverInfo['user']}: {$servererInfo['password']}@{$serverInfo['ftp1.server.com']}/{$serverInfo['For_Web/Web Images/Full Size/00-99/file']}");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
fwrite($fh, $result);
fclose($fh);
curl_close($curl);`
推荐答案
有几个问题;
您应该以二进制模式打开您的文件,即;
You should open your file for writing in binary mode, that is;
$fh = fopen("test.jpg", 'wb');
curl_exec返回bool(成功),而不是文件的内容, to CURLOPT_FILE。
curl_exec returns a bool (success), not the contents of the file, the file should instead be passed to CURLOPT_FILE.
您应该使用CURLOPT_USERPWD设置用户名/密码,但不确定是否可以使URL的方式工作。
You should set the username/password using CURLOPT_USERPWD, not sure if the URL way could be made to work too, though.
您应该设置CURLOPT_BINARYTRANSFER。
You should set CURLOPT_BINARYTRANSFER.
工作示例;
$curl = curl_init();
$fh = fopen("fips.exe", 'wb');
curl_setopt($curl, CURLOPT_URL, 'ftp://ftp.sunet.se/pub/FreeBSD/tools/fips.exe');
curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FILE, $fh);
curl_setopt($curl, CURLOPT_USERPWD, 'anonymous:olle');
$result = curl_exec($curl);
fclose($fh);
curl_close($curl);
这篇关于图像损坏后php curl传输FTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!