【问题标题】:PHP rename function does not workingPHP重命名功能不起作用
【发布时间】:2022-01-17 09:37:33
【问题描述】:

我想使用 PHP rename() 函数将文件从 one directory 移动到 another directory。但该功能不起作用。每次显示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';
?>

【问题讨论】:

  • 你应该使用本地路径。
  • 本地路径意味着..???您是在谈论绝对路径..?? '/var/www/website.com/public_html/'
  • 本地路径表示您的项目文件夹路径。不是在线网站名称
  • 不是http://www.website.com,而是更像/var/www/temp_images/ 或本地路径的任何地方
  • '/var/www/website.com/public_html/' ......????

标签: php rename


【解决方案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';

我测试过,它对我有用。

【讨论】:

  • 我更新了代码,它应该可以在现场没有任何问题
【解决方案2】:

也许使用完整的本地路径可以解决问题

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

【讨论】:

    【解决方案3】:

    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

    【讨论】:

    • @ShanBiswas 你试过了吗dirname(__FILE__)like $currentPath = ABSPATH.'/TempImages/'.$file_name;
    • 根据您的问题,所有 3 个答案都是正确的。我认为你的问题是另一个地方。
    【解决方案4】:

    获取真实路径会有所帮助:

    $file = realpath($filename);
    

    但这听起来可能是权限问题或 SELinux 问题。您将需要 apache 成为相关文件的组,并且文件应该可由该组写入。

    另外,如果您启用了 SELinux,则需要使用

    semanage fcontext -a -t httpd_sys_rw_content_t "/path/to/images/folder(/.*)?"
    

    restorecon -R -v /path/to/images/folder
    

    【讨论】:

      猜你喜欢
      • 2017-02-02
      • 2015-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-20
      • 2013-04-18
      • 2011-11-03
      • 2018-07-17
      相关资源
      最近更新 更多