我有一個php腳本,它從外部URL抓取圖像,讀取並保存到我的服務器上的目錄中。該腳本位於一個PHP文件,包含:通過php在窗體中保存圖像URL
<?php
$image_url = "http://example.com/image.jpg";
$ch = curl_init();
$timeout = 0;
curl_setopt ($ch, CURLOPT_URL, $image_url);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
// Getting binary data
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$image = curl_exec($ch);
curl_close($ch);
$f = fopen('/home1/path/public_html/path/saved/image.jpg', 'w');
fwrite($f, $image);
fclose($f);
?>
一切的偉大工程有...
我希望做的是有劇本做了多個網址。 URL將以textarea形式寫入,並以逗號(或其他)分隔。
然後提交按鈕會告訴腳本使用表單中的所有URL來操作,並以任何名稱保存,這並不重要(隨機會很好)。
我還是一個新手,我正在學習PHP。
在此先感謝您的幫助!
編輯
我的代碼看起來像現在這樣:
<?php
error_reporting(E_ALL);
$image_urls = explode('\n', $_POST['urls']);
foreach ($image_urls as $image_url) {
$ch = curl_init();
$timeout = 0;
curl_setopt ($ch, CURLOPT_URL, $image_url);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$image = curl_exec($ch);
curl_close($ch);
$f = fopen('/home1/path/public_html/path/saved/'.rand().time().".jpg", 'w');
fwrite($f, $image);
fclose($f);
}
?>
它僅適用於第一個,並且不返回任何錯誤...任何想法?
感謝您的大力幫助!
存儲個人網址在TXT或DB? – fabrik 2011-03-28 11:35:26
存儲在textarea,除以行... – Charleshaa 2011-03-28 12:43:19
對不起,我完全忽略了這個問題。不知何故,我認爲你想通過cron工作來獲取這些圖像。 – fabrik 2011-03-28 12:46:29