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