0
目前我正嘗試將圖像上傳到服務器端的Wordpress-Multisite。 (圖像已被另一條路徑在同一臺服務器)WordPress多站點wp_upload_dir錯誤
我寫了一個小PHP腳本應該處理這個問題:
$upload_dir = wp_upload_dir();
var_dump($upload_dir);
$filename = basename($imagepath);
if (wp_mkdir_p($upload_dir['path'])) {
$file = $upload_dir['path'].'/'.$filename;
} else {
$file = $upload_dir['basedir'].'/'.$filename;
}
copy($imagepath, $file);
$wp_filetype = wp_check_filetype($filename, null);
$attachment = array(
'guid' => $upload_dir['url'].'/'.basename($filename),
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name($filename),
'post_content' => '',
'post_status' => 'inherit',);
$attach_id = wp_insert_attachment($attachment, $file, $postid);
一切工作正常,但圖像存儲在錯誤的文件夾 wp_upload_dir();
返回公共路徑/wp-content/uploads/2016/03
,不是爲特定子網站的路徑,至極將/wp-content/uploads/sites/SITE_ID/2016/03
後來,當WP採用了圖像時,圖像的URL設置爲http://example.com/wp-content/uploads/sites/SITE_ID/2016/03/IMAGE.JPG
(這是不正確的......)
上傳與內置的WordPress的作品媒體上傳工具正確(文件存儲在/wp-content/uploads/sites/SITE_ID/2016/03
)
你看,wp_upload_dir()
工作不正確的..
感謝文件..