更新:我简化了代码(尝试过)
我正在尝试下载数组中设置的一系列图像,但显然不对:
function savePhoto($remoteImage,$fname) {
$ch = curl_init();
curl_setopt ($ch, CURLOPT_NOBODY, true);
curl_setopt ($ch, CURLOPT_URL, $remoteImage);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 0);
$fileContents = curl_exec($ch);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($retcode==200) {
$newImg = imagecreatefromstring($fileContents);
imagejpeg($newImg, ".{$fname}.jpg",100);
}
return $retcode;
}
$filesToGet = array('009');
$filesToPrint = array();
foreach ($filesToGet as $file) {
if(savePhoto('http://pimpin.dk/jpeg/'.$file.'.jpg',$file)==200) {
$size = getimagesize(".".$file.".jpg");
echo $size[0] . " * " . $size[1] . "<br />";
}
}
我收到以下错误:
警告:imagecreatefromstring() [function.imagecreatefromstring]: 空字符串或无效图像 C:\的Inetpub \虚拟主机\ dehold.net \的httpdocs \ ripdw \的index.php 在第15行
警告:imagejpeg():提供 参数不是有效的图像资源 在 C:\的Inetpub \虚拟主机\ dehold.net \的httpdocs \ ripdw \的index.php 第16行
警告:getimagesize(.009.jpg) [function.getimagesize]:失败了 open stream:没有这样的文件或目录 在 C:\的Inetpub \虚拟主机\ dehold.net \的httpdocs \ ripdw \的index.php 在第26行 *
答案 0 :(得分:0)
试试这个:
function get_file1($file, $local_path, $newfilename)
{
$err_msg = '';
echo "<br>Attempting message download for $file<br>";
$out = fopen($newfilename, 'wb');
if ($out == FALSE){
print "File not opened<br>";
exit;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_FILE, $out);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $file);
curl_exec($ch);
echo "<br>Error is : ".curl_error ( $ch);
curl_close($ch);
//fclose($handle);
}//end function
答案 1 :(得分:0)
您应该尝试使用file_get_contents来代替CURL(更简单,但它可以完成工作):
function savePhoto($remoteImage,$fname) {
$fileContents = file_get_contents($remoteImage);
try {
$newImg = imagecreatefromstring($fileContents);
imagejpeg($newImg, ".{$fname}.jpg",100);
} catch (Exception $e) {
//what to do if the url is invalid
}
}
答案 2 :(得分:0)
我终于得到了它的工作,在你们所有人的帮助下,还有一些窥探: - )
我最终使用了CURL:
function savePhoto($remoteImage,$fname) {
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $remoteImage);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 0);
$fileContents = curl_exec($ch);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($retcode == 200) {
$newImg = imagecreatefromstring($fileContents);
imagejpeg($newImg, $fname.".jpg",100);
}
return $retcode;
}
$website = "http://www.pimpin.dk/jpeg";
$filesToGet = array('009');
$filesToPrint = array();
foreach ($filesToGet as $file) {
if(savePhoto("$website/$file.jpg",$file)==200) {
$size = getimagesize($file.".jpg");
echo $size[0] . " * " . $size[1] . "<br />";
} else {
echo "File wasn't found";
}
}