0
我試圖在我的WordPress網站的前端創建自己的自定義圖像/文件上傳。當我上傳文件時,它會到達我的數據庫,但是在媒體庫中,文件沒有完全上傳,當我檢查我的wp-content/uploads文件夾時,沒有任何內容可以解決這個問題。wp_insert_attachment不上載圖像到媒體庫或上傳文件夾
在媒體庫文件的圖像 unfinished upload file in Media Library
<form method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="img-upload" id="img-upload" />
<input type="submit" value="Upload Image" name="submit">
</form>
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
$image = $_POST['img-upload'];
$upload_dir = wp_upload_dir();
$file_name = basename($image);
//$file_data = file_get_contents($file);
$file_dir = $upload_dir['basedir'] . "/" . basename($image);
$file_type = wp_check_filetype($image,null);
$attachment = array(
'guid' => $file_dir,
'post_mime_type' => $file_type['type'],
'post_title' => preg_replace('/\\.[^.\\s]{3,4}$/', '', $image),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment($attachment, $image,0);
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata($attach_id, $file_dir);
wp_update_attachment_metadata($attach_id, $attach_data);
可以提供'的print_r($ _ FILES)的內容;'? – user2914191
當我使用var_dump或print_r作爲$ _FILES @ user2914191時,沒有打印出來 –
找出你爲什麼沒有在'$ _FILES'中獲取任何東西。 https://stackoverflow.com/questions/3586919/why-would-files-be-empty-when-uploading-files-to-php只是讓你知道,當你提交表單時,'$ _FILES'應該只包含數據並上傳一個文件。 – user2914191