重命名PHP中的文件 [英] rename file in php
问题描述
我想从此代码将picture
文件名(不带扩展名)重命名为old.jpg
.
I want to rename picture
filename (without extension) to old.jpg
from this code.
我在父目录中有picture
文件,并且路径正确
I have picture
file in parent directory and the path is correctly
$old="picture";
$new="old.jpg";
rename($old , $new);
或此代码
$old="\picture";
$new="\old.jpg";
rename($old , $new);
$old="../picture";
$new="../old.jpg";
rename($old , $new);
$old="../picture";
$new="old.jpg";
rename($old , $new);
$old="./picture";
$new="./old.jpg";
rename($old , $new);
rename("picture", "old.jpg");
但是我得到这个错误:
Warning: rename(picture,old.jpg) [function.rename]: The system cannot find the file specified. (code: 2) in C:\xampp\htdocs\prj\change.php on line 21
推荐答案
相对路径基于正在执行的脚本(在Web服务器中运行时为$_SERVER['SCRIPT_FILENAME']
),该脚本并不总是文件操作所用的文件地点:
A relative path is based on the script that's being executed ($_SERVER['SCRIPT_FILENAME']
when run in web server) which is not always the file in which the file operation takes place:
// index.php
include('includes/mylib.php');
// mylib.php
rename('picture', 'img506.jpg'); // looks for 'picture' in ../
查找相对路径涉及比较执行脚本和您要操作的文件的绝对路径,例如:
Finding a relative path involves comparing the absolute paths of both the executing script and the file you wish to operate on, e.g.:
/var/www/html/index.php
/var/www/images/picture
在此示例中,相对路径为:../images/picture
In this example, the relative path is: ../images/picture
这篇关于重命名PHP中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!