3

サイトに ajax ファイルをアップロードするために、ファイル アップローダー jquery プラグインを使用しています。しかし、何がうまくいかないのか、なぜうまくいかないのかわかりません。

プラグイン: http://fineuploader.com/index.html

私のコード:

デモからコードを直接取得しました。

$(document).ready(function() {
$fub = $('#fine-uploader-basic');
$messages = $('#messages');

var uploader = new qq.FileUploaderBasic({
  button: $fub[0],
  action: 'http://localhost/script/file_upload/upload_test',
  debug: true,
  allowedExtensions: ['jpeg', 'jpg', 'gif', 'png'],
  sizeLimit: 204800, // 200 kB = 200 * 1024 bytes
  onSubmit: function(id, fileName) {
    $messages.append('<div id="file-' + id + '" class="alert" style="margin: 20px 0 0"></div>');
  },
  onUpload: function(id, fileName) {
    $('#file-' + id).addClass('alert-info')
                    .html('<img src="client/loading.gif" alt="Initializing. Please hold."> ' +
                          'Initializing ' +
                          '“' + fileName + '”');
  },
  onProgress: function(id, fileName, loaded, total) {
    if (loaded < total) {
      progress = Math.round(loaded / total * 100) + '% of ' + Math.round(total / 1024) + ' kB';
      $('#file-' + id).removeClass('alert-info')
                      .html('<img src="client/loading.gif" alt="In progress. Please hold."> ' +
                            'Uploading ' +
                            '“' + fileName + '” ' +
                            progress);
    } else {
      $('#file-' + id).addClass('alert-info')
                      .html('<img src="client/loading.gif" alt="Saving. Please hold."> ' +
                            'Saving ' +
                            '“' + fileName + '”');
    }
  },
  onComplete: function(id, fileName, responseJSON) {
    if (responseJSON.success) {
      $('#file-' + id).removeClass('alert-info')
                      .addClass('alert-success')
                      .html('<i class="icon-ok"></i> ' +
                            'Successfully saved ' +
                            '“' + fileName + '”' +
                            '<br><img src="img/success.jpg" alt="' + fileName + '">');
    } else {
      $('#file-' + id).removeClass('alert-info')
                      .addClass('alert-error')
                      .html('<i class="icon-exclamation-sign"></i> ' +
                            'Error with ' +
                            '“' + fileName + '”: ' +
                            responseJSON.error);
    }
  }
});
});

HTML:

<div id="fine-uploader-basic" class="btn btn-success">
  <i class="icon-upload icon-white"></i> Click to upload
</div>
<div id="messages"></div>

PHP:

public function upload_test() {
  $upload = move_uploaded_file('./user_files', $_FILES['qqfile']['tmp_name']);
  if($upload) {
    echo json_encode(array('success' => true));
  } else {
    echo json_encode(array('success' => false, 'error' => $upload));
  }
}

問題はPHPにあると思いますが、何が間違っているのかわかりません。気が狂う前に助けてください。

ありがとうございました。

4

6 に答える 6

2

js と html の部分は読んでいませんが、PHP の部分を変更する必要があります。彼のやり方を見て

細かいアップローダー PHP

于 2012-11-18T17:31:51.740 に答える
1

アップロードされたファイルを処理するためのサーバー側のスクリプトに問題を抱えている人をたくさん見てきました。アップロードされたファイルをデータベースに保存する独自のスクリプトを作成する方法を理解するのに時間がかかりました。以下のコード例は、同じ問題が発生しているユーザーにとって非常にわかりやすいものです。

<?php
$file = $_FILES['qqfile'];
$uploadDirectory = 'uploads';
$target = $uploadDirectory.DIRECTORY_SEPARATOR.$file['name'];
$result = null;
if (move_uploaded_file($file['tmp_name'], $target)){
    $result = array('success'=> true);
    $result['uploadName'] = $file['name'];
} else {
    $result = array('error'=> 'Upload failed');
}
header("Content-Type: text/plain");
echo json_encode($result);

もちろん、アップロード ディレクトリの名前を調整する必要があります。以下の JavaScript で問題なく動作するはずです。

$(document).ready(function() {
    var uploader = new qq.FineUploader({
        element: $('#basicUploadSuccessExample')[0],
        debug: true,
        request: {
            endpoint: "server/php/example.php"
        }
    });
});

html にすべての *.js が含まれていることを確認してください。

于 2013-01-25T23:25:44.643 に答える
1

フォルダへのアップロードにも問題がありました。詳細情報を取得するために、qqFileUploader.phpの59行目を次のように変更しました。

if (!is_writable($uploadDirectory) || !is_executable($uploadDirectory))
{
return array('error' =>"Server error. Uploads directory isn't writable or executable. CUR_DIR: " . getcwd() . " DIR: " . $uploadDirectory);
}

それを行った後、ルートディレクトリに並置されたように、phpディレクトリ内のフォルダーを探しているという問題があることがわかりました。

$result = $uploader->handleUpload('up/'); を変更しました。$result = $uploader->handleUpload('../up/'); に 問題は解決しました。

于 2013-01-30T22:33:02.963 に答える
1

削除action: 'http://localhost/script/file_upload/upload_test',して変更する

request: {
            // path to server-side upload script
        endpoint: 'script/file_upload/upload_test/php.php'
         }

また、php.php ファイルでコメントを外します。

$allowedExtensions = array("jpeg", "jpg", "bmp", "png");
$sizeLimit = 10 * 1024 * 1024;
$uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
$result = $uploader->handleUpload('uploads/'); //folder for uploaded files
echo htmlspecialchars(json_encode($result), ENT_NOQUOTES);
于 2012-11-27T11:50:11.147 に答える
1

ここで役立つ場合は、私のセットアップです:

-- jquery.html

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8">
        <title>Fine Uploader Demo</title>
        <link href="client/fineuploader.css" rel="stylesheet">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
      </head>
      <body>
        <div id="fine-uploader"></div>

        <script src="client/jquery.fineuploader-3.1.1.min.js"></script>
        <script>
          function createUploader() {
            var uploader = new qq.FineUploader({
              debug: true,  
              // Pass the HTML element here
              element: document.getElementById('fine-uploader'),
              // or, if using jQuery
              // element: $('#fine-uploader')[0],
              // Use the relevant server script url here
              // if it's different from the default “/server/upload”
              request: {
                endpoint: 'php.php'

              }
            });
          }

          window.onload = createUploader;
        </script>
      </body>
    </html>

--- php.php (jquery.html と同じフォルダ内)

次の行はコメント解除されています。

    // list of valid extensions, ex. array("jpeg", "xml", "bmp")
    $allowedExtensions = array();
    // max file size in bytes
    $sizeLimit = 10 * 1024 * 1024;
    //the input name set in the javascript
    $inputName = 'qqfile';

    $uploader = new qqFileUploader($allowedExtensions, $sizeLimit, $inputName);

    $result = $uploader->handleUpload('uploads/');
    header("Content-Type: text/plain");
    echo htmlspecialchars(json_encode($result), ENT_NOQUOTES);

アップロード フォルダーには 777 のアクセス許可があります

MAMP 注: アップロード時に stream_copy_to_stream() エラーがスローされます。解決策: php.php の 58 行目を $temp = tmpfile(); から変更します。to $temp = fopen("php://temp", "wb"); //$temp = tmpfile();

これが誰かに役立つことを願っています。

于 2013-01-09T09:59:54.703 に答える
0

「ファイルアップローダー」だと思いますか?

PHPコードにエラー処理を追加するなど、基本的なデバッグを行いましたか?あなたのコードは複数のレベルで壊れています:

1)move_upload_fileには、宛先FILE名が必要です。ディレクトリのみを提供しています。

move_uploaded_file('/path/to/destination/on/server.txt', $_FILES['qqfile']['tmp_name']);

2)アップロードが実際に成功したことを確認していません。

if($_FILES['qqfile']['error'] !== UPLOAD_ERR_OK) {
     die("UPload failed with error code " . $_FILES['qqfile']['error']);
}

エラーコードはここで定義されています:http://php.net/manual/en/features.file-upload.errors.php

于 2012-11-09T20:11:12.620 に答える