WordPress前端AJAX多文件上传代码示例

作者: 站长 上传时间: 浏览: N/A 下载: N/A 格式: N/A 评分: N/A

第 1 步:新建一个page-ajax-uploader.php空白模板,创建一个页面,页面地址为ajax-uploader,可以访问到http://example.com/ajax-uploader,应该是空白的页面

第 2 步:编辑page-ajax-uploader.php


>




第 3 步:在wp_header中加载JQ(这个就不详讲了)

第 4 步:functions.php中添加以下代码

add_action('wp_ajax_cvf_upload_files', 'cvf_upload_files');
add_action('wp_ajax_nopriv_cvf_upload_files', 'cvf_upload_files'); // Allow front-end submission

function cvf_upload_files(){

$parent_post_id = isset( $_POST['post_id'] ) ? $_POST['post_id'] : 0; // The parent ID of our attachments
$valid_formats = array("jpg", "png", "gif", "bmp", "jpeg"); // Supported file types
$max_file_size = 1024 * 500; // in kb
$max_image_upload = 10; // Define how many images can be uploaded to the current post
$wp_upload_dir = wp_upload_dir();
$path = $wp_upload_dir['path'] . '/';
$count = 0;

$attachments = get_posts( array(
'post_type' => 'attachment',
'posts_per_page' => -1,
'post_parent' => $parent_post_id,
'exclude' => get_post_thumbnail_id() // Exclude post thumbnail to the attachment count
) );

// Image upload handler
if( $_SERVER['REQUEST_METHOD'] == "POST" ){

// Check if user is trying to upload more than the allowed number of images for the current post
if( ( count( $attachments ) + count( $_FILES['files']['name'] ) ) > $max_image_upload ) {
$upload_message[] = "Sorry you can only upload " . $max_image_upload . " images for each Ad";
} else {

foreach ( $_FILES['files']['name'] as $f => $name ) {
$extension = pathinfo( $name, PATHINFO_EXTENSION );
// Generate a randon code for each file name
$new_filename = cvf_td_generate_random_code( 20 ) . '.' . $extension;

if ( $_FILES['files']['error'][$f] == 4 ) {
continue;
}

if ( $_FILES['files']['error'][$f] == 0 ) {
// Check if image size is larger than the allowed file size
if ( $_FILES['files']['size'][$f] > $max_file_size ) {
$upload_message[] = "$name is too large!.";
continue;

// Check if the file being uploaded is in the allowed file types
} elseif( ! in_array( strtolower( $extension ), $valid_formats ) ){
$upload_message[] = "$name is not a valid format";
continue;

} else{
// If no errors, upload the file...
if( move_uploaded_file( $_FILES["files"]["tmp_name"][$f], $path.$new_filename ) ) {

$count++;

$filename = $path.$new_filename;
$filetype = wp_check_filetype( basename( $filename ), null );
$wp_upload_dir = wp_upload_dir();
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
'post_content' => '',
'post_status' => 'inherit'
);
// Insert attachment to the database
$attach_id = wp_insert_attachment( $attachment, $filename, $parent_post_id );

require_once( ABSPATH . 'wp-admin/includes/image.php' );

// Generate meta data
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );

}
}
}
}
}
}
// Loop through each error then output it to the screen
if ( isset( $upload_message ) ) :
foreach ( $upload_message as $msg ){
printf( __('

%s

', 'wp-trade'), $msg );
}
endif;

// If no error, show success message
if( $count != 0 ){
printf( __('

%d files added successfully!

', 'wp-trade'), $count );
}

exit();
}

// Random code generator used for file names.
function cvf_td_generate_random_code($length=10) {

$string = '';
$characters = "23456789ABCDEFHJKLMNPRTVWXYZabcdefghijklmnopqrstuvwxyz";

for ($p = 0; $p < $length; $p++) { $string .= $characters[mt_rand(0, strlen($characters)-1)]; } return $string; }

其他:js可以修改下,可以存到wp的媒体库,也可以存到指定的位置