Öne Çıkanlar

PHP 5
Price: 19.0 TL 19.0 TL
Dreamweaver 8
Price: 23.0 TL 23.0 TL
Her Yönüyle PHP 6
Price: 25.65 TL 25.65 TL
PHP ile Web Programcılığı
Price: 39.0 TL 39.0 TL

Anket

En beğendiğiniz PHP kitabı hangisi?

Dosya dizin işlemleri



Projelerimizde bazı durumlarda dosya ve dizinlerle uğraşabiliyoruz. Son yıllarda metin dosyalarıyla pek uğraşacak durumda olmasam da, bazı durumlarda dosyalarla uğraşmak gerekebiliyor. Mesela shell script bilmeyen ama php konusunda uzman bir sistem yöneticisi, server üzerinde birçok işlemi PHP kullanarak yapabilir. Bu durumda da kesinlikle metin dosyaları üzerinde işlem yapması gerekecek. Onun için aşağıda vermiş olduğum class fazlasıyla işinizi görecektir. Eksik gördüğünüz yerleri kendiniz tamamlayabilirsiniz.

<?php
/**
 * dosya işlemlerini yürüten class
 *
 * @author Mehmet Şamlı
 */
class File{
  
/**
   * işlem yapılan dizin
   *
   * @var string
   */
  
private $pathName;
  
/**
   * işlem yapılan dosya
   *
   * @var string
   */
  
private $fileName;
  
/**
   * işlem yapılan dosya objesi
   *
   * @var object
   */
  
private $fileObject;
  
/**
   * dizini set eder
   *
   * @param string $path
   */
  
public function setPart($path,$file){
    
$this->pathName $path;
    
$this->fileName $file;
  }
  
/**
   * dosya var mı?
   *
   * @return boolean
   */
  
public function fileExists(){
    if(! @
file_exists($this->pathName.'/'.$this->fileName)){
      throw new 
Exception('Dosya veya dizin hatalı girildi.',1);
      return 
false;
    } else 
      return 
true;
  }
  
/**
   * dosyayı açar.
   *
   * @param string $paramater
   * @return void
   */
  
private function fileOpen($paramater,$fileExists=true){
      try{
        if(
$fileExists)
          
self::fileExists();
          
$this->fileObject fopen($this->pathName.'/'.$this->fileName,$paramater);
      } catch (
Exception $error){
        
trigger_error($error->getMessage(),E_USER_ERROR);
      }
  }
  
/**
   * açık olan dosyayı kapatır.
   *
   * @return void
   */
  
private function fileClose(){
      
fclose($this->fileObject);
  }
  
/**
   * dosya boyunu get eder
   *
   * @return integer
   */
  
private function getFileSize(){
      return 
filesize($this->pathName.'/'.$this->fileName);
  }
  
/**
   * metin dosyaların içeriğini okur
   *
   * @param string $readParameter
   * @return string
   */
  
public function read($readParameter='r'){
      try {
        
self::fileOpen($readParameter);
        
$fileSize self::getFileSize();
        
$read '';
        while (!
feof($this->fileObject)) {
          
$read .= fread($this->fileObject,$fileSize);
        }
        
self::fileClose();
        return 
$read;
      } catch (
Exception $error){
        
trigger_error($error->getMessage(),E_USER_ERROR);
      }
      return 
false;
  }
  
/**
   * dosya yazar
   *
   * @param string $text
   * @param string $parameter
   * @return boolean
   */
  
public function write($text,$parameter='w'){
      try {
        
self::fileOpen($parameter,false);
        if(!@
fwrite($this->fileObject,$text))
          throw new 
Exception("Dosya yazamadım",1);
        return 
true;          
      } catch (
Exception $error){
        
trigger_error($error->getMessage(),E_USER_ERROR);
      }
      return 
false;
  }
  
/**
   * exec için return döndürür
   *
   * @param integer $int
   * @return unknown
   */
  
private static function getExecReturn($int){
      if(
$int==0)
        return 
true;
      else 
        return 
false;
  }
  
/**
   * yeni klasör oluşturur
   *
   * @param string $pathName
   * @param boolean $exec
   * @return boolean
   */
  
public static function createDirectory($pathName,$exec=false){
      if(
$exec){
        
exec("mkdir $pathName",$arr,$int);
        return 
self::getExecReturn($int);
      }else {
        return 
mkdir($pathName,755);
      }
  }
  
/**
   * dizin veya dosya siler
   *
   * @param rstring $pathName
   * @param boolean $exec
   * @return boolean
   */
  
public static function remove($pathName,$exec=false){
      if(
$exec){
        
exec("rm -rf $pathName",$outArr,$int);
        return 
self::getExecReturn($int);
      }else {
        if(
is_file($pathName))
          return 
unlink($pathName);
        else
          return 
rmdir($pathName);
      }
  }
  
/**
   * dosya kopyalar
   *
   * @param string $sourceFile
   * @param string $copyFile
   * @return boolean
   */
  
public static function copy($sourceFile,$copyFile,$exec=false){
      if(
$exec){
        
exec("cp $sourceFile $copyFile",$outArr,$int);
        return 
self::getExecReturn($int);
      }else {
        return 
copy($sourceFile,$copyFile);
      }
  }  
  
/**
   * dosya adını değiştirir
   *
   * @param string $sourceFile
   * @param string $copyFile
   * @return boolean
   */
  
public static function rename($sourceFile,$renameFile,$exec=false){
      if(
$exec){
        
exec("mv $sourceFile $renameFile",$outArr,$int);
        return 
self::getExecReturn($int);
      }else {
        return 
rename($sourceFile,$renameFile);
      }
  }
  
/**
   * dosya upload edilirken kullanılan metod
   *
   * @param string $sourceFile
   * @param string $copyFile
   * @return boolan
   */
  
public static function upload($sourceFile,$copyFile){
      return 
move_uploaded_file($sourceFile,$copyFile);
  }
}
?>