php - 从 url 上传文件
问题描述
大家好 :) 我如何从 url 上传文件?(我读了很多关于这个的问题,但没有任何工作......)这是我的代码:
$url = 'https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png';
$file = file_get_contents($url);
if ($file != NULL) {
$fileName = md5(uniqid()) . '.' . $file->guessExtension();
$file->move(
$this->getParameter('image_directory'), $fileName
);
}
在这里,我只想从 url 下载图像并将其移动到一个目录(之后我会将路径保存在数据库中),但是使用此代码我有这个错误:
调用字符串上的成员函数guessExtension()
解决方案
file_get_contents
是读取文件的内容(txt、xml 等)。
相反,您应该使用 linux command wget
。您可以使用 PHP 函数运行它exec
。
$url = 'https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png';
//Execute wget
$file=exec("wget http://www.example.com/file.xml");
if ($file != NULL) {
$fileName=md5(uniqid()).'.'.$file->guessExtension();
$file->move($this->getParameter('image_directory'), $fileName);
}
推荐阅读
- python-3.x - 如何在函数中使用单选按钮值?
- docker - docker中的upower在启动时让我出现奇怪的错误
- python - 当 Python “None or True”返回 True 时,为什么 pandas “None | True”返回 False?
- ruby-on-rails - json中没有附件路径,连接表
- ubuntu - Ubuntu 20.04 L2TP 连接问题
- r - 计算R中数据集错误中定义值的标准偏差
- c# - 使用数据模板禁用组合框
- c++ - 如果一个单词在一个字符串中重复多次,我如何计算单词的重复次数及其位置?
- ruby-on-rails - Rails:使用 Devise 在一个控制器中验证两种用户类型?
- ansible - Ansible - 将外部变量设置为变量名称