copy() 函数返回 true,但该文件不会在 PHP 中更新


copy() function returns true, but the file is not updated in php

在我的PHP应用程序中,copy()函数工作正常,但在服务器中不起作用。

$new_file = 'some_new_file';
$old_file = 'existing_file';
if ( copy($new_file, $old_file) ) {
        return new JsonResponse(array('status' => true,'success' => 'File saved successfully'));
        //print_r("Copy success!");
    }else{
        return new JsonResponse(array('status' => false,'success' => 'Problem in saving file'));
    }

它返回的状态为 true ,但$old_file不会更新。

在这个 even copy() 返回 true,因为我得到的响应是

{"status":true,"success":"File saved successfully"}

看起来你交换了参数来copy。从 php.net 的文档中:

<?php
$file = 'example.txt';
$newfile = 'example.txt.bak';
if (!copy($file, $newfile)) {
    echo "failed to copy $file...'n";
}

如您所见,首先指定源文件,其次指定目标文件。


特定于您的代码,请尝试更改

copy($new_file, $old_file)

copy($old_file, $new_file)

一旦ckeck这个文件权限777需要这个