我想使用fopen,fwrite將文件從外部服務器保存到服務器上的文件夾中。使用fopen,在foreach循環中多次擦寫
首先從外部網站加載頁面,並掃描任何圖像鏈接。然後,該列表從一個fwrite函數發送。這些文件是創建的,但它們不是有效的jpg文件,在瀏覽器中查看它們,似乎它們在我的服務器上的路徑寫入了它們。
下面是代碼:
//read the file
$data = file_get_contents("http://foo.html");
//scan content for jpg links
preg_match_all('/src=("[^"]*.jpg)/i', $data, $result);
//save img function
function save_image($inPath,$outPath)
{
$in= fopen($inPath, "rb");
$out= fopen($outPath, "wb");
while ($chunk = fread($in,8192))
{
fwrite($out, $chunk, 8192);
}
fclose($in);
fclose($out);
}
//output each img link from array
foreach ($result[1] as $imgurl) {
echo "$imgurl<br />\n";
$imgn = (basename ($imgurl));
echo "$imgn<br />\n";
save_image($imgurl, $imgn);
}
的save_image功能的工作原理,如果我寫了一個清單:
save_image('http://foo.html', foo1.jpg);
save_image('http://foo.html', foo1.jpg);
我希望我能夠剛剛從循環列表在數組中匹配。
感謝您的期待。
非常感謝你,這個工程。 :) – Nooga 2011-03-07 00:17:03