简体   繁体   中英

How to add user_id at the beginning of filename when upload to server

How to an add user_id ( $_POST['pk'] ) at the beginning of the filename when I upload an image to server with the below function update_customer() . With this I can easily discover the correct files of a specific user.

Result should be:

filename = $_POST['pk'].'-'.$_POST['name']

function update_customer() {

if(isset($_POST['name']) && isset($_POST['pk'])) {

    //print_r($_FILES['image']);exit;
    $filename = '';
    if(isset($_FILES['image']) && $_FILES['image']['tmp_name'] && $_FILES['image']['error'] == 0) {
        $filename = $_FILES['image']['name'];

        $upload_dir = $_SERVER['DOCUMENT_ROOT'].'/wp-content/uploads/agent_company_logos/';
        $wp_filetype = wp_check_filetype_and_ext( $_FILES['image']['tmp_name'], $_FILES['image']['name'] );

        if($wp_filetype['proper_filename']) 
         $_FILES['image']['name'] = $wp_filetype['proper_filename'];

        if ( ( !$wp_filetype['type'] || !$wp_filetype['ext'] ) ) {
         $arrErrors['file'] = __('File type not allowed', 'agent-plugin');
        }
        else {

         move_uploaded_file($_FILES['image']['tmp_name'], $upload_dir.$_FILES['image']['name']);
        }

        if(!empty($arrErrors) && count($arrErrors) == 0) {
         die (json_encode(array('file' => agent_get_user_file_path($_FILES['image']['name']), 'caption' => '', 'status' => 1)));
        }

        //echo $_FILES['image']['name'];

 }
     $updated = $GLOBALS['wpdb']->update($GLOBALS['wpdb']->prefix.'agent_customer', array('company_logo' => $filename), array('user_id' => $_POST['pk']));                       
    $dir = wp_upload_dir();              
    echo $dir['baseurl'] . '/agent_company_logos/'.$filename; exit(0);

}
echo 0;
die();
}

您可以使用

$basname = $_POST['pk'].basename($file);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM