zoukankan      html  css  js  c++  java
  • PHP判断文件或者目录是否可写

    在PHP中,可用is_writable()函数来判断一个 文件/目录 是否可写,详情如下:

    参考

    is_writable

    (PHP 4, PHP 5)

    is_writable — 判断给定的文件名是否可写

    说明

    bool is_writable ( string $filename )

    如果文件存在并且可写则返回 TRUE。($filename 参数可以是一个目录名,即检查目录是否可写。 )

    记住 PHP 也许只能以运行 webserver 的用户名(通常为 'nobody')来访问文件。不计入安全模式的限制。

    Example #1 is_writable() 例子

    <?php
    $filename 'test.txt';
    if (is_writable($filename)) {
        echo 'The file is writable';
    else {
        echo 'The file is not writable';
    }
    ?>
     

    注意:is_writeable() 是 is_writable() 的别名!


    但是,上面那个函数在PHP4中是有BUG的,尤其是在Windows服务器下判断不准,官方相关bug报告链接如下:

    http://bugs.php.net/bug.php?id=27609

    为了兼容各个操作系统,可自定义一个判断可写函数,代码如下:

    /**
     * 判断 文件/目录 是否可写(取代系统自带的 is_writeable 函数)
     *
     * @param string $file 文件/目录
     * @return boolean
     */

    function new_is_writeable($file) {

        if (is_dir($file)){
            $dir $file;
            if ($fp = @fopen("$dir/test.txt"'w')) {
                @fclose($fp);
                @unlink("$dir/test.txt");
                $writeable = 1;
            else {
                $writeable = 0;
            }
        else {
            if ($fp = @fopen($file'a+')) {
                @fclose($fp);
                $writeable = 1;
            else {
                $writeable = 0;
            }
        }
     
        return $writeable;
    }
     
  • 相关阅读:
    对抗杀软高级启发(主动防御)技术
    __readfsdword __readgsqword PEB TEB
    net1 user
    vc 获取windows 网卡信息
    RunPE
    windows crpyt API 实现DES 3DES 3DES_112 加解密
    软件开发图标收藏
    有关windows 网卡信息获取
    BCGPContorBar 静态工程设置详解
    (二)特征匹配
  • 原文地址:https://www.cnblogs.com/ithubb/p/13261954.html
Copyright © 2011-2022 走看看