0

問題:

jQuery File Upload を使用して .txt ファイルのアップロードが完了したら、セッション変数を設定し、ユーザーを別の PHP ページにリダイレクトします。

HTML コード (upload.php):

<!-- The fileinput-button span is used to style the file input field as button -->
<span class="btn btn-success fileinput-button">
    <i class="glyphicon glyphicon-plus"></i>
    <span>Add files...</span>
    <!-- The file input field used as target for the file upload widget -->
    <input id="fileupload" type="file" name="files[]" multiple>
</span>
<br>
<br>
<!-- The global progress bar -->
<div id="progress" class="progress">
    <div class="progress-bar progress-bar-success"></div>
</div>
<!-- The container for the uploaded files -->
<div id="files" class="files"></div>

jQuery コード (upload.php):

<script>    
    $(function () {
        'use strict';
        // Server-side upload handler:
        var url = 'process.php';

        $('#fileupload').fileupload({
            url: url,
            autoUpload: true,
            acceptFileTypes: /(\.|\/)(txt)$/i,
            maxFileSize: 5000000, // 5 MB
            done: function (e, data) {
                $(this).delay(2000, function(){
                    window.location = "explorer.php";
                });
            },
            progressall: function (e, data) {
                var progress = parseInt(data.loaded / data.total * 100, 10);
                $('#progress .progress-bar').css(
                    'width',
                    progress + '%'
                );
            }
        }).prop('disabled', !$.support.fileInput)
            .parent().addClass($.support.fileInput ? undefined : 'disabled');
    });
</script>

PHP アップロード スクリプト (process.php):

<?php
    session_start();

    $folder      = 'upload';

    if (!empty($_FILES))
    {
        // Set temporary name
        $tmp    = $_FILES['files']['tmp_name'];

        // Set target path and file name
        $target = $folder . '/' . $_FILES['files']['name'];

        // Upload file to target folder
        $status = move_uploaded_file($tmp, $target);

        if ($status)
        {
            // Set session with txtfile name
            $_SESSION['txtfile'] = $_FILES['files']['name'];
        }
    }
?>

望ましい出力:

  • テキスト ファイルはフォルダー /upload にアップロードする必要があります。現在、このフォルダーには chmod 777 があります。
  • テキストファイル名のセッションは、変数 $_SESSION['txtfile'] に割り当てる必要があります
  • ファイル「explorer.php」へのアップロードが完了したら、ユーザーをリダイレクトします

編集:解決しました。上記の最終コード!

4

1 に答える 1

3

手始めに...

inputあなたの名前はfiles[]であり、属性が含まれていることに注意してくださいmultiple。これは、ファイルの配列をサーバーに送信していることを意味するため、php でそれらを参照するには、次のようなものが必要になります。

$_FILES['files']['name'][0]

最初のファイルの場合。

move_uploaded_file()また、宛先のフル パスが好きであることがわかりました$_SERVER['DOCUMENT_ROOT']

情報をjQueryに送り返すには、次echo json_encode()のように使用します...

echo json_encode(array(
    'status' => $status,
    'message' => 'your message here'
));

関数では、done次のようにデータにアクセスできます。

done: function(e, data){
     console.log(data.status);
     console.log(data.message);
}
于 2013-10-21T21:04:50.237 に答える