[英]PHP rename function does not working
I want to move a file from one directory
to another directory
using PHP rename()
function.我想使用 PHP
rename()
函数将文件从one directory
移动到another directory
。 But the function is not working.但该功能不起作用。 every time it display
Not done
.每次都显示
Not done
。 Your suggestions please.请提出您的建议。
Here is my code
这是我的代码
<?php
error_reporting(1);
error_reporting('On');
include 'includes/config.php'; // database configuration file
$site_path = "http://www.website.com/";
$file_name = "images800466507.jpg";
$currentPath = 'TempImages/'.$file_name; // ( file permission : 777 )
$newPath = 'ImagesNew/'.$file_name; // ( file permission : 755 )
if( rename($site_path.$currentPath , $site_path.$newPath ) )
echo 'done';
else
echo 'not done';
?>
You need to get your real path, for me real path /Applications/MAMP/htdocs/phptest
but you can define ABSPATH
like define('ABSPATH', dirname(__FILE__).'/');
您需要获取真实路径,对我来说,它是真实路径
/Applications/MAMP/htdocs/phptest
但是您可以定义ABSPATH
例如define('ABSPATH', dirname(__FILE__).'/');
Complete code: 完整的代码:
error_reporting(1);
error_reporting('On');
define('ABSPATH', dirname(__FILE__).'/');
include 'includes/config.php'; // database configuration file
$file_name = "images800466507.jpg";
$currentPath = ABSPATH.'/TempImages/'.$file_name; // ( file permission : 777 )
$newPath = ABSPATH.'/ImagesNew/'.$file_name; // ( file permission : 755 )
if( rename($currentPath ,$newPath ) )
echo 'done';
else
echo 'not done';
I tested and it worked for me. 我进行了测试,它为我工作。
Perhaps using the full local path might do the trick 也许使用完整的本地路径可能会成功
$filename = 'images800466507.jpg';
$currentpath = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'TempImages' . DIRECTORY_SEPARATOR . $filename;
$targetpath = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'ImagesNew' . DIRECTORY_SEPARATOR . $filename;
echo realpath( $currentpath ) && rename($currentpath,$targetpath) ? 'done' : 'not done';
Does the source file exist at the location specified? 源文件是否存在于指定的位置?
if( !file_exists( $currentpath ) ) echo 'Bad Foo - ' . $currentpath . ' not found!';
Add /
before TempImages/
and ImagesNew/
directory like /TempImages/
and /ImagesNew/
添加
/
前TempImages/
和ImagesNew/
目录像/TempImages/
和/ImagesNew/
<?php
error_reporting(1);
error_reporting('On');
include 'includes/config.php'; // database configuration file
//$site_path = "http://www.website.com/";
$file_name = "images800466507.jpg";
$currentPath = '/TempImages/'.$file_name; // ( file permission : 777 )
$newPath = '/ImagesNew/'.$file_name; // ( file permission : 755 )
if( rename($currentPath , $newPath ) )
echo 'done';
else
echo 'not done';
?>
more details rename
function http://php.net/manual/en/function.rename.php 更多详细信息
rename
功能http://php.net/manual/zh/function.rename.php
获取真实路径可以提供帮助:
$file = realpath($filename);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.