Swathi
Swathi

Reputation: 19

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

In my PHP app, copy() function works fine, but is not working in server.

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

It is returning status as true, but $old_file is not updated.

In this even copy() returns true, because the response what I'm getting is

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

Upvotes: 0

Views: 497

Answers (2)

abhayendra
abhayendra

Reputation: 187

once ckeck this file permission 777 need for this

Upvotes: 1

Robin Kanters
Robin Kanters

Reputation: 5146

Looks like you swapped the arguments to copy around. From the documentation on php.net:

<?php
$file = 'example.txt';
$newfile = 'example.txt.bak';

if (!copy($file, $newfile)) {
    echo "failed to copy $file...\n";
}

As you can see, the source file is specified first, the destination second.


Specific to your code, try changing

copy($new_file, $old_file)

to

copy($old_file, $new_file)

Upvotes: 2

Related Questions