doulong4169 2017-06-23 11:12
浏览 11

Wordpress文件上传

I have this plugin im making and in the file upload system i have this:

$mimes = array('image/jpeg','image/jpg','image/gif','image/png','application/pdf');
if(in_array($_FILES['attach']['type'], $mimes)){
    $error = 0;
}
else {
    $error = 1;
}

Then, along with other error checking i have this to upload the files to a custom folder

if($error == 0) {
    $folder = PLUGIN_DIR . '/uploads/';
    if(is_dir($folder)) {
        $file = $_FILES["attach"]["tmp_name"];
        move_uploaded_file($file, $folder.date('Ymd').'_'.$name);
    }
}

This works perfectly. I've tested it but, is it ok to do like this? Or is there a better way to do it?

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • douliao8402 2017-06-23 11:23
    关注

    I think better use this codex.wordpress.org

    <?php
    // We will check the protection of nonce and that the user can edit this post.
    if ( 
        isset( $_POST['my_image_upload_nonce'], $_POST['post_id'] ) 
        && wp_verify_nonce( $_POST['my_image_upload_nonce'], 'my_image_upload' )
        && current_user_can( 'edit_post', $_POST['post_id'] )
    ) {
        // all OK! We continue.
        // These files must be connected to the front end (front end).
        require_once( ABSPATH . 'wp-admin/includes/image.php' );
        require_once( ABSPATH . 'wp-admin/includes/file.php' );
        require_once( ABSPATH . 'wp-admin/includes/media.php' );
    
        // Let WordPress catch the download.
        // Do not forget to specify the attribute name field input - 'my_image_upload'
        $attachment_id = media_handle_upload( 'my_image_upload', $_POST['post_id'] );
    
        if ( is_wp_error( $attachment_id ) ) {
            echo "Error loading media file.";
        } else {
            echo "The media file has been successfully uploaded!";
        }
    
    } else {
        echo "Verification failed. Unable to load file.";
    }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序