Wednesday, February 8, 2023

move files from one folder to another by php code if you have no access to cpanel

 Hello.

TOday I will explain how to move files one folder to another folder in server with php code. Recently I face problem to move files from my computer to server. SO i make zip file ad upload it to server and i unzip it. But it creates a folder and put all files into that folder. My cpanel has not enough rights to move all files. IT has right only for one by one file move. It is very tedious job. SO I make this php code for move files.


<?php

//File Manager - Home Folder: dede.cptost-h.net/public_html/wp-includes/files_zip_0/

//File Manager - Home Folder: dede.cptost.h.net/public_html/wp-admin/wpadmin_zip_0/

$dir = "wp-admin/wpadmin_zip_0";

$new_location = "wp-admin/";

$i = 1;

if ($handle = opendir($dir)) {

    while (false !== ($entry = readdir($handle))) {


        if ($entry != "." && $entry != "..") {

            $file = $dir . "/" . $entry;

            $new_file = $new_location . $entry;


            if (!rename($file, $new_file)) {

                echo "<br>$i Error: Unable to move the file: $entry.";

            } else {

                echo "<br>$i File $entry moved successfully.";

            }

        }

        $i++;

    }


    closedir($handle);

} else {

    echo "Error: Unable to open the directory. <br>";

}


?>

No comments:

Post a Comment