Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从URL向Wordpress上传图像_Wordpress - Fatal编程技术网

从URL向Wordpress上传图像

从URL向Wordpress上传图像,wordpress,Wordpress,我正在为Wordpress网站创建一个用户提交的帖子工具 用户可以输入一个图像URL,我希望它与其他内容(如标题和帖子内容)一起添加到帖子中 我已经用一个普通的图像附件工作了。我创建帖子,然后使用以下脚本附加图像: $files = $_FILES['upload_attachment']; $file = array( 'name' => $files['name'], 'type' => $files['type'], 'tmp_name' =>

我正在为Wordpress网站创建一个用户提交的帖子工具

用户可以输入一个图像URL,我希望它与其他内容(如标题和帖子内容)一起添加到帖子中

我已经用一个普通的图像附件工作了。我创建帖子,然后使用以下脚本附加图像:

 $files = $_FILES['upload_attachment'];

 $file = array(
    'name' => $files['name'],
    'type' => $files['type'],
    'tmp_name' => $files['tmp_name'],
    'error' => $files['error'],
    'size' => $files['size']
);

$_FILES = array("upload_attachment" => $file);

foreach ($_FILES as $file => $array) {
     $newupload = insert_attachment($file,$userPost);
    }
但是如何使用
insert_attachment()
或类似的URL来做同样的事情呢?

我认为您可以使用和。您可能必须执行以下操作:

file_put_contents('your_file_on_server.xxx', file_get_contents('http://someurl.com/filename.xxx'));

这将从指定的url下载文件并将其保存到服务器。从那里,您可以稍微修改原始代码,将其附加到帖子。

谢谢您的帮助。我已成功地将该文件复制到服务器,但我不确定如何执行第二部分:修改代码以将URL视为$\u文件。我想要像
$file=get\u file($image\u url)
这样的东西。然后我可以访问它的属性,比如
$file['name']
。实际上有这样一个函数。退房如果您使用的是PHP>=5.3,那么它已经可用了。如果没有,请检查并按照安装说明进行操作。