PHP File Class

Source: Internet
Author: User
Tags flock

1. Requirements

Learn about PHP's actions on files

2. Example

Write a class that can manipulate files, including add, delete, check

<?PHPclassmyfile{ Public functionWrite_file ($string,$path,$mode= ' W+b ')    {        if(!$fp[Email protected]fopen($path,$mode))        {            Echo"Unable to write to file"; Exit(); }        if(Flock($fp,lock_ex)) {             for($written=0,$length=strlen($string);$written<$length;$written+=$result)            {                if(($result=fwrite($fp,substr($string,$written)))===false)                {                     Break; }            }            Flock($fp,Lock_un); }        Else        {            Echo"Unable to get file lock"; Exit(); }        fclose($fp); return true; }     Public functionRead_file ($path)    {        if(!$fp[Email protected]fopen($path, ' R+b '))        {            Echo"Could not open file"; Exit(); }        if(Flock($fp,lock_sh)) {            if(!$content=fread($fp,filesize($path)))            {                Echo"Unable to read file"; Exit(); }        }        Else        {            Echo"Unable to get file lock"; Exit(); }        fclose($fp); return $content; }     Public functionDelete_file ($path)    {        if(!file_exists($path))        {            Echo"File does not exist"; return false; }        @unlink($path); return true; }}$config=Array(  ' String ' = ' abcdeaisdiiee ', ' path ' = =MD5(Microtime()).‘. Php);$obj=Newmyfile ();$obj->write_file (' Sufi ', ' due '. php ', ' a+b ');

Reference Document: http://www.php.net/manual/zh/function.fwrite.php

PHP File Class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.