我正在尝试使用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();
?>
答案 0 :(得分:1)
默认情况下,FileReference
对象的上传数据字段名称为 "Filedata"
,这就是您应该在PHP代码中使用的内容($_FILES['Filedata']
.. )。
您当然可以在FileReference.upload()
功能中更改该名称:
fr.upload(request, 'upfile');
希望可以提供帮助