2012-11-09 3 views
3

내 사이트에 아약스 파일을 업로드하기 위해 파일 업 로더 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 :

내가 그들의 데모에서 직접 코드를했다

나는 문제가 PHP와 같다고 생각하지만 잘못된 일을 이해할 수 없다. 제 정신이 나기 전에 도와주세요.

감사합니다.

+0

정확히 무엇이 문제입니까? –

+0

그것은 정의되지 않은 오류로 파일을 업로드 할 수 없다고 말합니다. 폴더에 올바른 사용 권한이 있습니다. 왜 작동하지 않는지 나는 이해할 수 없다. – user1701398

답변

2

,하지만 당신은 PHP 부분을 변경해야

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

오류 코드는 여기에 정의되어있다. 그는 그것이

fine-uploader PHP

+0

@ RayNicholus, github에있는 php 파일을 참조 하시겠습니까? – Stranger

+1

@ Stranger이 답변은 TianLoon에 의해 기고되었습니다. 원래 링크가 깨 졌으므로 간단히 원래 링크를 업데이트했습니다. –

+0

좋아,하지만 그가 무슨 파일을 말하는지 아십니까? – Stranger

0

"파일 업 로더", 추측 하시겠습니까?

기본 디버깅을 했습니까? 귀하의 PHP 코드에 오류 처리를 추가? 코드가 여러 수준에서 깨졌습니다.

1) move_upload_file에 FILE이라는 이름이 필요합니다. 디렉토리 만 제공하고 있습니다.

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

2) 업로드가 제대로 완료되었는지 확인하지 않았습니다. 내가 UR JS 및 HTML 부분을 읽어 보지 않았 http://php.net/manual/en/features.file-upload.errors.php

+0

아니, 괜찮아요 : D 그가 게시 한 링크를 따라 –

+0

나는 좋은 업 로더를 사용하고 있지만, 나는 당신이 제안한 것을 시도했지만 아무 것도 바뀌지 않았다. 나는 여전히 같은 오류/문제가 발생합니다. – user1701398

1

action: 'http://localhost/script/file_upload/upload_test',을 제거하고 php.php 파일의 주석에서 또한

request: { 
      // path to server-side upload script 
     endpoint: 'script/file_upload/upload_test/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); 
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> 

--- 다음 줄이 주석왔다

합니다 (jquery.html과 같은 폴더에) php.php :

// 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(); ~ $ temp = fopen ("php : // temp", "wb"); // $ temp = tmpfile();

누군가에게 도움이되기를 바랍니다.

+0

을보고 싶습니다.이 예제를 따르지만 fileuploader가 내 UI에 표시되지 않습니다. 왜 그런 일이 일어날 지에 대한 암시가 있습니까? – Stranger

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

물론 업로드 디렉토리의 이름을 조정해야합니다. 그것은 아래의 자바 스크립트와 함께 잘 작동합니다 :

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

그냥 모든 * .js를 HTML에 포함 시켰는지 확인하십시오.

+0

은 'example.php'라는 PHP에 게시 한 것입니까? – Stranger

1

폴더에 업로드하는 데 문제가있었습니다. 나는에 나란히 놓이는으로 문제가 내 PHP는 디렉토리에 폴더를 찾고 있던 것을보고 있음을 수행 한 후

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

자세한 정보를 얻기 위하여 나는 qqFileUploader.php 라인 59로 변경 루트 디렉토리.

변경됨 $ 결과 = $ uploader-> handleUpload ('up /'); ~ $ 결과 = $ uploader-> handleUpload ('../ up /'); 문제가 해결되었습니다.

관련 문제