2012-08-06 104 views
0

我正在为Wordpress网站创建用户提交的发布工具。从URL上传图片到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()或类似,但具有URL是一回事吗?

+0

http://stackoverflow.com/questions/2003996/how-can-i-upload-an-image-from-a-url-in-php – mittmemo 2012-08-06 17:06:56

回答

0

我认为你可以使用file_get_contents()file_put_contents()。你可能需要这样做:

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

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

+0

感谢您的帮助。我已经成功将该文件复制到我的服务器,但我不知道如何执行第二部分:修改代码以将URL视为$ _FILES。我喜欢'$ file = get_file($ image_url)'。然后我可以访问它的属性,如'$ file ['name']'。 – podcastfan88 2012-08-12 00:36:42

+0

其实有这样的功能。查看[fileinfo_file()](http://www.php.net/manual/en/function.finfo-file.php)。如果您使用PHP> = 5.3,它已经可用。如果没有,请检查[this](http://www.php.net/manual/en/book.fileinfo.php)并按照安装说明进行操作。 – Edenbauer 2012-08-13 03:21:03