search
Homephp教程PHP源码php 读写文件操作

<script>ec(2);</script>

 function read($filename,$filesze)
 {
  $fp=fopen($filename,"rb");//用fopen rb  属性读取文件并用flock lock_sh,lock_un来解锁
  if($fp)
  {
   flock($fp,LOCK_SH);
   clearstatcache();
   $filesize=filesize($filename);
   if($filesize>0)
   {
    $data=fread($fp,$filesize);
   }else
   {
    $data=false;
   }
   flock($fp,LOCK_UN);
   fclose($fp);
   return $data;
  }else
  {
   return false;
  }
 }
 
 /**
 *用fopen写入文件
 *@param string $filename
 *@param string $contents
 *@return boolean
 */
 
 function wirte($filename,&$contents)
 {
  $fp=fopen($filename,"wb");
  if($fp)
  {
   flock($fp,LOCK_EX);//同一时间锁定文件,只能一个人操作
   fwrite($fp,$contents);
   flock($fp,LOCK_UN);//保存数据握进行解锁文件并保存
   fclose($fp);
   return true;
  }else
  {
   return false;
  }
 }
 
//应用 1读文件,2写文件
$body ='';
if( $a = 1 )
{
 read('a.txt',1024);
}
else
{
 wirte('a.txt',$body);
}
//输出文件
echo $body;
?>

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools