php檔案操作的簡單例子

來源:互聯網
上載者:User
 代碼如下 複製代碼

//擷取檔案的主要資訊。
$file = "data.txt";

if(is_dir($file))
{
    echo "檔案 $file 是個目錄";
    echo "<br/>";
}
else
{
    echo "檔案 $file 不是目錄";
    echo "<br/>";
}

if(is_file($file))
{
    echo "檔案 $file 是一個普通檔案";
    echo "<br/>";
}

if(is_readable($file))
{
    echo "檔案 $file 是可讀的";
    echo "<br/>";
}
else
{
    echo "檔案 $file 是不可讀的";
    echo "<br/>";
}

if(is_writeable($file))
{
   echo "檔案 $file 是可寫的";
   echo "<br/>";
}
else
{
   echo "檔案 $file 是不可寫的";
   echo "<br/>";
}
//判斷檔案的性質。
$path = "/home/prog/php/sayhello.php";

$file_name = basename($path);
$dir_name = dirname($path);

echo "完整路徑:".$path;
echo "<hr>";
echo "<br/>";

echo "其中目錄名為:".$dir_name;
echo "<br/>";
echo "其中檔案名稱為:".$file_name;
echo "<br/>";
//擷取檔案名稱和目錄名。
$file = "data.txt";
$dir = "info/newdata";

if(file_exists($file))
{
    echo "目前的目錄中,檔案".$file."存在";
    echo "<br/>";
}
else
{
     echo "目前的目錄中,檔案".$file."不存在";
     echo "<br/>";
}
echo "<br/>";
echo "<hr>";
echo "<br/>";

if(file_exists($dir))
{
    echo "目前的目錄下,目錄".$dir."存在";
    echo "<br/>";
}
else
{
     echo "目前的目錄下,目錄".$dir."不存在";
     echo "<br/>";
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.