2017-04-20 1 views
0

PHP를 사용하여 별도의 디렉토리에있는 파일의 이름을 바꾸려고합니다. 이것이 제가 시도한 것입니다.파일이 스크립트와 다른 디렉토리에있을 때 PHP에서 파일의 이름을 바꾸려면 어떻게합니까?

<?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'); 

?> 

그러나 이전 디렉토리에서 여전히 파일을 찾는 중 오류가 발생합니다.

rename($file_dir, newfilename.txt): No such file or directory in /home/name/there/myhandler.php 

답변

2

내가 실제로 내 코드에서 그런 식으로했다

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

이 코드는 'rename ('full/file/path/name.txt ','newname.txt ')이라는 코드가있는 모든 예제에서 작동했습니다. –

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'); 

?> 
+0

newname은에 대한 올바른 경로를 지정하십시오 사용할 수 있습니다 간결하게하기 위해 여기에. 나는 그것을 바로 잡을 것이다! –

관련 문제