dongtiao0657 2018-07-31 05:19
浏览 277

Wordpress - wp_handle_upload - 如何获取上传文件的文件名/目录?

I am working on a WordPress website which allows users to upload files upon registration. I got the upload and registration portion working. Below is the code that handles the upload once the user clicks the register button:

function text_domain_woo_save_reg_form_fields($customer_id) {
    //First name field
    if (isset($_POST['billing_first_name'])) {
        update_user_meta($customer_id, 'first_name', sanitize_text_field($_POST['billing_first_name']));
        update_user_meta($customer_id, 'billing_first_name', sanitize_text_field($_POST['billing_first_name']));
    }
    //Last name field
    if (isset($_POST['billing_last_name'])) {
        update_user_meta($customer_id, 'last_name', sanitize_text_field($_POST['billing_last_name']));
        update_user_meta($customer_id, 'billing_last_name', sanitize_text_field($_POST['billing_last_name']));
    }
            // $target_path = "c:/"; 
        // $target_path = $target_path.basename( $_FILES['image_verification']['name']); 
        // echo " <script>console.log('it hit!!'); </script>";
        // echo " <script>console.log('image upload - success! " . $target_path . "' ); </script>";
        // if(move_uploaded_file($_FILES['image_verification']['tmp_name'], $target_path)) { 
            // echo "File uploaded successfully!";
            // echo " <script>console.log('image upload - success!'); </script>";
            // echo " <script>console.log('image upload - success! " . $target_path . "' ); </script>";
        // } else { 
            // echo "Sorry, file not uploaded, please try again!"; 
            // echo " <script>console.log('image upload - fail!'); </script>";
            // echo " <script>console.log('image upload - success! " . $target_path . "' ); </script>";
        // } 

    if ( ! function_exists( 'wp_handle_upload' ) ) {
        require_once( ABSPATH . 'wp-admin/includes/file.php' );
    }

    $uploadedfile = $_FILES['image_verification'];
    $movefile = wp_handle_upload($uploadedfile, array('test_form' => false));

    if ($movefile && !isset($movefile['error'])) {
        var_dump($movefile);
    } else {
        echo $movefile['error'];
    }

}

The wp_handle_upload moves the file the user uploads into the C:\wamp64\www\wordpress\wp-content\uploads\2018\07\imgname.png directory.

now my concern here is:

if 2 users register and they both upload the file imgname.png, it will rename the second "imgname.png" to "imgname-1.png". This is perfectly fine but I am trying to also store a reference to these images in the database, how would I be able to know if the image got renamed or not?

Do you guys have any suggestions for this?

Example: UserA signing up uploads a file called "test.png", i want to create a record in the db like this:

|UserA|localhost\wordpress\wp-content\uploads\2018\07\test.png.|

Now UserB signing up uploads a file called "test.png", his file will be renamed "test-1.png". Therefore I would need to create a record in the DB for him as the following:

|UserA|localhost\wordpress\wp-content\uploads\2018\07\test-1.png.|

How would I get the directory/image name of the file if the change to "test 1.png" happened after it was uploaded?

Any suggestions?

  • 写回答

1条回答 默认 最新

  • dongpao9165 2018-08-01 03:34
    关注

    To access the new file name, you can use the php function basename() and give it the path returned from the wp_handle_upload() function and compare it to the file name that was uploaded.

    $movefile = wp_handle_upload($uploadedfile, array('test_form' => false));
    $new_filename = basename( $movefile['file'] );
    $filename_was_changed = new_filename !== $_FILES['image_verification']['name'];
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果