如何从wordpress页面上传pdf文件?
<form action="" method="POST">
<input type="file" name="file-upload" id="file-upload" />
<?php
$attachment_id = media_handle_upload('file-upload', $post->ID);
?>
<input type="submit" name="save" value="Submit">
</form>
&#13;
我是第一次这样做,所以请帮忙。
答案 0 :(得分:0)
您可以在模板或任何页面中尝试此代码。我测试并确认其适合我。
<?php
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
$attachment_id = media_handle_upload('file-upload', $post->ID);
?>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="file-upload" id="file-upload" />
<input type="submit" name="upload">
</form>
答案 1 :(得分:0)
Try this :
<?php
function upload_user_file( $file = array() ) {
require_once( ABSPATH . 'wp-admin/includes/admin.php' );
$file_return = wp_handle_upload( $file, array('test_form' => false ) );
if( isset( $file_return['error'] ) || isset( $file_return['upload_error_handler'] ) ) {
return false;
} else {
$filename = $file_return['file'];
$attachment = array(
'post_mime_type' => $file_return['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
'post_content' => '',
'post_status' => 'inherit',
'guid' => $file_return['url']
);
$attachment_id = wp_insert_attachment( $attachment, $file_return['url'] );
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
wp_update_attachment_metadata( $attachment_id, $attachment_data );
if( 0 < intval( $attachment_id ) ) {
return $attachment_id;
}
}
return false;
}
?>
<?php
if( ! empty( $_FILES ) ) {
foreach( $_FILES as $file ) {
if( is_array( $file ) ) {
$attachment_id = upload_user_file( $file );
}
}
}
?>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="file-upload" id="file-upload" />
<input type="submit" name="upload">
</form>