-1

コンテンツ (画像) を自分の Web サイトに配置するクローラーを作成しています。URL をクロールして画像に保存するスクリプトを作成しましたが、画像をサーバーにアップロードして、自分のサーバーで使用できるようにする必要があります。 Webサイト。

file_put_contents を使用する必要があると言っている人を見たことがありますが、画像名と拡張子を指定する必要がありますが、動的になりますか?

4

2 に答える 2

1

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);
于 2012-08-16T17:17:59.633 に答える
0

file_get_contentsandを使用するの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);
}

ドキュメンテーション

于 2012-08-16T17:20:03.487 に答える