如何在上传到服务器

时间:2017-04-13 12:42:59

标签: php file-upload

当我使用以下函数$_POST['pk']将图像上传到服务器时,如何在文件名的开头添加user_id(update_customer())。 有了这个,我可以轻松发现特定用户的正确文件。

结果应该是:

  

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();
}

1 个答案:

答案 0 :(得分:-2)

你可以使用

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