php jquery 多文件上传简单实例
程序员文章站
2023-11-11 23:41:58
复制代码 代码如下:
<div id="mulitplefileuploader">upload</div>
<div id="status"></div>
<script>
$(document).ready(function()
{
var settings = {
url: "upload.php",
method: "post",
allowedtypes:"jpg,png,gif,doc,pdf,zip",
filename: "myfile",
multiple: true,
onsuccess:function(files,data,xhr)
{
$("#status").html("<font color='green'>upload is success</font>");
},
onerror: function(files,status,errmsg)
{
$("#status").html("<font color='red'>upload is failed</font>");
}
}
$("#mulitplefileuploader").uploadfile(settings);
});
</script>
<?php
//if directory doesnot exists create it.
$output_dir = "../upload";
if(isset($_files["myfile"]))
{
$ret = array();
$error =$_files["myfile"]["error"];
{
if(!is_array($_files["myfile"]['name'])) //single file
{
$filename = $_files["myfile"]["name"];
move_uploaded_file($_files["myfile"]["tmp_name"],$output_dir. $_files["myfile"]["name"]);
//echo "<br> error: ".$_files["myfile"]["error"];
$ret[$filename]= $output_dir.$filename;
}
else
{
$filecount = count($_files["myfile"]['name']);
for($i=0; $i < $filecount; $i++)
{
$filename = $_files["myfile"]["name"][$i];
$ret[$filename]= $output_dir.$filename;
move_uploaded_file($_files["myfile"]["tmp_name"][$i],$output_dir.$filename );
}
}
}
echo json_encode($ret);
}
?>
upload
复制代码 代码如下:
<div id="mulitplefileuploader">upload</div>
<div id="status"></div>
<script>
$(document).ready(function()
{
var settings = {
url: "upload.php",
method: "post",
allowedtypes:"jpg,png,gif,doc,pdf,zip",
filename: "myfile",
multiple: true,
onsuccess:function(files,data,xhr)
{
$("#status").html("<font color='green'>upload is success</font>");
},
onerror: function(files,status,errmsg)
{
$("#status").html("<font color='red'>upload is failed</font>");
}
}
$("#mulitplefileuploader").uploadfile(settings);
});
</script>
upload.php
复制代码 代码如下:
<?php
//if directory doesnot exists create it.
$output_dir = "../upload";
if(isset($_files["myfile"]))
{
$ret = array();
$error =$_files["myfile"]["error"];
{
if(!is_array($_files["myfile"]['name'])) //single file
{
$filename = $_files["myfile"]["name"];
move_uploaded_file($_files["myfile"]["tmp_name"],$output_dir. $_files["myfile"]["name"]);
//echo "<br> error: ".$_files["myfile"]["error"];
$ret[$filename]= $output_dir.$filename;
}
else
{
$filecount = count($_files["myfile"]['name']);
for($i=0; $i < $filecount; $i++)
{
$filename = $_files["myfile"]["name"][$i];
$ret[$filename]= $output_dir.$filename;
move_uploaded_file($_files["myfile"]["tmp_name"][$i],$output_dir.$filename );
}
}
}
echo json_encode($ret);
}
?>