我想将xml文件(fabfurnish.xml)中的所有图像下载到本地文件夹(输出)。根据@user1978142编辑下面的代码后,我得到了想要的结果。谢谢。
<?php
$xml = simplexml_load_file('input/fabfurnish.xml'); // your xml
foreach($xml->product as $url) {
$url = (string) $url->image;
$filename = basename($url); // get the filename
if(file_exists($filename)) {
echo "file <strong>$filename</strong> already exists <br/>";
} else {
$img = file_get_contents($url); // get the image from the url
file_put_contents('output/'.$filename, $img); // create a file and feed the image
echo "file <strong>$filename</strong> created <br/>";
}
}
?>
这是我正在使用的实际xml文件:
https://www.wetransfer.com/downloads/a88c2605cf038f8cc72603a0cf33904620140711034115/2ce9841bc9fd57063a0919de5f46862120140711034115/c1adbb实际上,这很简单。示例:
$xml = simplexml_load_file('fabfurnish.xml'); // your xml
foreach($xml->product as $url) {
$url = (string) $url->image;
$filename = basename($url); // get the filename
if(file_exists($filename)) {
echo "file <strong>$filename</strong> already exists <br/>";
} else {
$img = file_get_contents($url); // get the image from the url
file_put_contents($filename, $img); // create a file and feed the image
echo "file <strong>$filename</strong> created <br/>";
}
}
如果您希望像示例一样解析特定的xml文件,您可以在getImagesFromXML($xmlurl)中编辑以下代码:
foreach ($xml->product as $product) {
foreach($product->image as $url) {
$tmp = explode("/", $url);
save($url, end($tmp), "output/");
}
}