찾다
php教程PHP源码适用于初学者的简易PHP文件上传类_php技巧

这篇文章主要为大家分享了一个适用于初学者的简易PHP文件上传类,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例讲述了PHP多文件上传类,分享给大家供大家参考。具体如下:

<?php
class Test_Upload{
 
  protected $_uploaded = array();
  protected $_destination;  
  protected $_max = 1024000;
  protected $_messages = array();
  protected $_permited = array(
                &#39;image/gif&#39;,
                &#39;image/jpeg&#39;,
                &#39;image/pjpeg&#39;,
                &#39;image/png&#39;  
  );
  protected $_renamed = false;
   
  /**
   * 
   * @param mix $path
   * 
   */
  public function __construct($path){
     
    if (!is_dir($path) || !is_writable($path)){
      throw new Exception("文件名不可写,或者不是目录!");
    }
    $this->_destination = $path;
    $this->_uploaded = $_FILES;
  }
  /**
   * 移动文件
   * 
   */
  public function move(){
     
    $filed = current($this->_uploaded); 
       
    $isOk = $this->checkError($filed[&#39;name&#39;], $filed[&#39;error&#39;]);
    //debug ok
    if ($isOk){
      $sizeOk = $this->checkSize($filed[&#39;name&#39;], $filed[&#39;size&#39;]);
      $typeOk = $this->checkType($filed[&#39;name&#39;], $filed[&#39;type&#39;]);
      if ($sizeOk && $typeOk){
         
        $success = move_uploaded_file($filed[&#39;tmp_name&#39;], $this->_destination.$filed[&#39;name&#39;]);
         
        if ($success){
          $this->_messages[] = $filed[&#39;name&#39;]."文件上传成功";
        }else {
          $this->_messages[] = $filed[&#39;name&#39;]."文件上传失败";
        }
      }
       
    }
  }
  /**
   * 查询messages数组内容 
   *
   */
  public function getMessages(){
    return $this->_messages;
  }
   
  /**
   * 检测上传的文件大小
   * @param mix $string
   * @param int $size
   */
  public function checkSize($filename, $size){
     
    if ($size == 0){
      return false;
    }else if ($size > $this->_max){
      $this->_messages[] = "文件超出上传限制大小".$this->getMaxsize();
      return false;
    }else { 
      return true;
    }
  }
   
  /**
   * 检测上传文件的类型
   * @param mix $filename
   * @param mix $type
   */
  protected function checkType($filename, $type){
    if (!in_array($type, $this->_permited)){
      $this->_messages[] = "该文件类型是不被允许的上传类型";
      return false;
    }else {
      return true;
    }
  }
   
  /**
   * 获取文件大小
   * 
   */
  public function getMaxsize(){
    return number_format($this->_max / 1024, 1).&#39;KB&#39;;
  }
   
  /**
   * 检测上传错误
   * @param mix $filename
   * @param int $error
   * 
   */
  public function checkError($filename, $error){
    switch ($error){
      case 0 : return true;
      case 1 :
      case 2 : $this->_messages[] = "文件过大!"; return true;
      case 3 : $this->_messages[] = "错误上传文件!";return false;
      case 4 : $this->_messages[] = "没有选择文件!"; return false;
      default : $this->_messages[] = "系统错误!"; return false;
    }
  }
}
?>

성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

인기 기사

R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25 : Myrise에서 모든 것을 잠금 해제하는 방법
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

에디트플러스 중국어 크랙 버전

에디트플러스 중국어 크랙 버전

작은 크기, 구문 강조, 코드 프롬프트 기능을 지원하지 않음

SublimeText3 Linux 새 버전

SublimeText3 Linux 새 버전

SublimeText3 Linux 최신 버전

WebStorm Mac 버전

WebStorm Mac 버전

유용한 JavaScript 개발 도구

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

Atom Editor Mac 버전 다운로드

Atom Editor Mac 버전 다운로드

가장 인기 있는 오픈 소스 편집기