nqkk58 发表于 2024-10-4 15:42:47

PHP 文件处理详解


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1. 文件类型</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">重视</span>:php文件的概念<span style="color: black;">包含</span>文件和目录(文件夹),<span style="color: black;">怎样</span><span style="color: black;">无</span><span style="color: black;">尤其</span>强调,都是基于文件和目录(文件夹)操作的函数</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">以Linux为模型的, 在Windows只能获取file, dir或unknow 三种类型</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在Linux/Unix下, block, char, dir, fifo, file, link, unknown等七种类型</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">block :块设备文件, 磁盘分区, 软驱, cd-rom等</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">char: 字符设备, I/O 以字符为单位, 键盘,打印机等</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">dir: 目录<span style="color: black;">亦</span>是文件的一种</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">fifo:命名管道</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">file:普通文件类型</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">link:符号链接,<span style="color: black;">指的是</span>向文件指针的指针, 类似windows中的快捷方式</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">unknown:未知类型</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">is_dir -- 判断给定文件名<span style="color: black;">是不是</span>是一个目录</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">is_executable -- 判断给定文件名<span style="color: black;">是不是</span>可执行</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">is_file -- 判断给定文件名<span style="color: black;">是不是</span>为一个正常的文件</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">is_link -- 判断给定文件名<span style="color: black;">是不是</span>为一个符号连接</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">is_readable -- 判断给定文件名<span style="color: black;">是不是</span>可读</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">is_uploaded_file -- 判断文件<span style="color: black;">是不是</span>是<span style="color: black;">经过</span> HTTP POST 上传的</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">is_writable -- 判断给定的文件名<span style="color: black;">是不是</span>可写</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">is_writeable -- is_writable() 的别名</p><span style="color: black;"><span style="color: black;">&lt;?php</span>
      <span style="color: black;">echo</span> filetype(<span style="color: black;">"c:\\windows\\php.ini"</span>); <span style="color: black;">//双引号下需要两个反斜线</span>
      <span style="color: black;">echo</span> <span style="color: black;">"&lt;br /&gt;"</span>;
      <span style="color: black;">echo</span> filetype(<span style="color: black;">"c:\\windows"</span>);
      <span style="color: black;">echo</span> <span style="color: black;">"&lt;br /&gt;"</span>;
      <span style="color: black;">echo</span> filetype(<span style="color: black;">"C:/windows/php.ini"</span>); <span style="color: black;">//windows操作系统路径不区分<span style="color: black;">体积</span>写</span>
      <span style="color: black;">?&gt;</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2. 文件的属性</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">file_exists(); <span style="color: black;">检测</span>文件或目录<span style="color: black;">是不是</span>存在, 在utf-8字符集下, file_exists()适合判断英文和数字的文件名, 含有中文的文件是<span style="color: black;">没法</span>判断。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">处理</span><span style="color: black;">办法</span>:<span style="color: black;">运用</span>iconv()函数对中文的路径进行转码, 转化为gb2312<span style="color: black;">才可</span>识别, <span style="color: black;">倘若</span>文件是以gb2312或GBK字符集, 则无需转换, file_exists()能够判断。</p>$filepath =<span style="color: black;">"北京.jpeg"</span>;
    <span style="color: black;">if</span>(!file_exists($filepath)){
    $filepath = iconv(<span style="color: black;">"utf-8"</span>, <span style="color: black;">"gb2312"</span>, $filepath);
    <span style="color: black;">if</span>(!file_exists($filepath)){
    <span style="color: black;">die</span>(<span style="color: black;">文件路径错误</span>);
    }
    }<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">filesize(); 返回文件<span style="color: black;">体积</span>(单位字节), 用于统计文件<span style="color: black;">体积</span>, 不<span style="color: black;">包含</span>目录(文件夹);<span style="color: black;">倘若</span>统计文件夹<span style="color: black;">体积</span>, 必须用递归函数</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">is_readable();</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">is_writeable(); 用于目录判断,返回false, 返回true的文件有text/txt,text/css,text/html,js等文件</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">is_executable() 用于目录判断,返回false, 返回true的文件有sh(UNIX/LINUX 操作系统的脚本文件)</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">filectime(); 返回unix时间戳格式</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">filemtime();</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">fileatime();</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">stat(); 获取由filename指定的文件的统计信息。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3. 和文件路径<span style="color: black;">关联</span>的函数</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">相对路径:相<span style="color: black;">针对</span>当前目录的上级和下级目录</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">. 当前目录</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.. 上一级目录</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">./php/apache/index.php</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">php/apahce/index.php</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">login.php</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">./login.php</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">../images/tpl/logo.gif</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">路径分隔符号</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">linux/Unix "/"</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">windows "\"</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">DIRECTORY_SEPARATOR: 为<span style="color: black;">区别</span>平台,在Windows \ Linux /</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">DIRECTORY_SEPARATOR系统目录分隔符的<span style="color: black;">运用</span><span style="color: black;">办法</span>:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$path=e:.DIRECTORY_SEPARATOR.web.DIRECTORY_SEPARATOR.liwenkai.DIRECTORY_SEPARATOR.abc.php;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">不管是什么操作系统PHP的目录分割符号都支持 / (Linux)</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在PHP和Apache配置文件中<span style="color: black;">倘若</span>需要指定目录, <span style="color: black;">亦</span><span style="color: black;">运用</span>/<span style="color: black;">做为</span>目录符号</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">绝对路径:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/ 根路径</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/images/index.php</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">指的操作系统的根</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">指的是存放网站的文档根目录</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">分<span style="color: black;">状况</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span>是在服务器中执行(<span style="color: black;">经过</span>PHP文件处理函数执行)路径 则"根"指的<span style="color: black;">便是</span>操作系统的根</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span>程序是下载的客户端,再<span style="color: black;">拜访</span>服务器中的文件时,<span style="color: black;">仅有</span><span style="color: black;">经过</span>Apache<span style="color: black;">拜访</span>,"根"<span style="color: black;">亦</span>就指的是文档根目录</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">http://www.xxxxxx.com/logo.gif</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">关于路径<span style="color: black;">是不是</span>区分<span style="color: black;">体积</span>写的问题:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">linux操作系统路径是区分<span style="color: black;">体积</span>写的,而windows操作系统不区分<span style="color: black;">体积</span>写</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">basename(url)</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">dirname(url)</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">pathinfo(url)</p><span style="color: black;"><span style="color: black;">&lt;?php</span>
      $path =<span style="color: black;">"/usr/www/html/index.php"</span>;
      <span style="color: black;">echo</span> basename($path).<span style="color: black;">"&lt;br&gt;"</span>; <span style="color: black;">//index.php</span>

      <span style="color: black;">//<span style="color: black;">倘若</span><span style="color: black;">选取</span>suffix则忽略扩展名</span>
      <span style="color: black;">echo</span> basename($path,<span style="color: black;">".php"</span>); <span style="color: black;">//index</span>
      <span style="color: black;">?&gt;</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">处理</span>: basename()<span style="color: black;">没法</span>正确获取带中文字符的文件名</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">basename()函数依赖于区域, <span style="color: black;">咱们</span>只需设置区域:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">setlocale(LC_ALL, zh_CN.UTF8);</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">setlocale() 函数设置地区信息(地域信息)。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">地区信息是针对一个地理区域的语言、货币、时间以及其他信息。</p><span style="color: black;"><span style="color: black;">&lt;?php</span>
      $path_parts = pathinfo(<span style="color: black;">"C:/AppServ/www/52ekt/wenjian/test.txt"</span>);
      <span style="color: black;">echo</span> $path_parts[<span style="color: black;">"dirname"</span>].<span style="color: black;">"&lt;br /&gt;"</span>;
      <span style="color: black;">echo</span> $path_parts[<span style="color: black;">"basename"</span>].<span style="color: black;">"&lt;br /&gt;"</span>;
      <span style="color: black;">echo</span> $path_parts[<span style="color: black;">"extension"</span>];
      <span style="color: black;">?&gt;</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">输出结果: </p>C:/AppServ/www/52ekt/wenjian

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">test.txt</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">txt</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">以上1-3点基于文件和目录(文件夹)操作的函数</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">实例: <span style="color: black;">怎样</span>获取上传文件名(不<span style="color: black;">包含</span>后缀名)</p><span style="color: black;">$filename</span> = <span style="color: black;">"test.txt"</span>;
    <span style="color: black;">$ext</span> = substr(strrchr(<span style="color: black;">$filename</span>, <span style="color: black;">.</span>), 1);
    <span style="color: black;">$result</span> = basename(<span style="color: black;">$filename</span>,<span style="color: black;">"."</span>.<span style="color: black;">$ext</span>);
    <span style="color: black;">echo</span> <span style="color: black;">$result</span>; //<span style="color: black;">test</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">编写自定义函数</p><span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">getFileName</span><span style="color: black;">($originname)</span></span>{
    $filename = basename($originname);<span style="color: black;">//获取test.txt</span>
    $ext = substr(strrchr($filename, <span style="color: black;">.</span>), <span style="color: black;">1</span>); <span style="color: black;">//获取扩展名</span>
    $result = basename($filename,<span style="color: black;">"."</span>.$ext);
    <span style="color: black;">return</span>$result;
    }
    $filename =<span style="color: black;">"C:/AppServ/www/52ekt/wenjian/test.txt"</span>;
    <span style="color: black;">echo</span> getFileName($filename); <span style="color: black;">//test</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">realpath 返回规范化的绝对路径名</p><span style="color: black;"><span style="color: black;">&lt;?php</span>
      $path =<span style="color: black;">"./test2.php"</span>;
      $realpath= realpath($path);<span style="color: black;">echo</span> $realpath; <span style="color: black;">//D:\phpStudy\WWW\study\test2.php</span>
      <span style="color: black;">?&gt;</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">PHP自带的basename函数不支持中文的<span style="color: black;">处理</span>办法</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">办法</span>一:</p>array_pop(explode(<span style="color: black;">"/"</span>,<span style="color: black;">$path</span>));<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">办法</span>二:</p><span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">get_basename</span><span style="color: black;">($path)</span></span>{
    <span style="color: black;">return</span> preg_replace(<span style="color: black;">/^.+[\\\\\\/]/</span>, , $path);
    }<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">4. 文件的操作<span style="color: black;">关联</span>的函数(基于文件操作,不<span style="color: black;">包含</span>目录(文件夹))</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">创建文件 touch("文件名") 文件名要<span style="color: black;">包括</span>完整路径</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">删除文件 unlink("文件路径"); 只能删除文件, 删除文件夹采用rmdir()函数</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">移动文件 为文件重新命名 rename("当前文件路径", "目录为文件路径”)</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">复制文件 copy("当前", "<span style="color: black;">目的</span>"); 必须填写完整的路径(<span style="color: black;">包含</span>文件名), 其<span style="color: black;">能够</span>把在线的<span style="color: black;">照片</span>复制到本地。<span style="color: black;">倘若</span><span style="color: black;">目的</span>文件名存在, 则会覆盖;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">截取文件 ftruncate() 将文件截断到给定的长度</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">bool ftruncate ( resource $handle , int $size )</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">其中复制和移动(用copy函数)、统计文件<span style="color: black;">体积</span>、删除目录等(应用文件夹环境下),必须用递归函数,<span style="color: black;">仔细</span>可见:php|自定义函数|复制和移动目录</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">php|自定义函数|统计目录<span style="color: black;">体积</span>和php|自定义函数|删除目录</p><span style="color: black;">touch</span>(<span style="color: black;">"./php.apache"</span>); <span style="color: black;">//创建一个为文件</span>
    <span style="color: black;">unlink</span>(<span style="color: black;">"C:/AppServ/www/xsphp/apache.php"</span>);
    <span style="color: black;">rename</span>(<span style="color: black;">"./test.txt"</span>, <span style="color: black;">"d:/test2.txt"</span>); 移动文件
    <span style="color: black;">copy</span>(<span style="color: black;">"cache.txt"</span>, <span style="color: black;">"./cache5.txt"</span>);<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">必定</span>要有PHP执行这个文件权限, Apache, 一个用户</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">ls -l 或 ll</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">_rwxrwxrwx 777</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">类型 _:<span style="color: black;">暗示</span>是文件 d:<span style="color: black;">暗示</span>是目录 l:符号链接文件 b:块文件</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">rwx 表这个文件的<span style="color: black;">持有</span>者 r读 w写 x执行</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">rwx 表这个文件的<span style="color: black;">持有</span>者所在的组 r读 w写 x执行</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">rwx 其它用户对这个为文件的权限 r读 w写 x执行</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">r 4 示使文件可读</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">w 2 <span style="color: black;">暗示</span>使文件可写</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">x 1 <span style="color: black;">暗示</span>使文件可执行</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">怎样</span>删除目录及其目录下所有的文件</p><span style="color: black;"><span style="color: black;">&lt;?php</span>
      <span style="color: black;">foreach</span> (scandir($dir) <span style="color: black;">as</span> $item) {
      <span style="color: black;">if</span> ($item == <span style="color: black;">.</span> || $item == <span style="color: black;">..</span>) <span style="color: black;">continue</span>;
      unlink($dir.DIRECTORY_SEPARATOR.$item);
      }
      rmdir($dir);<span style="color: black;">?&gt;</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">5 下列函数是基于LINUX操作系统, windows环境<span style="color: black;">不可</span>操作</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">chgrp() -- 改变文件所属的组</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">chown() -- 改变文件的所有者</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">filegroup() -- 取得文件的组</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">fileowner() -- 取得文件的所有者</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">chmod() -- 改变文件模式</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">bool chmod ( string $filename , int $mode )</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">尝试将 filename 所指定文件的模式改成 mode 所给定的。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">重视</span> mode 不会被自动当成八进制数值,<span style="color: black;">况且</span><span style="color: black;">亦</span><span style="color: black;">不可</span>用字符串(例如 "g+w")。要<span style="color: black;">保证</span>正确操作, 需要给 mode 前面加上 0:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">正确写法:</p><span style="color: black;">chmod</span>(<span style="color: black;">"/somedir/somefile"</span>, <span style="color: black;">0755</span>); <span style="color: black;">// 八进制数,正确的 mode 值</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">错误的写法:</p><span style="color: black;">chmod</span>(<span style="color: black;">"/somedir/somefile"</span>, <span style="color: black;">755</span>); <span style="color: black;">// 十进制数,可能不对</span>
    <span style="color: black;">chmod</span>(<span style="color: black;">"/somedir/somefile"</span>, <span style="color: black;">"u+rwx,go+rx"</span>); <span style="color: black;">// 字符串,不对</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">取得文件权限 int fileperms ( string $filename ) 返回的是八进制数</p><span style="color: black;">printf</span>(<span style="color: black;">%o</span>,fileperms(<span style="color: black;">"data.txt"</span>)); <span style="color: black;">//100666</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">无论是Windows操作系统, 还是Linux操作系统, 其权限都是采用八进制进行表述的</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">windows操作系统权限返回的结果都为(40xxx或100xxx)</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">40:<span style="color: black;">表率</span>文件类型, 文件夹</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">100:<span style="color: black;">表率</span>文件类型, 文件</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">666:<span style="color: black;">表率</span>文件权限, 一共分为三部分(<span style="color: black;">持有</span>者权限, 组内用户权限, 组外用户的权限)</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">其实任何的一个文件都有三个属性:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">r(read缩写): 可读 4</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">w(write缩写): 可写 2</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">x(execute缩写):可执行 1</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">7=(4+2+1) 6(4+2) 5(4+1)</p><span style="color: black;"><span style="color: black;">&lt;?php</span>
      <span style="color: black;">echo</span> sprintf ( <span style="color: black;">%o</span> , fileperms ( <span style="color: black;">../20170528</span> )); <span style="color: black;">//40777</span>
      <span style="color: black;">echo</span> <span style="color: black;">"&lt;br&gt;"</span>;
      <span style="color: black;">echo</span>sprintf (<span style="color: black;">%o</span> , fileperms ( <span style="color: black;">youxianji.php</span> )); <span style="color: black;">//100666</span>
      <span style="color: black;">?&gt;</span></span>




7wu1wm0 发表于 2024-10-13 05:18:55

外贸网站建设方法 http://www.fok120.com/

4lqedz 发表于 2024-10-17 05:03:36

楼主的文章深得我心,表示由衷的感谢!

1fy07h 发表于 2024-10-18 15:58:24

楼主的文章非常有意义,提升了我的知识水平。

j8typz 发表于 2024-10-19 02:34:12

你的话语真是温暖如春,让我心生感激。

4zhvml8 发表于 2024-11-9 08:55:44

感谢你的精彩评论,带给我新的思考角度。

nykek5i 发表于 6 天前

这夸赞甜到心里,让我感觉温暖无比。

wrjc1hod 发表于 6 天前

论坛的成功是建立在我们诚恳、务实、高效、创新和团结合作基础上,我们要把这种精神传递下去。
页: [1]
查看完整版本: PHP 文件处理详解