Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 多次从URL保存图像_Php - Fatal编程技术网

Php 多次从URL保存图像

Php 多次从URL保存图像,php,Php,我做了一个爬虫,将把内容(图像)到我的网站上,我已经做了一个脚本,爬虫和保存的url到一个图像罚款,但现在我需要上传到我的服务器,以便我可以在我的网站上使用它们 我看到有人说应该使用文件内容,但您必须指定图像名称和文本,但它将是动态的?您可以使用和 使用file\u get\u contents和file\u put\u contents可能是最简单的方法 为了避免冲突(同名文件),可以将url作为文件名的一部分: 假设URL数组: $path = '/path/to/image/folder/

我做了一个爬虫,将把内容(图像)到我的网站上,我已经做了一个脚本,爬虫和保存的url到一个图像罚款,但现在我需要上传到我的服务器,以便我可以在我的网站上使用它们

我看到有人说应该使用文件内容,但您必须指定图像名称和文本,但它将是动态的?

您可以使用和


使用
file\u get\u contents
file\u put\u 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);
}
文档

  • 文件获取内容
    -
  • 文件内容
    -
  • preg\u replace
    -
  • str\u replace
    -

那么您不是在网站所在的同一台服务器上运行爬虫程序?如果您正在下载和保存,您应该知道下载内容的名称,以便您可以使用该名称进行上传。或者,您可以进行流外上传,只需浏览下载目录,然后上传找到的每个文件。
$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);
}