簡體   English   中英

使用AS3和PHP將文件上傳到服務器時出錯

[英]uploading files to server using AS3 and PHP error

我正在嘗試使用AS3和PHP將文件上傳到服務器。 這是我的AS3代碼,然后是PHP代碼。 我嘗試上傳到的文件夾可寫。 文件大小約為20Kb。 php腳本在我的服務器上,並且flash文件將其調用。

var UPLOAD_URL: String ="linktophpscriptonMysite"
var fr: FileReference;
var request: URLRequest = new URLRequest();
request.url = UPLOAD_URL;

function startThis(): void {

     fr = new FileReference();
     fr.addEventListener(Event.SELECT, selectHandler);
     fr.addEventListener(Event.OPEN, openHandler);
     fr.addEventListener(ProgressEvent.PROGRESS, progressHandler);
     fr.addEventListener(Event.COMPLETE, completeHandler);
     fr.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
     startUpload()
}

function startUpload(): void {
    try {
        var success: Boolean = fr.browse();
        trace("success")
    } catch (error: Error) {
        trace("Unable to browse for files.", Error);
    }
}

function progressHandler(event: ProgressEvent): void {
    trace(event.bytesLoaded, event.bytesTotal);

}
function ioErrorHandler(event: IOErrorEvent): void {
    //trace("Some error ", event.target.data.systemResult);
    //systemResult is echoed by PHP

}
function openHandler(event: Event): void {
    try {
        //var success: Boolean = fr.browse();
    } catch (error: Error) {
        trace("Unable to browse for files.", Error);
    }

}
function completeHandler(event: Event): void {
    trace(event.target.data.systemResult);
//this reads the result, again, from PHP echo "systemResult=all is good";


}

function selectHandler(event: Event): void {

    fr.upload(request);
}

然后,這是php代碼:此代碼是我在php手冊網站上找到的常規上傳腳本

   <?php

header('Content-Type: text/plain; charset=utf-8');

try {


    // Undefined | Multiple Files | $_FILES Corruption Attack
    // If this request falls under any of them, treat it invalid.
    if (
        !isset($_FILES['upfile']['error']) ||
        is_array($_FILES['upfile']['error'])
    ) {
         echo "systemResult=Error";
        throw new RuntimeException('Invalid parameters.');

    }

    // Check $_FILES['upfile']['error'] value.
    switch ($_FILES['upfile']['error']) {
        case UPLOAD_ERR_OK:
            break;
        case UPLOAD_ERR_NO_FILE:
            throw new RuntimeException('No file sent.');

        case UPLOAD_ERR_INI_SIZE:
        case UPLOAD_ERR_FORM_SIZE:
            throw new RuntimeException('Exceeded filesize limit.');
        default:
            throw new RuntimeException('Unknown errors.');
    }

    // You should also check filesize here. max is 100 mb
    if ($_FILES['upfile']['size'] > 10000000) {
        throw new RuntimeException('Exceeded filesize limit.');
    }

    // DO NOT TRUST $_FILES['upfile']['mime'] VALUE !!
    // Check MIME Type by yourself.
    $finfo = new finfo(FILEINFO_MIME_TYPE);
    if (false === $ext = array_search(
        $finfo->file($_FILES['upfile']['tmp_name']),
        array(
            'jpg' => 'image/jpeg',
            'png' => 'image/png',
            'gif' => 'image/gif',
        ),
        true
    )) {
        throw new RuntimeException('Invalid file format.');
    }

    // You should name it uniquely.
    // DO NOT USE $_FILES['upfile']['name'] WITHOUT ANY VALIDATION !!
    // On this example, obtain safe unique name from its binary data.
    if (!move_uploaded_file(
        $_FILES['upfile']['tmp_name'],
        sprintf('./uploads/%s.%s',
            sha1_file($_FILES['upfile']['tmp_name']),
            $ext
        )
    )) {
        throw new RuntimeException('Failed to move uploaded file.');
    }

    echo 'File is uploaded successfully.';

} catch (RuntimeException $e) {

    echo $e->getMessage();

}

?>

我遇到的問題是該文件未上傳,並且為什么我沒有從php得到任何反饋。

感謝您的任何幫助

更新:謝謝@akmozo的答復和回答。 就像我在評論中說的那樣,此腳本有效

<?php

 $uploads_dir = './uploads/';

  if( $_FILES['Filedata']['error'] == 0 ){

  if( move_uploaded_file( $_FILES['Filedata']['tmp_name'],     $uploads_dir.$_FILES['Filedata']['name'] ) ){

     echo 'ok';
     echo 'systemResult=Awesome';

     exit();

     }

   }

   echo 'error';
   echo 'systemResult=did not work';

   exit();

   ?>

默認情況下, FileReference對象的上載數據字段名稱為"Filedata" ,這就是您應該在PHP代碼中使用的名稱( $_FILES['Filedata'] ...)。

您當然可以在FileReference.upload()函數中更改該名稱:

 fr.upload(request, 'upfile');

希望能有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM