【问题标题】:strip url for file_put_contents php去除 file_put_contents php 的 url
【发布时间】:2013-02-17 17:53:38
【问题描述】:

我有一个解析 XML 文件的脚本。它需要将文件中引用的图像保存到本地服务器。我遇到的问题是它试图将图像保存为完整的 url 路径,这会导致 http 错误。这是重要的 XML:

$xml_str = file_get_contents(''.$target_file_path.'');
$xml = new SimpleXMLElement($xml_str);
$items = $xml->xpath('//image1/@src');
foreach($items as $item) {
$content = file_get_contents(''.$url.''.$item.'');
file_put_contents(''.$images_folder_path.''.$item.'', $content);

我需要剥离存储在 $item 中的 url,只在 file_get_contents 和 file_put_contents 之间留下 image.jpg,任何帮助将不胜感激。

编辑:XML 输入是:

<time>13:12</time>
<date>02/15/13 </date>
<image1 src="http://path/to/image.jpeg" height="180" width="144" style="border-color: #800000" border="1" />

图像是远程存储的,我需要将它们复制到本地服务器,因为当脚本到达 file_put_contents 试图将文件另存为时,完整的 url 存储在“item”变量中

/home/server/public_html/folder/http://path/to/image.jpeg

【问题讨论】:

  • 尝试使用 'basename()' 这将去除路径信息,只留下文件名
  • 你能发布一个例子&lt;image1&gt;标签吗?我不确定我是否理解您的意见。

标签: php xml xml-parsing


【解决方案1】:

怎么样:

basename("http://www.example.com/picture.jpg");

它将输出picture.jpg

【讨论】:

  • 关于如何在 file_put_contents 之前从变量 $item 中删除它的任何想法
猜你喜欢
  • 1970-01-01
  • 2015-12-14
  • 1970-01-01
  • 2023-03-05
  • 2012-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多