我已经开发了一个上传网站几个月了,网站上最受欢迎的功能就是多文件上传器,个人而言,我很乐意将这个功能带入,但我正在寻找已经可用的脚本将允许我这样做,因为我不知道如何编写纯粹的php / html上传工具。
我遇到了SWFUpload,这看起来像是一个不错的脚本,但是我想知道它是否可以执行以下操作:
- >允许我选择允许上传哪些文件扩展名。 - >允许我设置上传的最大文件数量。 - >允许我在上传后执行MySQL查询,因为我们记录所有上传并将其分配给帐户
我需要在这样的脚本中有很大的灵活性,而SWFUpload似乎没有提供这样的灵活性来将脚本更改为我想要的东西,所以我想问一下转换目前单个文件是否更容易上传到多文件上传器。
感谢您的时间, 杰克
答案 0 :(得分:5)
如果您愿意为现代浏览器制作一些东西,只需将表格看作:
<input name="files[]" type="file" multiple="multiple" />
您的浏览器将自动完成前端的剩余部分。
如果您已经可以上传文件,那么上传多个文件就不需要进行太多更改,只需在循环中执行您现在正在执行的操作。 print_r($ _ FILES)将显示结构。
如果你想要一个花哨的前端我推荐Uploadify,它适用于jQuery。
答案 1 :(得分:2)
之前我使用过Maian Uploader,非常灵活。您可以指定允许用户上传的文件类型,文件大小限制,选择要上传的多个文件等。以下是指向页面http://www.maianscriptworld.co.uk/free-php-scripts/maian-uploader/free-file-upload-system/index.html的链接。
设计不是很漂亮但你可以根据自己的喜好自定义。您可能需要先查看演示http://www.maianscriptworld.com/demos/uploader/。您只需登录并转到左侧的上传部分即可查看。
答案 2 :(得分:1)
此脚本提供您所需的一切!
答案 3 :(得分:0)
您需要包含这4个文件以及名为“uploads”的文件夹,其中将存储上传的图像。
<强> multiupload.php 强>
<html>
<head>
<title>Upload Multiple Images Using jquery and PHP</title>
<!-------Including jQuery from Google ------>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="script.js"></script>
<!------- Including CSS File ------>
<link rel="stylesheet" type="text/css" href="style.css">
<body>
<div id="maindiv">
<div id="formdiv">
<h2>Multiple Image Upload Form</h2>
<form enctype="multipart/form-data" action="upload.php" method="post">
First Field is Compulsory. Only JPEG,PNG,JPG Type Image Uploaded. Image Size Should Be Less Than 100KB.
<div id="filediv"><input name="file[]" type="file" id="file"/></div>
<input type="button" id="add_more" class="upload" value="Add More Files"/>
<input type="submit" value="Upload File" name="submit" id="upload" class="upload"/>
</form>
<!------- Including PHP Script here ------>
<?php include "connect.php"; ?>
<?php include "upload.php"; ?>
</div>
</div>
</body>
</html>
<强> upload.php的强>
<?php include "connect.php"; ?>
<?php
if (isset($_POST['submit'])) {
$j = 0; // Variable for indexing uploaded image.
$target_path = "uploads/"; // Declaring Path for uploaded images.
for ($i = 0; $i < count($_FILES['file']['name']); $i++) {
// Loop to get individual element from the array
$validextensions = array("jpeg", "jpg", "png","pdf","gif","doc","docx","txt","bmp"); // Extensions which are allowed.
$ext = explode('.', basename($_FILES['file']['name'][$i])); // Explode file name from dot(.)
$file_extension = end($ext); // Store extensions in the variable.
//$target_path = $target_path . md5(md5(uniqid())) . "." . $ext[count($ext) - 1]; // Set the target path with a new name of image.
$j = $j + 1; // Increment the number of uploaded images according to the files in array.
if (($_FILES["file"]["size"][$i] < 20000000) // Approx. 100kb files can be uploaded.
&& in_array($file_extension, $validextensions)) {
if (move_uploaded_file($_FILES['file']['tmp_name'][$i], "uploads/".$_FILES['file']['name'][$i])) {
//if (move_uploaded_file($_FILES['file']['tmp_name'][$i], $target_path)) { <!----For file uploading with actual name->
// If file moved to uploads folder.
echo $imagename=basename($_FILES['file']['name'][$i]);
echo $imagetmp="uploads/" . $imagename;
//Insert the image name and image content in image_table
$insert_image="INSERT INTO `image_table`(`image_name`, `image_content`) VALUES('$imagetmp','$imagename')";
mysql_query($insert_image);
echo $j. ').<span id="noerror">Image uploaded successfully!.</span><br/><br/>';
} else { // If File Was Not Moved.
echo "not inserted in db";
echo $j. ').<span id="error">please try again!.</span><br/><br/>';
}
} else { // If File Size And File Type Was Incorrect.
echo $j. ').<span id="error">***Invalid file Size or Type***</span><br/><br/>';
}
}
}
?>
<强>的script.js 强>
var abc = 0; // Declaring and defining global increment variable.
$(document).ready(function() {
// To add new input file field dynamically, on click of "Add More Files" button below function will be executed.
$('#add_more').click(function() {
$(this).before($("<div/>", {
id: 'filediv'
}).fadeIn('slow').append($("<input/>", {
name: 'file[]',
type: 'file',
id: 'file'
}), $("<br/><br/>")));
});
// Following function will executes on change event of file input to select different file.
$('body').on('change', '#file', function() {
if (this.files && this.files[0]) {
abc += 1; // Incrementing global variable by 1.
var z = abc - 1;
var x = $(this).parent().find('#previewimg' + z).remove();
$(this).before("<div id='abcd" + abc + "' class='abcd'><img id='previewimg" + abc + "' src=''/></div>");
var reader = new FileReader();
reader.onload = imageIsLoaded;
reader.readAsDataURL(this.files[0]);
$(this).hide();
$("#abcd" + abc).append($("<img/>", {
id: 'img',
src: 'x.jpg',
alt: 'delete'
}).click(function() {
$(this).parent().parent().remove();
}));
}
});
// To Preview Image
function imageIsLoaded(e) {
$('#previewimg' + abc).attr('src', e.target.result);
};
$('#upload').click(function(e) {
var name = $(":file").val();
if (!name) {
alert("First Image Must Be Selected");
e.preventDefault();
}
});
});
<强>的style.css 强>
@import "http://fonts.googleapis.com/css?family=Droid+Sans";
form{
background-color:#fff
}
#maindiv{
width:960px;
margin:10px auto;
padding:10px;
font-family:'Droid Sans',sans-serif
}
#formdiv{
width:500px;
float:left;
text-align:center
}
form{
padding:40px 20px;
box-shadow:0 0 10px;
border-radius:2px
}
h2{
margin-left:30px
}
.upload{
background-color:red;
border:1px solid red;
color:#fff;
border-radius:5px;
padding:10px;
text-shadow:1px 1px 0 green;
box-shadow:2px 2px 15px rgba(0,0,0,.75)
}
.upload:hover{
cursor:pointer;
background:#c20b0b;
border:1px solid #c20b0b;
box-shadow:0 0 5px rgba(0,0,0,.75)
}
#file{
color:green;
padding:5px;
border:1px dashed #123456;
background-color:#f9ffe5
}
#upload{
margin-left:45px
}
#noerror{
color:green;
text-align:left
}
#error{
color:red;
text-align:left
}
#img{
width:17px;
border:none;
height:17px;
margin-left:-20px;
margin-bottom:91px
}
.abcd{
text-align:center
}
.abcd img{
height:100px;
width:100px;
padding:5px;
border:1px solid #e8debd
}
b{
color:red
}
通过使用此代码,您可以上传多个图像,并且图像也将存储在数据库中。为此,您需要在数据库中创建一个表,其中有三个字段id,image_name和image_content。