PHP學習記錄之檔案的處理三

來源:互聯網
上載者:User

標籤:

1.file_exists()判斷檔案是否存在:

<?php $path=‘E:\PHP\phpStudy\WW\20160722\demo1.php‘; if(file_exists($path)){ echo ‘已存在這個檔案‘;}else{$fp=fopen($path,‘w‘);echo ‘建立成功‘;fclose($fp);}?>

2.unlink刪除一個檔案

<? $path=‘E:\PHP\phpStudy\WW\20160722\demo3.php‘; if(file_exists($path)){unlink($path);echo ‘刪除成功‘;}else{echo ‘沒有找到這個檔案‘;}?>

3.rewind 將指標複位到開始,ftell指標的所在位置,fseek指標定位在哪個位置

<? $path=‘E:\PHP\phpStudy\WW\20160722\file.txt‘; $fp=fopen($path,‘r‘); echo fgetc($fp); echo fgetc($fp);
echo ftell($fp);//指標的所在位置應該是2//此時游標應該在第2個字元上 rewind($fp);//已經複位echo fgetc($fp);
echo fseek($fp,20);//指標定位在20位置上的字元 fclose($fp);?>

4.flock簡單的鎖定配合LOCK_EX和LOCK_UN使用

<?php $path=‘E:\PHP\phpStudy\WW\20160722\file.txt‘; $fp=fopen($path,‘w‘); flock($fp,LOCK_EX);//鎖定 fwrite($fp,‘要被寫入進去‘); flock($fp,LOCK_UN);//解鎖 fclose($fp);?>

5.opendir與closedir的用法以及readdir scandir可以用數組都去出目錄

<?php $path=‘E:\PHP\phpStudy\WW\20160722‘; $dr=opendir($path);while($rd=readdir($dr)){ echo $rd.‘<br />‘;}closedir($dr);?>
<?php
print_r(scandir(‘E:\PHP\phpStudy\WW\20160722‘));
?>

6.rmdir刪除一個目錄

<?php$path=‘E:\PHP\phpStudy\WW\20160722\letsee‘; if(file_exists($path)){rmdir($path);echo ‘刪除成功‘;}else{echo‘沒有此目錄‘;}?>

7.rename更換檔案名稱

<?php rename(‘file.txt‘,‘file3.txt‘);?>

  

  

  

PHP學習記錄之檔案的處理三

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.