从URL上传图像到Wordpress

时间:2012-08-06 16:54:11

标签: wordpress

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

用户可以输入图片网址,我希望将其与标题和帖子内容等其他内容一起添加到帖子中。

我使用常规图像附件工作了。我创建帖子,然后使用此脚本附加图像:

 $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()或类似但使用网址做同样的事情?

1 个答案:

答案 0 :(得分:0)

我认为您可以使用file_get_contents()file_put_contents()。你可能不得不做这样的事情:

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

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