Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

PHP rename() not working to move files

Johna

Frontend Developer
Staff Team
Guardian
I have a file called index.php (located in the root folder of the website) which creates a file called cat.php in the root folder of the website.
PHP:
<?php
$title = "cat";
$image = "cat_pics";
$desc = "funny cat pictures";

$content = "<?php include '_data/head.php'; ?><title>" . $title . " | Crazy Chicken Girl</title><?php include '_data/header.php'; ?><?php echo 'Title: " . $title . "'; echo '\nImage: " . $image . "'; echo '\nDescription: " . $desc . "'; ?><?php include '_data/footer.php'; ?>";

$newfile = fopen($title . ".php", "w");
fwrite($newfile, $content);
fclose($newfile);
?>

I want to move the cat.php file to a folder and rename the file index.php, so that the file path will be /cat/index.php.
After a bit of searching I saw that you can move a file using the rename() function, so I added this to my code:
PHP:
$path = $title . ".php";
$newpath = $title . "/index.php";
rename($path, $newpath);


After adding the rename() function, I have this code, but the rename() function doesn't seem to move the file.
PHP:
<?php
$title = "cat";
$image = "cat_pics";
$desc = "funny cat pictures";

$content = "<?php include '_data/head.php'; ?><title>" . $title . " | Crazy Chicken Girl</title><?php include '_data/header.php'; ?><?php echo 'Title: " . $title . "'; echo '\nImage: " . $image . "'; echo '\nDescription: " . $desc . "'; ?><?php include '_data/footer.php'; ?>";

$newfile = fopen($title . ".php", "w");
fwrite($newfile, $content);
fclose($newfile);

$path = $title . ".php";
$newpath = $title . "/index.php";
rename($path, $newpath);
?>
 
Last edited:
Solution
I can see the cat.php, but there is no cat folder
ok the fun part lol
you're going to want to check if the folder already exists, if it doesn't then go ahead and create it
Code:
if (!is_dir(dirname($folderName))) {
    mkdir(dirname($folderName), 0777, true);
}
then make your call to rename
I have a file called index.php (located in the root folder of the website) which creates a file called cat.php in the root folder of the website.
PHP:
<?php
$title = "cat";
$image = "cat_pics";
$desc = "funny cat pictures";

$content = "<?php include '_data/head.php'; ?><title>" . $title . " | Crazy Chicken Girl</title><?php include '_data/header.php'; ?><?php echo 'Title: " . $title . "'; echo '\nImage: " . $image . "'; echo '\nDescription: " . $desc . "'; ?><?php include '_data/footer.php'; ?>";

$newfile = fopen($title . ".php", "w");
fwrite($newfile, $content);
fclose($newfile);
?>

I want to move the cat.php file to a folder and rename the file index.php, so that the file path will be /cat/index.php.
After a bit of searching I saw that you can move a file using the rename() function, so I added this to my code:
PHP:
$path = $title . ".php";
$newpath = $title . "/index.php";
rename($path, $newpath);


After adding the rename() function, I have this code, but the rename() function doesn't seem to move the file.
PHP:
<?php
$title = "cat";
$image = "cat_pics";
$desc = "funny cat pictures";

$content = "<?php include '_data/head.php'; ?><title>" . $title . " | Crazy Chicken Girl</title><?php include '_data/header.php'; ?><?php echo 'Title: " . $title . "'; echo '\nImage: " . $image . "'; echo '\nDescription: " . $desc . "'; ?><?php include '_data/footer.php'; ?>";

$newfile = fopen($title . ".php", "w");
fwrite($newfile, $content);
fclose($newfile);

$path = $title . ".php";
$newpath = $title . "/index.php";
rename($path, $newpath);
?>
So question for ya...
Can you walk me through what is going on in this snippet?
Code:
$path = $title . ".php";
$newpath = $title . "/index.php";
rename($path, $newpath);

;)
 
In the first line, I'm creating a variable called $path, which combines the variable $title ($title = "cat" in line 2) and the string ".php", which should make $path equal cat.php

The second line is the same as the first, but with a few things changed, so $newpath is cat/index.php

In the third line I use the rename function to rename the file cat.php ($path) to cat/index.php ($newpath). Doing this rename is supposed to move the file to a folder called cat, and rename the file to index.php

Link where I got that rename() moves files: https://thisinterestsme.com/move-file-php
 
In the first line, I'm creating a variable called $path, which combines the variable $title ($title = "cat" in line 2) and the string ".php", which should make $path equal cat.php

The second line is the same as the first, but with a few things changed, so $newpath is cat/index.php

In the third line I use the rename function to rename the file cat.php ($path) to cat/index.php ($newpath). Doing this rename is supposed to move the file to a folder called cat, and rename the file to index.php

Link where I got that rename() moves files: https://thisinterestsme.com/move-file-php
More specifically, look at your $path and $newpath... would you, say, be missing a part of the path itself? ;) say, the root part ;)
 
could be a couple of things:
1. check to make sure the directories have the proper permissions
2. Might need to use ABSPATH to get the absolute path
Code:
ex: ABSPATH . "/".$title . ".php"
 
so also a few things:
might wanna check if file exists before calling rename
Code:
if (file_exists($old_name) && 
    ((!file_exists($new_name)) || is_writable($new_name))) {
    rename($old_name, $new_name);
}

turn on error logging
Code:
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

 
root folder is drwxr-xr-x (0755)
index.php is -rw-r--r-- (0644)
the generated cat.php is -rw-rw-rw- (0666)


yeah I see the index.php and the cat.php next to to the index.php
XD that's a nice number you got there rw XD I would personally have gone for 777 XD
 
root folder is drwxr-xr-x (0755)
index.php is -rw-r--r-- (0644)
the generated cat.php is -rw-rw-rw- (0666)


yeah I see the index.php and the cat.php next to to the index.php
ok so that means that according to your code, a cat.php file should have been converted to a index.php file, correct?
 
Code:
$path = "/" . $title . ".php";
$newpath = "/" . $title . "/index.php";
rename($path, $newpath);

example:
$title = "cat"
$path = "/cat.php"
$newpath = "/index.php"

result:
cat.php gets renamed to index.php under the same directory.

if you are trying to move to a new directory, you need to specify which
 
Code:
$path = "/" . $title . ".php";
$newpath = "/" . $title . "/index.php";
rename($path, $newpath);

example:
$title = "cat"
$path = "/cat.php"
$newpath = "/index.php"

result:
cat.php gets renamed to index.php under the same directory.

if you are trying to move to a new directory, you need to specify which
So for example:
if you want to move cat.php to /demo and rename to index.php
it would be
Code:
rename('/cat.php', '/demo/index.php');
 

Buy us a coffee!

Back
Top Bottom