2017-08-14 38 views
3

我試圖使用URL文件(PNG)上傳到我的網頁目錄:將文件上傳到我的網頁目錄

$url = "http://api.qrserver.com/v1/create-qr-code/?data=hello_word&size=100x100"; 
$file = file_get_contents($url); 
$fileName = "filename"; 
$dir = $this->get('kernel')->getRootDir() . '/../web/uploads/img'; 
$file->move($dir , $fileName); 

警告:的file_get_contents(http://api.qrserver.com/v1/create-qr-code/?data=hello_word&size=100x100):未能打開流: HTTP請求失敗! HTTP/1.1 400錯誤的請求

回答

1
$fileName = "filename"; 
$dir = $this->get('kernel')->getRootDir() . 
'/../web/uploads/img'.$fileName; 
$ch = curl_init('http://api.qrserver.com/v1/create-qr-code/? 
data=hello_word&size=100x100'); 
$fp = fopen($dir, 'wb'); 
curl_setopt($ch, CURLOPT_FILE, $fp); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_exec($ch); 
curl_close($ch); 
fclose($fp); 

嘗試類似的東西。如果這不起作用,我會考慮使用。 http://php.net/manual/en/function.file-put-contents.php

+0

它是如何轉換我的文件,因爲我應該有一個PNG文件 – Achraf

0
  1. 功能file_get_contents將返回一個字符串。
  2. 在將URL傳遞給file_get_contents之前,您需要使用http://php.net/manual/en/function.urlencode.php函數對您的URL進行編碼。
  3. 在php.net文檔中檢查file_put_contents函數。