ajax多次上传doenst php文件处理
Posted
技术标签:
【中文标题】ajax多次上传doenst php文件处理【英文标题】:ajax multiple upload doenst php file processing 【发布时间】:2012-09-08 20:58:26 【问题描述】:您好,我正在使用fineupload multi file uploading script,但有些东西我无法掌握。我正在尝试制作一个 php 服务器端文件处理脚本。
当你包括
<script type="text/javascript">
$(document).ready(function()
var uploader = new qq.FileUploader(
element: $('#manualUploadModeExample')[0],
action: "core/up.php",
autoUpload: false,
demoMode: false,
debug: false,
multiple: true,
maxConnections: 3,
disableCancelForFormUploads: false,
//0 is geen limit getal in bytes
minSizeLimit: 0,
sizeLimit: 0,
inputName: "qqfile",
uploadButtonText: "Select Files",
cancelButtonText: "verwijder",
failUploadText: "Upload mislukt"
);
$('#triggerUpload').click(function()
uploader.uploadStoredFiles();
);
);
显示它的html是
<div id="Upload">
<noscript>
<p>Please enable JavaScript to use file uploader.</p>
<!-- or put a simple form for upload here -->
</noscript>
<ul id="manualUploadModeExample" class="unstyled"></ul>
<span id="triggerUpload" class="btn btn-primary">Upload Queued Files</span>
</div>
现在在up.php
我想做文件验证之类的,但我无法获取文件信息
代码
<?php
if(isset($_FILES["qqfile"]))
echo json_encode(array('error' => "There is a file to work with"));
else
echo json_encode(array('error' => "there is no file set"));
?>
在上传表单上给出错误没有文件设置为错误。所以它确实从 php 文件中收到了错误……但它发送了什么?我怎么找到它
当我发回成功消息时
echo json_encode(array('success' => TRUE));
上传表单显示上传的文件变成绿色..
【问题讨论】:
【参考方案1】:他我不知道你是否已经找到了解决方案,但也许你可以看看这个:
第一个 html 表单
只需构建一个没有提交按钮而只是一个按钮按钮的普通 html 表单。 请注意,我的解决方案还有一个精美的加载栏!
<form enctype="multipart/form-data" id="myform">
<input type="text" name="some_usual_form_data" />
<br>
<input type="text" name="some_other_usual_form_data" />
<br>
<input type="file" multiple name="file[]" id="image" /> <sub>note that you have to use [] behind the name or php wil only see one file</sub>
<br>
<input type="button" value="Upload files" class="upload" />
</form>
<progress value="0" max="100"></progress>
<hr>
<div id="content_here_please"></div>
现在您可以添加accept="image/*"
或something in that fashion 以仅选择您需要或想要的文件类型。
然后用 jquery/javascript 上传
看起来像你的,但更好。
$(document).ready(function ()
$('body').on('click', '.upload', function()
// Get the form data. This serializes the entire form. pritty easy huh!
var form = new FormData($('#myform')[0]);
// Make the ajax call
$.ajax(
url: 'action.php',
type: 'POST',
xhr: function()
var myXhr = $.ajaxSettings.xhr();
if(myXhr.upload)
myXhr.upload.addEventListener('progress',progress, false);
return myXhr;
,
//add beforesend handler to validate or something
//beforeSend: functionname,
success: function (res)
$('#content_here_please').html(res);
,
//add error handler for when a error occurs if you want!
//error: errorfunction,
data: form,
// this is the important stuf you need to overide the usual post behavior
cache: false,
contentType: false,
processData: false
);
);
);
// Yes outside of the .ready space becouse this is a function not an event listner!
function progress(e)
if(e.lengthComputable)
//this makes a nice fancy progress bar
$('progress').attr(value:e.loaded,max:e.total);
相信我,这很简单。但是,您可以在此处创建一个 javascript 函数来验证文件以及是否需要整个表单。只需将您验证函数名称放在beforeSend: youvalfunctname
后面,您也可以在那里创建一个回调,例如beforeSend: function() //do stuf here
。当上传确实发生错误时,您可以使用error:
执行相同的操作。
服务器端php。终于
你可以在这里做你想做的事,但我只是举一个例子,你可以怎么做。
<?php
$succeed = 0;
$error = 0;
$thegoodstuf = '';
foreach($_FILES["file"]["error"] as $key => $value)
if ($value == UPLOAD_ERR_OK)
$succeed++;
//took this from: "https://***.com/questions/7563658/php-check-file-extension"
//you can loop through different file types
$file_parts = pathinfo($filename);
switch($file_parts['extension'])
case "jpg":
//do something with jpg
break;
case "exe":
// do sometinhg with exe
break;
case "": // Handle file extension for files ending in '.'
case NULL: // Handle no file extension
break;
$name = $_FILES['file']['name'][$key];
// replace file to where you want
copy($_FILES['file']['tmp_name'][$key], './upload/'.$name);
$size = filesize($_FILES['file']['tmp_name'][$key]);
// make some nice html to send back
$thegoodstuf .= "
<br>
<hr>
<br>
<h2>File $succeed - $name</h2>
<br>
give some specs:
<br>
size: $size bytes
";
else
$error++;
echo 'Good lord vader '.$succeed.' files where uploaded with success!<br>';
if($error)
echo 'shameful display! '.$error.' files where not properly uploaded!<br>';
echo '<br>O jeah there was a field containing some usual form data: '. $_REQUEST['some_usual_form_data'];
echo '<br>O jeah there was a field containing some usual form data: '. $_REQUEST['some_other_usual_form_data'];
echo $thegoodstuf;
?>
你也可以看看专门为上传图片做的demo:Note not always online
这里也是the demo的代码示例
【讨论】:
【参考方案2】:我以前用过qqFileUploader。我检查了我的代码,这是我访问该文件的方式。您需要使用$uploader ->file->getName()
。
$allowedExtensions = array('jpg','gif','png');
$sizeLimit = 2 * 1024 * 1024; //2mb
$uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
$tmp = $uploader->file->getName(); // this is your $_FILES["qqfile"]
$result = $uploader->handleUpload('/path/where/to/upload/','filename.jpg');
if ($result['success'])
// successfully moved the file from temp location
else
// not, error
【讨论】:
我见过“服务器”端 php,但我不知道如何使用它 请在github.com/valums/file-uploader上阅读有关如何使用脚本的更多信息以上是关于ajax多次上传doenst php文件处理的主要内容,如果未能解决你的问题,请参考以下文章