ZengCode.Com (The Thai Php Framework)  


Home   Download   Manual   About us    

Facebook   


MAIN MENU
News
Php Tips
Spring+Strut+Hibernate
Android Programming
Design Pattern By PHP
Ubuntu
Linux Quick Tips
C# Design Pattern
C# using Linq น่าใช้จริงๆ
C# Tips & Technique
Java & JavaScript Tips
Database & SQL
ZengCode Framework Guide
Zeng Code Code
Programming
IPhone (Tips and Trick)

Download เอกสารที่น่าสนใจ

     Class Upload ครับ   (2009-03-25)

เผื่อช่วยให้เรื่องการ upload ของคุณง่ายขึ้นกว่าเดิม
หากต้องการให้เพิ่มเติมอะไรเข้าไปก็ comment บอกนะครับ
ผมจะได้สร้างเพิ่มเติม ครับ
คลิกเข้าไปดู code ข้างในเลยครับ

 

<?php
/*
Created by Chiwa Kantawong
Created Date 25-03-2009
kchiwa@gmail.com
http://www.zengcode.com
*/

Class UploadObject
{
  public $name;
  public $saveName;
  public $type;
  public $files;
  public $error;
  public $size;
  public $status;
 
  private $saveLocation=Null;

  public function UploadObject($uploadFile)
    {
     $this->name = $uploadFile['name'] ;
     $this->type = $uploadFile['type'] ;
     $this->files = $uploadFile['tmp_name'] ;
     $this->error = $uploadFile['error'] ;
     $this->size = $uploadFile['size'] ;

     $this->saveName = $this->name;
    }

    public function SaveFile($flag=Null)
    /*
        $flag = Null   => save with old name
        $flag = 1      => save with new name md5 with microtime+old name;
        $flag = 2      => save with old name+version (not allow replace old version)
    */
    {
        if ($this->saveLocation == Null)
        {
            $this->status .= "saveLocation is not define<br> ";
            return false;
        }else if( !file_exists($this->saveLocation) )
        {
            $this->status .= $this->saveLocation." not exixts.<br> ";
            return false;
        }
        //generate new file name
        if ($flag == 1)            $this->saveName = md5(microtime())."_".$this->saveName;
        elseif ($flag == 2)   $this->saveName  = $this->GenerateVesionName();

        if (move_uploaded_file ($this->files,$this->saveLocation.$this->saveName ) )
        {
           $this->status .= "File had save to ==> ".$this->saveLocation.$this->saveName ;
            return true;
        }

       
    }


   public function GenerateVesionName()
    {
          if (file_exists($this->saveLocation.$this->name) ){
              list($name,$ext) = split('\.',$this->name);
                 $i = 1;
                 while(true) //loop for new version
                 {
                     $newName = $name."_".$i++.".".$ext;
                     if (!file_exists($this->saveLocation.$newName) ) return $newName;
                 }
          }else{
              return $this->name;
          }
    }


    public function SetLocation($location)
    {
        $this->saveLocation = $location;
    }

 
  public function Information()
    {
      echo "<BR>File Name => ".$this->name;
      echo "<BR>File Type  => ".$this->type;
      echo "<BR>File file        => ".$this->files;
      echo "<BR>File error  => ".$this->error;
      echo "<BR>File size   => ".$this->size;
       echo "<BR>File status   => ".$this->status;
      echo "<BR>saveLocation   => ".$this->saveLocation;
    }


}//end class




การเรียกใช้งานตาม code ด้านล่างนี้เลยครับ



if ($_FILES){
    $upload = new UploadObject($_FILES['userfile']);
    $upload->SetLocation("d:\\www\\course\\files\\");
    if ($upload->SaveFile(2) )
    {
        echo "<BR>Upload successful ".$upload->status;
        $upload ->Information();
    }else
    {
        echo "<BR>Upload fail ".$upload->error;
        echo "<BR>Upload fail ".$upload->status;
    }
}


?>



อันนี้เป็น Form ส่งข้อมูลธรรมดาๆ
<hr>
<U><B>Upload File To Server</B></U>
<hr>
<form action="" method="post" enctype="multipart/form-data">
  Browse your files:&nbsp;&nbsp;<input name="userfile" type="file" /><br />
  <input type="submit" value="Send files" />
</form>


 


Comment

555  (12 กรกฎาคม 2553)   
IP : 119.42.95.232

oop ของ php มันไม่เข้ากันกับ php ทำความเข้าใจอยาก ตัวช่วยน้อย เอาอักขระ ที่ไม่มีใช้ใน java หรือ .net มาใช้   โดยส่วนตัวผมจะเขียนรูปแบบเดิมใช้พวก function ช่วยครับ เมาะสำหรับงานที่ไม่ใหญ่มาก ไม่ซับซ้อนมาก  สำหรับงานใหญ่ๆ  จะใช้ .net(c#)  มาทำงาน  เคยลองใช้ java แต่แหล่งความรู้ที่จะอธิบายให้เราเข้าใจไม่ค่อยจะมี  เลยมาลองจับ  .net   อย่าไปยึดติดที่โปรแกรมมากนักครับ หาโปรแกรมที่ถนัด ทำเงินให้เราได้ ช่วยให้เราทำงานได้รวดเร็ว ยิ้งมีเครื่องมือ tool มาให้เยอะๆ ยิ้งดีประหยัดเวลา  เวลาทำงานเจ้านายไม่มานั่งดูหรอกครับว่าคุณจะเก่งการเขียน Code ขนาดไหนใช้ TEXT EDITOR เขียน code อย่างเทพ เค้าไม่สน เค้าต้องการแค่ให้เสร็จโดยเร็วเป็นพอ งานหนักจะมาตกที่คุณล่ะ หาตัวช่วยเข้าไว้ เพราะไม่มีใครจะมาช่วยคุณได้นอกจากตัวคุณเอง

 



เด็กอ่อน  (28 มีนาคม 2552)   
IP : 124.120.167.146

อ่ะ ผมไม่ค่อยเข้าใจ OOP แต่จะลองเอาไปศึกษาดูครับ



อาร์  (25 มีนาคม 2552)   
IP : 210.4.138.40

อึ้ม น่าจะมีประโยชน์ครับ
เดี๋ยวจะลองเอาไปใช้ดู concept ดีมากครับ



d  (25 มีนาคม 2552)   
IP : 202.176.88.57

ดีคะ


Name
Comment
Security CodeCAPTCHA Image

web hit counter

This page took 0.023510 seconds to load.