如何显示jQuery多文件上传插件的文件上传名称

时间:2014-03-06 17:47:31

标签: php jquery file-upload

如何显示jQuery多文件上传插件v1.48的文件上传名称?

用户上传文件后,有没有办法显示他们在个人资料下上传的文件?

1.if ( isset($_FILES['tb-documents']) ) {        
  $documents = $_FILES['tb-documents'];
  foreach ( $documents['name'] as $key => $value ) {
   if ( $documents['name'][$key]) {
    $document = array(
     'name' => $documents['name'][$key],
     'type' => $documents['type'][$key],
     'tmp_name' => $documents['tmp_name'][$key],
     'error' => $documents['error'][$key],
     'size' => $documents['size'][$key]       
     );
        $status = wp_handle_upload($document, array('test_form' => false));

    if( !isset($status['error']) ) {
     $uploads = wp_upload_dir();
     add_user_meta($user->ID, 'tb-documents', $uploads['url'].'/'.basename($status['file']));
    }                 
   }

  }
 }

上传在用户个人资料中运行良好。但是,我想提取他们上传的文件名并显示所有上传的文件名。

我应该使用哪个变量?

1.I tried this, $content .= '.$documents['name']'; but it doesn't work. It shows syntax error.

2 个答案:

答案 0 :(得分:0)

您必须合并此$documents['name'][$key]的结果并将其显示给用户。 例如:

if (isset($_FILES['tb-documents'])) {
    $uploadedFiles = array();
    $documents = $_FILES['tb-documents'];
    foreach ($documents['name'] as $key => $value) {
        if ($documents['name'][$key]) {
            $document = array(
                    'name' => $documents['name'][$key],
                    'type' => $documents['type'][$key],
                    'tmp_name' => $documents['tmp_name'][$key],
                    'error' => $documents['error'][$key],
                    'size' => $documents['size'][$key]
            );
            $status = wp_handle_upload($document, array('test_form' => false));

            if (!isset($status['error'])) {
                $uploads = wp_upload_dir();
                add_user_meta($user->ID, 'tb-documents', $uploads['url'] . '/' . basename($status['file']));
                $uploadedFiles[] = $documents['name'][$key];
            }
        }   
    }

    var_dump(implode(", ", $uploadedFiles));
}

答案 1 :(得分:0)

如果要将上传的文件显示给上传者,可以使用jQuery uploader提供的fileuploaddone回调在JQuery中轻松处理

$('#fileupload')
    .bind('fileuploaddone', function (e, data) {
            console.log( data['result']['files'][0]['name'] );
            console.log( data['result']['files'][0]['url'] );
        });

如果您有多个文件,则需要循环遍历data['result']['files']数组。