Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. 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.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" 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 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
 
Solution
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
The folder doesn't already exist, I was thinking it would create the folder automatically if it can't find it. lol

Thanks, it's working now.
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom