所以,我正在使用我在WWW上下载的jQuery上传脚本。
这是我的JavaScript代码:
$(document).ready(function()
{
var settings = {
url: "upload.php",
method: "POST",
allowedTypes:"pdf,xls,doc,docx,xlsx",
fileName: "name",
multiple: true,
onSuccess:function(files,data,xhr)
{
$("#status").html("<font color='green'>Upload is successed</font>");
},
onError: function(files,status,errMsg)
{
$("#status").html("<font color='red'>Upload is failed: " + errMsg + "</font>");
},
showDelete:true,
deleteCallback: function(data,pd)
{
for(var i=0;i<data.length;i++)
{
$.post("delete.php",{op:"delete",name:data[i]},
function(resp, textStatus, jqXHR)
{
//Show Message
$("#status").html("<font color='red'>File deleted.</font>");
});
}
pd.statusbar.hide(); //You choice to hide/not.
}
};
$("#mulitplefileuploader").uploadFile(settings);
});
这是我的&#34; upload.php&#34;:
<?php
include_once("_assets/_pages/db.php");
$output_dir = "_files/";
if(isset($_FILES["myfile"]))
{
$ret = array();
$error = $_FILES["myfile"]["error"];
//You need to handle both cases
//If Any browser does not support serializing of multiple files using FormData()
if(!is_array($_FILES["myfile"]["name"])) //single file
{
$fileName = $_FILES["myfile"]["name"];
move_uploaded_file($_FILES["myfile"]["tmp_name"],$output_dir.$fileName);
$ret[]= $fileName;
$sql = 'INSERT INTO `Files` (FileName, UploadDate, DownloadCount) VALUES ($filename, CURRENT_TIMESTAMP, 0)';
$db->query($sql);
}
else //Multiple files, file[]
{
$fileCount = count($_FILES["myfile"]["name"]);
for($i=0; $i < $fileCount; $i++)
{
$fileName = $_FILES["myfile"]["name"][$i];
move_uploaded_file($_FILES["myfile"]["tmp_name"][$i],$output_dir.$fileName);
$ret[]= $fileName;
$sql = 'INSERT INTO `Files` (FileName, UploadDate, DownloadCount) VALUES ($filename, CURRENT_TIMESTAMP, 0)';
$db->query($sql);
}
}
echo json_encode($ret);
}
?>
我的upload.php没有将文件放入&#34; _files /&#34;,也没有在我的数据库中插入任何内容? 我做错了什么?
其他问题:你是否推荐任何其他设计良好(引导程序友好)的jQuery文件上传处理程序?
答案 0 :(得分:1)
我想,如果没有显示任何文件......您可能会在表单中遗漏enctype="multipart/form-data"
。
答案 1 :(得分:1)
PHP正在寻找isset($ _ FILES [&#34; myfile&#34;]),但是,你永远不会定义任何名为&#39; myfile&#39;在你的JS中。我的猜测是你的if(isset())纯粹被跳过,没有错误,状态200 OK。