Webmaster общности: Predpriemach.com | SearchEngines.bg

    Работа с файлове

    Автор: Velikcho

    Здр! Ще ви покажа един клас, чрез който ще можете да работите с файлове т.е. да променяте съдържанието им, да ги местите и т.н.

    class.file.php

    <?
    /*
    @ class.file.php
    @ Автор: Велик Хазнатарлиев - Velikcho
    @ Чрез този клас вие можете да извършвате действия върху файл.
    @ ----------------------------------------------------------------------------------------
    */
    
    class file extends Main
    {
    var $filename;
    var $size;
    var $type;
    
    
    function file($file="")
    {
    if ($file&&$this->isFile($file)) $this->Info($file);
    else return;
    }
    
    
    
    function isFile($file)
    {
    if (!@is_file($file))
    $this->SetError($file." не е файл или не съществува",__LINE__,__FILE__);
    else
    return true;
    }
    
    
    
    function Info($file)
    {
    $this->OriginalName = $file;
    $this->filename = basename($file);
    $this->size = filesize($file);
    $infoPath = pathinfo($file);
    $this->type = $infoPath["extension"];
    }
    
    
    
    function CopyTo($dest)
    {
    if (!@copy($this->OriginalName,$dest))
    $this->SetError("Не мога да копирам файла",__LINE__,__FILE__);
    }
    
    
    
    function Delete()
    {
    if (!@unlink($this->OriginalName))
    $this->SetError("Не мога да изтрия файла",__LINE__,__FILE__);
    }
    
    
    
    function chmod($mode=0750)
    {
    if (!@chmod($this->OriginalName,$mode))
    $this->SetError("Не мога да променя CHMOD-а",__LINE__,__FILE__);
    }
    
    
    
    function rn($newName)
    {
    if (!@rename($this->OriginalName,$newName))
    $this->SetError("Не мога да сменя името на файла ".$this->OriginalName." на ".$newName,__LINE__,__FILE__);
    }
    
    
    
    function makeDir($folder,$perms=0777)
    {
    if (!@is_dir($folder))
    @mkdir($folder,$perms);
    else
    $this->SetError("Папката ".$folder." съществува.",__LINE__,__FILE__);
    }
    
    
    
    function isDir($folder)
    {
    if (!@is_dir($folder))
    return false;
    else
    return true;
    }
    
    
    
    function DeleteDir($folder)
    {
    if (@is_dir($folder))
    @rmdir($folder);
    else
    $this->SetError("Не мога да изтрия папката ".$folder." тази папка не съществува.",__LINE__,__FILE__);
    }
    
    
    
    
    function OpenFile($mode="w+")
    {
    $return = @fopen($this->OriginalName,$mode);
    if (!$return)
    $this->SetError("Не мога да отворя файла ".$this->OriginalName,__LINE__,__FILE__);
    else
    {
    $this->Handler = $return;
    
    return $return;
    }
    }
    
    
    function GetContent($handler="")
    {
    $this->FileContent = (!$handler)?fread($this->Handler,$this->size):fread($handler,$this->size);
    return $this->FileContent;
    }
    
    
    function CloseFile($handler="")
    {
    return (!$handler)?fclose($this->Handler):fclose($handler);
    }
    
    
    function WriteFile($handler="")
    {
    $handler = (!$handler)?$this->Handler:$handler;
    if (!@fwrite($handler,$this->NewFileContent))
    $this->SetError("Не мога да пиша върху файла ".$this->OriginalName." the content\n\n".$Content,__LINE__,__FILE__);
    }
    
    
    function WriteFPuts($handler="")
    {
    $handler = (!$handler)?$this->Handler:$handler;
    if (!@fputs($handler,$this->NewFileContent))
    $this->SetError("Не мога да пиша върху файла ".$this->OriginalName." the content\n\n".$Content,__LINE__,__FILE__);
    }
    
    
    
    function GetFolderContent($fName)
    {
    if (!$this->isDir($fName))
    {
    $this->SetError("Съжалявам, но ".$fName." е невалидна папка",__FILE__,__LINE__);
    return false;
    }
    else
    {
    $handle=opendir($fName);
    while ($file = readdir($handle))
    if ($file!="."&&$file!="..")
    $this->FileList[] = $file;
    closedir($handle);
    }
    }
    
    
    
    function SetError($error,$linea="not specified",$archivo="undefined")
    {
    if (!$error) $this->error = "Неизвестна грешка!";
    else $this->error = $error."<br><li> Ред: ".$linea."</li><li> Файл:".$archivo;
    }
    
    }
    ?>

    Следващия код можете да го кръстите във файл, който вие си решите ето го и него:

    <?
    include("class.file.php"); //Инклудваме файла на класа в случая "class.file.php"
    //На мястото на "faila.txt" описвате пътя към файла, върху който ще работите.
    $File = new File("faila.txt");
    
    //Копиране в друга директория.
    //Пример:
    $File->CopyTo("nova_papka/imeto_na_faila.txt");
    
    //Промяна на име.
    //Пример:
    $File->rn("novo_ime_na_faila.php");
    
    //Изтриване на файла.
    //Пример:
    $File->Delete();
    
    //Създаване на папка.
    //Пример:
    $File->makeDir("ime_na_papkata");
    
    //Отваряне на файла и модифициране на съдържанието му.
    //Пример:
    $File->OpenFile("w+");
    $File->GetContent();
    $File->FileContent = "Тук пишете новото съдържание на файла.";
    $File->WriteFile();
    $File->CloseFile();
    
    //Взимане на съдържанието на папка и показването и.
    //Пример:
    $File->GetFolderContent("ime_na_papkata");
    
    for ($f=0; $f<count($File->FileList); $f++)
    echo $File->FileList[$f]."\n<br />"
    
    ?>

    Това е! Демо няма да давам, защото е излишно.

    Оставете коментар