コンテンツ (画像) を自分の Web サイトに配置するクローラーを作成しています。URL をクロールして画像に保存するスクリプトを作成しましたが、画像をサーバーにアップロードして、自分のサーバーで使用できるようにする必要があります。 Webサイト。
file_put_contents を使用する必要があると言っている人を見たことがありますが、画像名と拡張子を指定する必要がありますが、動的になりますか?
コンテンツ (画像) を自分の Web サイトに配置するクローラーを作成しています。URL をクロールして画像に保存するスクリプトを作成しましたが、画像をサーバーにアップロードして、自分のサーバーで使用できるようにする必要があります。 Webサイト。
file_put_contents を使用する必要があると言っている人を見たことがありますが、画像名と拡張子を指定する必要がありますが、動的になりますか?
file_get_contents()およびfile_put_contents( )を使用できます
$image = 'http://example.org/image.jpg';
$filename = basename($image);
$content = file_get_contents($image);
file_put_contents('/path/to/your/dir/'.$filename, $content);
file_get_contents
andを使用するのfile_put_contents
がおそらく最も簡単な方法です。
衝突 (同じ名前のファイル) を避けるために、ファイル名の url 部分を作成できます。
URL の配列を想定すると、次のようになります。
$path = '/path/to/image/folder/';
$urls = array(
'http://www.somesite.no/some_image.jpg',
'http://www.someothersite.no/some_other_image.jpg',
'http://www.someothersite.no/another_image.jpg'
);
foreach ($urls as $url) {
$file_content = file_get_contents($url);
$filename = preg_replace("/[^a-zA-Z0-9 ]/", "", $url);
$filename = str_replace(" ", "-", $filename);
file_put_contents($path.$filename, $file_content);
}
ドキュメンテーション
file_get_contents
- http://www.php.net/manual/en/function.file-get-contents.phpfile_put_contents
- http://www.php.net/manual/en/function.file-put-contents.phppreg_replace
- http://php.net/manual/en/function.preg-replace.phpstr_replace
- http://php.net/manual/en/function.str-replace.php