使用 uploader.php class 使用 php 和 jquery 处理多个文件上传
Using uploader.php class to handle multiple file upload using php and jquery
我想使用 link
中的答案使用 uploader.php class 上传多个 pdf 文件。
我正在使用 jquery 和 php。下面是我的代码,现在我想使用 link 中的上传器 class 处理和上传文件。
使用此代码,我在 uploader.php
的第 70 行和第 71 行中得到未定义的索引 txtFile
上传者class是
<?php
class Uploader
{
private $destinationPath;
private $errorMessage;
private $extensions;
private $allowAll;
private $maxSize;
private $uploadName;
private $seqnence;
public $name='Uploader';
public $useTable =false;
function setDir($path){
$this->destinationPath = $path;
$this->allowAll = false;
}
function allowAllFormats(){
$this->allowAll = true;
}
function setMaxSize($sizeMB){
$this->maxSize = $sizeMB * (1024*1024);
}
function setExtensions($options){
$this->extensions = $options;
}
function setSameFileName(){
$this->sameFileName = true;
$this->sameName = true;
}
function getExtension($string){
$ext = "";
try{
$parts = explode(".",$string);
$ext = strtolower($parts[count($parts)-1]);
}catch(Exception $c){
$ext = "";
}
return $ext;
}
function setMessage($message){
$this->errorMessage = $message;
}
function getMessage(){
return $this->errorMessage;
}
function getUploadName(){
return $this->uploadName;
}
function setSequence($seq){
$this->imageSeq = $seq;
}
function getRandom(){
return strtotime(date('Y-m-d H:i:s')).rand(1111,9999).rand(11,99).rand(111,999);
}
function sameName($true){
$this->sameName = $true;
}
function uploadFile($fileBrowse){
$result = false;
$size = $_FILES[$fileBrowse]["size"];
$name = $_FILES[$fileBrowse]["name"];
$ext = $this->getExtension($name);
if(!is_dir($this->destinationPath)){
$this->setMessage("Destination folder is not a directory ");
}else if(!is_writable($this->destinationPath)){
$this->setMessage("Destination is not writable !");
}else if(empty($name)){
$this->setMessage("File not selected ");
}else if($size>$this->maxSize){
$this->setMessage("Too large file !");
}else if($this->allowAll || (!$this->allowAll && in_array($ext,$this->extensions))){
if($this->sameName==false){
$this->uploadName = $this->imageSeq."-".substr(md5(rand(1111,9999)),0,8).$this->getRandom().rand(1111,1000).rand(99,9999).".".$ext;
}else{
$this->uploadName= $name;
}
if(move_uploaded_file($_FILES[$fileBrowse]["tmp_name"],$this->destinationPath.$this->uploadName)){
$result = true;
}else{
$this->setMessage("Upload failed , try later !");
}
}else{
$this->setMessage("Invalid file format !");
}
return $result;
}
function deleteUploaded(){
unlink($this->destinationPath.$this->uploadName);
}
}
?>
$(document).ready(function(){
$("#add_more").click(function(e){
var current_count = $('input[type="file"]').length;
var next_count = current_count + 1;
$('#file_upload').append('<div class="col-md-3"><input class="form-control" type = "file" name = "file_'+next_count+ '"/></div> ');
});
});
!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link rel="stylesheet" type="text/css" href="../bootstrap-3.3.4-dist/css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="../bootstrap-datetimepicker-master/build/css/bootstrap-datetimepicker.min.css" />
<link href="includes/Styles/style_boot.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<?php echo $header; ?>
<hr />
<div class="container">
<div class="col-md-8">
<?php
if(Session::exists('home')){
echo Session::flash('home') ;
}
?>
</div>
<hr />
<div class="container">
<form id="file_upload" action="upload_file.php" method="get" enctype="multipart/form-data">
<div class="row">
<div class="col-md-3 ">
<input class="form-control" type="file" name="file_1">
</div>
</div><br>
<div class="row">
<div class="col-md-3">
<a id="add_more" href="#">Add More Files</a>
</div>
<div class="col-md3">
<input class="btn-success" type="submit" value="upload" />
</div>
</div>
</form>
</div>
</div>
<hr />
<?php echo $footer; ?>
<script type="text/javascript" src="../jquery/jquery-2.1.4.js"></script>
<script type="text/javascript" src="includes/common/ext.js"></script>
<script type="text/javascript" src="../bootstrap-3.3.4-dist/js/bootstrap.js"></script>
</body>
</html>
我想使用这段代码来处理 php 中的文件,但是我应该使用什么来代替这一行中的 txtFile "if($uploader->uploadFile('txtFile'))";
<?php
$uploader = new Uploader();
$uploader->setDir('uploads/images/');
$uploader->setExtensions(array('jpg','jpeg','png','gif')); //allowed extensions list//
$uploader->setMaxSize(.5); //set max file size to be allowed in MB//
if($uploader->uploadFile('txtFile')){ //txtFile is the filebrowse element name //
$image = $uploader->getUploadName(); //get uploaded file name, renames on upload//
}else{//upload failed
$uploader->getMessage(); //get upload error message
}
?>
未定义的错误是因为你想访问一个未初始化的变量。之前检查一下:
if(isset($var))
要访问发布的文件,请使用如下内容:
foreach($_FILES as $key => $info) {
if(substr($key, 0, 5) != "file_")
continue;
// Handle the file - file information at $info as array
}
或者在您的文件输入中使用 multiple
标签 - 那么您甚至可以 select 多个文件:
<input class="form-control" type="file" name="files[]" multiple>
然后它在 PHP 中工作:
foreach($_FILES['files'] as $file) {
// Handle the file - file information at $file as array
}
您的方法是 get
,应该是 POST
,您的文件上传字段的名称是 file_1
,但您尝试通过 txtFile
访问文件。
我想使用 link
中的答案使用 uploader.php class 上传多个 pdf 文件。
我正在使用 jquery 和 php。下面是我的代码,现在我想使用 link 中的上传器 class 处理和上传文件。
使用此代码,我在 uploader.php
的第 70 行和第 71 行中得到未定义的索引 txtFile上传者class是
<?php
class Uploader
{
private $destinationPath;
private $errorMessage;
private $extensions;
private $allowAll;
private $maxSize;
private $uploadName;
private $seqnence;
public $name='Uploader';
public $useTable =false;
function setDir($path){
$this->destinationPath = $path;
$this->allowAll = false;
}
function allowAllFormats(){
$this->allowAll = true;
}
function setMaxSize($sizeMB){
$this->maxSize = $sizeMB * (1024*1024);
}
function setExtensions($options){
$this->extensions = $options;
}
function setSameFileName(){
$this->sameFileName = true;
$this->sameName = true;
}
function getExtension($string){
$ext = "";
try{
$parts = explode(".",$string);
$ext = strtolower($parts[count($parts)-1]);
}catch(Exception $c){
$ext = "";
}
return $ext;
}
function setMessage($message){
$this->errorMessage = $message;
}
function getMessage(){
return $this->errorMessage;
}
function getUploadName(){
return $this->uploadName;
}
function setSequence($seq){
$this->imageSeq = $seq;
}
function getRandom(){
return strtotime(date('Y-m-d H:i:s')).rand(1111,9999).rand(11,99).rand(111,999);
}
function sameName($true){
$this->sameName = $true;
}
function uploadFile($fileBrowse){
$result = false;
$size = $_FILES[$fileBrowse]["size"];
$name = $_FILES[$fileBrowse]["name"];
$ext = $this->getExtension($name);
if(!is_dir($this->destinationPath)){
$this->setMessage("Destination folder is not a directory ");
}else if(!is_writable($this->destinationPath)){
$this->setMessage("Destination is not writable !");
}else if(empty($name)){
$this->setMessage("File not selected ");
}else if($size>$this->maxSize){
$this->setMessage("Too large file !");
}else if($this->allowAll || (!$this->allowAll && in_array($ext,$this->extensions))){
if($this->sameName==false){
$this->uploadName = $this->imageSeq."-".substr(md5(rand(1111,9999)),0,8).$this->getRandom().rand(1111,1000).rand(99,9999).".".$ext;
}else{
$this->uploadName= $name;
}
if(move_uploaded_file($_FILES[$fileBrowse]["tmp_name"],$this->destinationPath.$this->uploadName)){
$result = true;
}else{
$this->setMessage("Upload failed , try later !");
}
}else{
$this->setMessage("Invalid file format !");
}
return $result;
}
function deleteUploaded(){
unlink($this->destinationPath.$this->uploadName);
}
}
?>
$(document).ready(function(){
$("#add_more").click(function(e){
var current_count = $('input[type="file"]').length;
var next_count = current_count + 1;
$('#file_upload').append('<div class="col-md-3"><input class="form-control" type = "file" name = "file_'+next_count+ '"/></div> ');
});
});
!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link rel="stylesheet" type="text/css" href="../bootstrap-3.3.4-dist/css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="../bootstrap-datetimepicker-master/build/css/bootstrap-datetimepicker.min.css" />
<link href="includes/Styles/style_boot.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<?php echo $header; ?>
<hr />
<div class="container">
<div class="col-md-8">
<?php
if(Session::exists('home')){
echo Session::flash('home') ;
}
?>
</div>
<hr />
<div class="container">
<form id="file_upload" action="upload_file.php" method="get" enctype="multipart/form-data">
<div class="row">
<div class="col-md-3 ">
<input class="form-control" type="file" name="file_1">
</div>
</div><br>
<div class="row">
<div class="col-md-3">
<a id="add_more" href="#">Add More Files</a>
</div>
<div class="col-md3">
<input class="btn-success" type="submit" value="upload" />
</div>
</div>
</form>
</div>
</div>
<hr />
<?php echo $footer; ?>
<script type="text/javascript" src="../jquery/jquery-2.1.4.js"></script>
<script type="text/javascript" src="includes/common/ext.js"></script>
<script type="text/javascript" src="../bootstrap-3.3.4-dist/js/bootstrap.js"></script>
</body>
</html>
我想使用这段代码来处理 php 中的文件,但是我应该使用什么来代替这一行中的 txtFile "if($uploader->uploadFile('txtFile'))";
<?php
$uploader = new Uploader();
$uploader->setDir('uploads/images/');
$uploader->setExtensions(array('jpg','jpeg','png','gif')); //allowed extensions list//
$uploader->setMaxSize(.5); //set max file size to be allowed in MB//
if($uploader->uploadFile('txtFile')){ //txtFile is the filebrowse element name //
$image = $uploader->getUploadName(); //get uploaded file name, renames on upload//
}else{//upload failed
$uploader->getMessage(); //get upload error message
}
?>
未定义的错误是因为你想访问一个未初始化的变量。之前检查一下:
if(isset($var))
要访问发布的文件,请使用如下内容:
foreach($_FILES as $key => $info) {
if(substr($key, 0, 5) != "file_")
continue;
// Handle the file - file information at $info as array
}
或者在您的文件输入中使用 multiple
标签 - 那么您甚至可以 select 多个文件:
<input class="form-control" type="file" name="files[]" multiple>
然后它在 PHP 中工作:
foreach($_FILES['files'] as $file) {
// Handle the file - file information at $file as array
}
您的方法是 get
,应该是 POST
,您的文件上传字段的名称是 file_1
,但您尝试通过 txtFile
访问文件。