What does dirname(__FILE__) and basename(dirname(__FILE__)) do?
来源:互联网 发布:代源码迅雷 编辑:程序博客网 时间:2024/06/03 19:22
from:http://thedaneshproject.com/posts/what-does-dirname__file__-and-basenamedirname__file__-do/
Was helping a friend fix his php script today. He was not too sure about what “dirname(__FILE__)” did.
dirname() is a PHP function which returns the directory name of a file. For example if file abc.txt was in “/tmp/abc.txt” then the dirname() function would return “/tmp” .
Example Usage;
<?php
$file = “/tmp/abc.txt”;
$path = dirname($file); // $path will now contain /tmp
?>
What does dirname(__FILE__) and basename(dirname(__FILE__)) do then?
The __FILE__ constant represents the running script. It will return the full path and file name of the running script. For example, the __FILE__ constant on my server would return “/var/www/html/index.php” for my index.php file which is in the “/var/www/html/” directory.
The basename() command is normally used in conjunction with the dirname() function to strip the parent directory from a full file name. For example “/var/www/html/abc.txt” when passed through basename() would return abc.txt. basename() also works on directories. So, basename() on “/var/www/html” would return “html” since in Linux directories are files.
Example Usage;
Imagine __FILE__ represents /var/www/html/index.php
<?php
echo dirname(__FILE__); // returns /var/www/html
echo basename(__FILE__); //returns index.php
echo basename(dirname(__FILE__)); //returns html
?>
- What does dirname(__FILE__) and basename(dirname(__FILE__)) do?
- php basename dirname __FILE__
- dirname(__FILE__)
- dirname(__FILE__)
- dirname(__FILE__);
- __FILE__和dirname(__FILE__)
- __FILE__、dirname(__FILE__) 、 dirname(dirname(__FILE__))的使用
- dirname(__FILE__).DIRECTORY_SEPARATOR
- dirname( __FILE__ ).DIRECTORY_SEPARATOR;
- dirname(__FILE__) 'DOCUMENT_ROOT'
- PHP 常量 dirname(__file__)
- dirname(__FILE__).DIRECTORY_SEPARATOR
- dirname(__FILE__) 用法
- php dirname(__FILE__)
- dirname(__FILE__)的意思
- php dirname(__FILE__)
- dirname(__FILE__) 的解释
- dirname(__FILE__)及相关
- ik-expression 表达式解析执行器
- Hashcode作用以及hash算法的理解
- EditPlus 3.12 注册码
- 数据库连接字符串大全
- happy
- What does dirname(__FILE__) and basename(dirname(__FILE__)) do?
- 转载-linux 2.6 进程总结
- css100%高度
- 指向const对象的指针和const指针 (转)
- Protocol Buffers学习小记-基于servlet的http RPC实现
- grub4dos初级教程-入门篇
- java解惑之静态方法覆盖
- VC常用控件用法
- 转载 linux 2.6进程与线程