使用AS3和PHP错误将文件上传到服务器
问题描述:
我正尝试使用AS3和PHP将文件上传到服务器。这是我的AS3代码,然后是PHP代码。我想要上传到的文件夹是可写的。和文件大小约20Kb。 php脚本在我的服务器上,并且flash文件调用它。使用AS3和PHP错误将文件上传到服务器
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获得任何反馈,为什么。
感谢您的帮助
UPDATE: 谢谢@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');
希望可以帮助
感谢您的快速评论,回答。不幸的是,我仍然得到相同的错误,没有上传。我试着将upfile改为filedata,但没有运气。然而,这个基本脚本的PHP脚本工作,这告诉我,闪光做的是应该做的,但也许PHP脚本中有问题 – FlashV8
@ FlashV8对不起,但我不明白是什么问题:Flash或PHP ? – akmozo