2016-11-18 126 views
2

我想使用PHP copy()函數將遠程文件複製到本地路徑。PHP無法複製遠程文件

// 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([email protected]($imageURL, $localURL)) { 
    $errors= error_get_last(); 
    echo "COPY ERROR: ".$errors['type']; 
    echo "<br />\n".$errors['message']; 
} else { 
    echo "File copied from remote!"; 
} 

它不斷給我的錯誤:

COPY ERROR: 2 copy(/baker-and-co/img/tmp/5804a6a8-9c78-49f2-88cc-49f5c0a83865-1.jpg): failed to open stream: No such file or directory

tmp目錄已經存在於我的地方,所以我不認爲這個問題是在這裏。我也有allow_url_fopen設置爲true。

任何想法?

+0

什麼是您當前的文件夾? –

+0

我正在使用CakePHP MVC結構,因此我在'/ View/Reports/admin_view.ctp'文件夾位於'/ webroot/img/tmp' – user3574492

回答

0

您可以嘗試此操作以創建新文件,因爲您無法複製遠程文件。

$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!"; 
} 
+0

您當然可以複製遠程文件,請參閱[文檔] (http://php.net/manual/en/function.copy.php),這段代碼給出了相同的錯誤 – user3574492

+0

http://stackoverflow.com/questions/9843933/copy-file-from-remote-server-or -url –

+0

該鏈接正是我所嘗試的,copy()可以使用源文件的URL而不是目的地。所以再次這沒有幫助,因爲我從URL和我的本地文件路徑複製 – user3574492