![](/img/trans.png)
[英]Created new Symfony 3.4 project using Flex - Is this really Symfony 3.4?
[英]Url to image using Symfony 3.4
我正在尝试从URL下载图像。 我想将其放入数据库中并保存路径。
我是针对用户上传但未从URL下载的图像执行此操作的。
我所有的代码如下:
控制器( $array_photo
是具有很多URL的数组):
$file = file_get_contents($array_photos[0]);
if ($file != NULL) {
$fileName = md5(uniqid()) . '.' . $file->guessExtension();
$file->move(
$this->getParameter('image_directory'), $fileName
);
}
var_dump($array_photos[0]) -> string(23) "http://photos..."
image_directory是Web中的目录。
当我使用表单执行此操作时(我使用$file = $form->get('avatarPath')->getData();
而不是$file = file_get_contents($array_photos[0]);
,但出现此错误:
在字符串上调用成员函数guessExtension()
如果我用'.png'替换guessExtension,则会出现此错误:
在字符串上调用成员函数move()
文件确实是一个字符串:
$file = file_get_contents($array_photos[0]); // returns string of image data
您只需要使用file_put_contents()
:-)
http://php.net/manual/zh/function.file-put-contents.php
file_put_contents($file, '/path/to/save');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.