PHP无刷新上传文件(非AJAX)

180it 2020-02-27 PM 1223℃ 0条

前言
在PHP中,我们一般写的form表单都是会进行跳转到另外一个页面进行数据操作的,其中我们也可以用到例如像Ajax一样的技术来让数据进行无刷新的操作,这次就分享一下非Ajax无刷新上传文件的教程。

教程
在这个例子中,会用到一个HTMLiframe标签,该标签介绍请进入:W3C
多余的话就不多说,直接上代码。

index.php

<!DOCTYPE html>
<meta charset="utf-8" />
<title>文件上传</title>
<script type="text/javascript">
        function startUpload() {
             document.getElementById('processing').innerHTML = 'loding...';
            return true;
        }
        function stopUpload(rel){
          var msg = '<img style="width: 300px;height: 200px;" src="'+rel+'" />';
            document.getElementById('processing').innerHTML = msg;
        }
    </script>
<body>
  <div id="processing"></div>
  <form method="post" enctype="multipart/form-data" action="newfile.php" target="form-target" onsubmit="startUpload();">
    <input type="file" name="file" />
    <input type="submit" name="sub" value="上传" />
  </form>
  <iframe style="width:0; height:0; border:0;" name="form-target"></iframe>
</body>

newfile.php

<?php
/*
 * 该代码来自憧憬点滴记忆 https://www.licoy.cn/1863.html
 * 转载请注明出处
 */
require "file.class.php";
$file = new FileUpload;
$file->Uploads();
$code = $file->GetErrorCode();
$dir = $file->GetUploadInfo();
?>
<script type="text/javascript">
window.top.window.stopUpload('<?php echo dirname($_SERVER['REQUEST_URI'])."/".$dir['fileDir'] ?>');
</script>
<?php
/*
 * 该代码来自憧憬点滴记忆 https://www.licoy.cn/1863.html
 * 转载请注明出处
 */
require "file.class.php";
$file = new FileUpload;
$file->Uploads();
$code = $file->GetErrorCode();
$dir = $file->GetUploadInfo();
?>
<script type="text/javascript">
window.top.window.stopUpload('<?php echo dirname($_SERVER['REQUEST_URI'])."/".$dir['fileDir'] ?>');
</script>

下面分享一个我写的文件上传类,有获取上传信息,获取上传错误,获取上传错误注释等等功能。

file.class.php

/*
 * 该代码来自憧憬点滴记忆 https://www.licoy.cn/1863.html
 * 转载请注明出处
 */
class FileUpload{
  /*
   * 文件上传信息
   */
  private $fileName;  //文件名
  private $fileType;  //文件类型
  private $fileSize;  //文件大小
  private $fileTmpName;  //文件临时存储位置
  private $fileError;  //上传错误信息
  private $otherError; //其他错误信息
  private $fileDir; //最终路径
  /*
   * 文件上传设置
   */
   private $path = 'uploads';  //文件上传位置
   private $type = array(
     'jpg','png','gif','jpeg','zip','mp4'
   );            //允许上传的文件类型
   private $size = 20480000; //字节为单位
   private $randName = TRUE; //随机名,为false关闭
   /*
    *构造函数
    */
   public function __construct(){
     $this->fileName = $_FILES["file"]["name"];
    $this->fileType = $_FILES["file"]["type"];
    $this->fileSize = $_FILES["file"]["size"];
    $this->fileTmpName = $_FILES["file"]["tmp_name"];
    $this->fileError = $_FILES["file"]["error"];
    if($this->randName){
      $this->fileName = date("Y-m-d-H-i-s",time()).".".$this->fileTypeName();
    }
   }
   /*
    * 文件上传
    */
   public function Uploads(){
       if($this->checkFileSize()){
         if($this->checkFileType()){
           if($this->checkFileDir()){
             $this->fileDir = $this->path."/".$this->fileName;
             if(!move_uploaded_file($this->fileTmpName,$this->fileDir)){
               $this->otherError = 0;  
               return false;
             }
            else{
              $this->otherError = 1;
              return true;
            }
           }
         }
       }
   }
   /*
    * 获取文件后缀名
    */
   private function fileTypeName(){
     $typeArr = explode(".", $this->fileName);
    $count = count($typeArr);
    return $typeArr[$count-1];
   }
   /*
    * 检测文件大小是否符合设置
    */
   private function checkFileSize(){
       if($this->fileSize < $this->size){
         return true;
      }
      else{
        $this->otherError = -1;
        return false;
      }
   }
   /*
    * 检测文件类型是否符合规定
    */
   private function checkFileType(){
     $typeArr = explode(".", $this->fileName);
    $count = count($typeArr);
     if(in_array(strtolower($typeArr[$count-1]), $this->type)) {
          return true;
      }else {
          $this->otherError = -2;
          return false;
      }
   }
   /*
    * 检测目录是否存在
    */
   private function checkFileDir(){
     if(file_exists($this->path)){
       return true;
     }
    else{
      $this->otherError = -3;
          return false;
    }
   }
   /*
    * 不存在目录可调用此方法创建文件夹
    */
   public function mkdirs(){
     mkdir($this->path);
    chmod($this->path,0777);
   }
   /*
    * 获取错误信息
    */
  public function GetError(){
    switch($this->otherError){
      case -1 : $errorStr = "文件过大!";break;
      case -2 : $errorStr = "文件不符合规定!";break;
      case -3 : $errorStr = "不存在上传目录!";break;
      case 1 : $errorStr = "上传成功!";break;
      case 0 : $errorStr = "上传失败!";break;
      default : $errorStr = '未知';
    }
    return $errorStr;
  }
  /*
   * 获取错误码
   */
   public function GetErrorCode(){
     return $this->otherError;
   }
  /*
   * 获取文件上传信息
   */
   public function GetUploadInfo(){
     $infoArr = array (
       'fileName' => $this->fileName,
      'fileType' => $this->fileType,
      'fileSize' => $this->fileSize,
      'fileTmpName' => $this->fileTmpName,
      'fileError' => $this->fileError,
      'fileDir' => $this->fileDir
     );
    return $infoArr;
   }
   
 
}//FileUploads_Class_End
支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

PHP无刷新上传文件(非AJAX)