PHP重命名功能不起作用

时间:2016-11-14 07:43:02

标签: php rename

我想使用PHP one directory函数将文件从another directory移动到rename()。但功能不起作用。每次显示Not done。请你的建议。

  

这是我的代码

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

3 个答案:

答案 0 :(得分:1)

你需要获得真正的路径,对我来说是真正的路径/Applications/MAMP/htdocs/phptest,但你可以定义ABSPATH,如define('ABSPATH', dirname(__FILE__).'/');

完整代码:

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

我测试过,它对我有用。

答案 1 :(得分:0)

也许使用完整的本地路径可能会发挥作用

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

源文件是否存在于指定的位置?

if( !file_exists( $currentpath ) ) echo 'Bad Foo - ' . $currentpath . ' not found!';

答案 2 :(得分:0)

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

更多详情rename函数http://php.net/manual/en/function.rename.php