2012-08-06 92 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