PHP 文件編程綜合案例-文件上傳的實現(xiàn)(2)_PHP教程

      編輯Tag賺U幣
      教程Tag:暫無Tag,歡迎添加,賺取U幣!

      推薦:解析在PHP中使用mysqli擴展庫對mysql的操作
      本篇文章是對在PHP中使用mysqli擴展庫對mysql的操作進行了詳細的分析介紹,需要的朋友參考下 1、在PHP中 使用mysqli擴展庫對mysql 的dql操作 復制代碼 代碼如下: ?php header(Content-type: text/html;charset=utf-8); //mysqli操作mysql數(shù)據(jù)庫(面向?qū)ο蠓绞? //1、創(chuàng)建


      <?php
          class Upload{
              public $upload_name; //上傳文件名
              public $upload_tmp_path; //上傳文件保存到服務(wù)器的temp路徑
              public $file_size;
              public $file_type;
              public $file_save_path;
              function __construct(){
                  $this->upload_name=$_FILES['myfile']['name'];
                  $this->upload_tmp_path=$_FILES['myfile']['tmp_name'];
                  $this->file_size=$_FILES['myfile']['size'];
                  $this->file_type=$_FILES['myfile']['type'];
                  $this->allow_file_type = array('jpeg','jpg','png','gif','bmp','doc','zip','rar','txt','wps','xlsx','ppt');
                  $this->file_save_path=$_SERVER['DOCUMENT_ROOT']."/file/up/";
              }
              public function upload_file($username){
                  //判斷文件大小
                  if($this->file_size>2*1024*1024){
                      echo "<script type='text/javascript'>window.alert('文件不能大于2M')</script>";
                      exit();
                  }
                  //獲取文件類型
      /*            if($this->file_type!="image/jpeg" && $this->file_type!="image/pjpeg"){
                      echo "文件類型只能是 jpg 格式";
                      exit();
                  }
      */            //獲取文件的擴展名
                  $file_type=$this->getFileExt($this->upload_name);
                  if(!in_array($file_type,$this->allow_file_type)){
                      echo "上傳文件類型格式錯誤";
                      exit();
                  }           
                  //判斷上傳是否OK
                  if(is_uploaded_file($this->upload_tmp_path)){

                      //防止圖片覆蓋問題,為每個用戶建立一個文件夾   
                      $user_path=$this->file_save_path.$username;
                      if(!file_exists($user_path)){
                          mkdir ($user_path);
                      }
                      //$move_to_file=$user_path."/".$_FILES['myfile']['name'];
                      //防止用戶上傳用戶名相同的問題
                      //$file_true_name=$_FILES['myfile']['name'];
                      $move_to_file=$user_path."/".time().rand(1,1000).substr($this->upload_name,strripos($this->upload_name,"."));
                      //echo $upload_file.$move_to_file;
                      //中文要轉(zhuǎn)碼
                      if(move_uploaded_file($this->upload_tmp_path,iconv("utf-8","gb2312","$move_to_file"))){
                          echo $this->upload_name."上傳成功";
                      }else{
                          echo "上傳失敗";
                      }
                  }else{
                      echo "上傳失敗";
                  }
              }

              //獲取文件的擴展名
              public function getFileExt($filename){
                  $fileExt=pathinfo($filename);
                  return $fileExt["extension"];
              }
          }
      ?>

      分享:淺析PHP繪圖技術(shù)
      1、圖片格式:目前網(wǎng)站開發(fā)常見的圖片格式有g(shù)if,jpg/jpeg,png ..... 區(qū)別: gif 圖片壓縮率高,但是只能顯示256色,可能造成顏色的丟失,可以顯示動畫 jpg/jpeg 圖片壓縮率高(有損壓縮),可以用較小的文件來顯示,網(wǎng)頁上用得比較多 png 該格式綜合了gif和jpg的優(yōu)勢,缺

      共2頁上一頁12下一頁
      來源:模板無憂//所屬分類:PHP教程/更新時間:2013-07-04
      相關(guān)PHP教程