简体   繁体   English

将文件上传到我的网站目录

[英]upload a file to my web directory

I'm trying to upload a file (png) to my web directory using an URL: 我正在尝试使用URL将文件(png)上传到我的web目录:

$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);

Warning: file_get_contents( http://api.qrserver.com/v1/create-qr-code/?data=hello_word&size=100x100 ): failed to open stream: HTTP request failed! 警告:file_get_contents( http://api.qrserver.com/v1/create-qr-code/?data=hello_word&size=100x100 ):无法打开流:HTTP请求失败! HTTP/1.1 400 Bad Request 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);

Try something like that. 尝试类似的东西。 If that doesn't work i'd consider using. 如果这不起作用我会考虑使用。 http://php.net/manual/en/function.file-put-contents.php http://php.net/manual/en/function.file-put-contents.php

  1. Function file_get_contents will return a string. 函数file_get_contents将返回一个字符串。
  2. You need to use http://php.net/manual/en/function.urlencode.php function to encode your URL before passing it to file_get_contents . 您需要使用http://php.net/manual/en/function.urlencode.php函数对URL进行编码,然后再将其传递给file_get_contents
  3. Check file_put_contents function in php.net docs. 检查php.net文档中的file_put_contents函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM