zoukankan      html  css  js  c++  java
  • PHP得到根目录和删除文件,删除目录(转)

     来源:http://www.phpweblog.net/haix/archive/2009/05/13/6471.aspx
    <?php
    $file = "test.txt";
    if (!unlink($file))
    {
    echo ("Error deleting $file");
    }
    else
    {
    echo ("Deleted $file");
    }
    ?>



    php可以获得站点的根目录
    function GetRootPath()
    {
    $sRealPath = realpath( './' ) ;

    $sSelfPath = $_SERVER['PHP_SELF'] ;
    $sSelfPath = substr( $sSelfPath, 0, strrpos( $sSelfPath, '/' ) ) ;

    return substr( $sRealPath, 0, strlen( $sRealPath ) - strlen( $sSelfPath ) ) ;
    }


    通过$_SERVER["DOCUMENT_ROOT"]变量取虚拟目录

    function removeDir($dirName)
    {
    $result = false;
    if(! is_dir($dirName))
    {
    trigger_error("目录名称错误", E_USER_ERROR);
    }
    $handle = opendir($dirName);
    while(($file = readdir($handle)) !== false)
    {
    if($file != '.' && $file != '..')
    {
    $dir = $dirName . DIRECTORY_SEPARATOR . $file;
    is_dir($dir) ? removeDir($dir) : unlink($dir);
    }
    }
    closedir($handle);
    $result = rmdir($dirName) ? true : false;
    return $result;
    }


    <?php

    function remove_directory($dir) {
    if ($handle = opendir("$dir")) {
    while (false !== ($item = readdir($handle))) {
    if ($item != "." && $item != "..") {
    if (is_dir("$dir/$item")) {
    remove_directory("$dir/$item");
    } else {
    unlink("$dir/$item");
    echo " removing $dir/$item<br>\n";
    }
    }
    }
    closedir($handle);
    rmdir($dir);
    echo "removing $dir<br>\n";
    }
    }


    function removeDir($dirName)
    {
    if(! is_dir($dirName))
    {
    return false;
    }
    $handle = @opendir($dirName);
    while(($file = @readdir($handle)) !== false)
    {
    if($file != '.' && $file != '..')
    {
    $dir = $dirName . '/' . $file;
    is_dir($dir) ? removeDir($dir) : @unlink($dir);
    }
    }
    closedir($handle);

    return rmdir($dirName) ;
    }
    ?>

  • 相关阅读:
    Primitive Roots POJ
    [kuangbin带你飞]专题十四 数论基础 A
    计蒜客 最长不下降子序列 (贪心+二分nlogn算法)
    [kuangbin带你飞]专题十二 基础DP1 N
    hdu 2527 Safe Or Unsafe (优先队列实现Huffman)
    nyoj 991-Registration system (map)
    hdu 1075 What Are You Talking About (map)
    hdu 1263 水果 (嵌套 map)
    hdu 1556 Color the ball (技巧 || 线段树)
    hdu 2896 病毒侵袭 (AC自动机)
  • 原文地址:https://www.cnblogs.com/dabaopku/p/1701396.html
Copyright © 2011-2022 走看看