【问题标题】:PHP can't copy remote filePHP无法复制远程文件
【发布时间】:2017-04-02 04:29:12
【问题描述】:

我正在尝试使用 PHP copy() 函数将远程文件复制到本地路径。

 // https://d47lev3ki2x1o.cloudfront.net/5804aaa8-e5a8-484a-9c3b-4a72c0a83865-1.jpg
 $imageURL = Configure::read('cloudfront') . $photo['image_location'];
 $localURL =  $this->webroot . 'img/tmp/' . $photo['image_location'];


 if(!@copy($imageURL, $localURL)) {
     $errors= error_get_last();
     echo "COPY ERROR: ".$errors['type'];
     echo "<br />\n".$errors['message'];
 } else {
     echo "File copied from remote!";
 }

它一直给我错误:

复制错误:2 复制(/baker-and-co/img/tmp/5804a6a8-9c78-49f2-88cc-49f5c0a83865-1.jpg): 无法打开流:没有这样的文件或目录

tmp 目录已经存在于我的本地,所以我看不出这里有什么问题。我也将 allow_url_fopen 设置为 true。

有什么想法吗?

【问题讨论】:

  • 你当前的文件夹是什么?
  • 我使用的是CakePHP MVC结构,所以我在/View/Reports/admin_view.ctp,文件夹位于/webroot/img/tmp

标签: php file copy


【解决方案1】:

您可以尝试创建一个新文件,因为您无法复制远程文件。

 $imageURL = Configure::read('cloudfront') . $photo['image_location'];
 $localURL =  $this->webroot . 'img/tmp/' . $photo['image_location'];
if(!file_put_contents($localURL, file_get_contents($imageURL));) {
 $errors= error_get_last();
 echo "COPY ERROR: ".$errors['type'];
 echo "<br />\n".$errors['message'];
} else {
 echo "File copied from remote!";
}

【讨论】:

  • 你当然可以复制远程文件,见docs,这段代码给出了同样的错误
  • 该链接正是我尝试过的,copy() 可以将 URL 用于源文件,但不能用于目标。所以这再次没有帮助,因为我正在从 URL 复制到我的本地文件路径
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-03
相关资源
最近更新 更多