当文件位于与脚本不同的目录中时,如何在PHP中重命名文件?

时间:2017-04-20 00:55:47

标签: php rename

我正在尝试使用PHP重命名位于单独目录中的文件。这就是我的尝试。

from graphics import *

RADIUS = 25

def redCircles(win):
    outline_template = Circle(Point(0, 0), RADIUS)
    outline_template.setOutline('red')

    fill_template = outline_template.clone()
    fill_template.setFill('red')

    horizontal_template = Rectangle(Point(0, 0), Point(RADIUS * 2, RADIUS))
    horizontal_template.setFill('white')
    horizontal_template.setOutline('white')

    vertical_template = Rectangle(Point(0, 0), Point(RADIUS, RADIUS * 2))
    vertical_template.setFill('white')
    vertical_template.setOutline('white')

    for parity, x in enumerate(range(RADIUS, RADIUS * 10, RADIUS * 2)):

        for y in range(RADIUS, RADIUS * 10, RADIUS * 2):

            fill = fill_template.clone()
            fill.move(x, y)
            fill.draw(win)

            if parity % 2 == 1:
                rectangle = horizontal_template.clone()
                rectangle.move(x - RADIUS, y)
            else:
                rectangle = vertical_template.clone()
                rectangle.move(x - RADIUS, y - RADIUS)

            rectangle.draw(win)

            outline = outline_template.clone()
            outline.move(x, y)
            outline.draw(win)

if __name__ == '__main__':
    win = GraphWin('Patch2', RADIUS * 10, RADIUS * 10)

    redCircles(win)

    win.getMouse()
    win.close()

但是,我收到一个错误,它仍然在旧目录中查找该文件。

<?php

//file i want to change
$file_dir = '/home/name/here/myfile.txt';

//directory of script
$cur_dir = '/home/name/there/myhandler.php';

//change directory to location of file to be renamed
chdir('/home/name/here/');

//change file name
rename($file_dir, 'newfilename.txt');

?>

2 个答案:

答案 0 :(得分:2)

尝试为新名称指定正确的路径

//change file name
rename('/home/name/here/myfile.txt', '/home/name/here/newfile.txt');

答案 1 :(得分:0)

$ file_dir需要是目录名,而不是chdir的文件名。您不需要脚本目录,也可以使用__DIR __

<?php

//file i want to change
$file_dir = '/home/name/here';

//change directory to location of file to be renamed
chdir($file_dir);

//change file name
rename('filename.txt', 'newfilename.txt');

?>