2017-08-14 52 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函数。