将文件上传到我的网页目录
问题描述:
我试图使用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错误的请求
答
$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
答
- 功能
file_get_contents
将返回一个字符串。 - 在将URL传递给
file_get_contents
之前,您需要使用http://php.net/manual/en/function.urlencode.php函数对您的URL进行编码。 - 在php.net文档中检查
file_put_contents
函数。
它是如何转换我的文件,因为我应该有一个PNG文件 – Achraf