Jannick drev
Jannick drev

Reputation: 37

PHP copy folder from another place on ftp to new destination ø

I have made this script to generate 3 folders with Random names, and in the end, I paste a complete folder I have placed another place inside the ftp.

It really works great with the folders, but I can't make the copy paste in the end folder!

Can you explain how to fix it?

<?php    


                 $tags = mysqli_query($conn, "SELECT domaine FROM `domains` ORDER BY rand() LIMIT 1; ") or die(mysqli_error($conn));
                 while($row = mysqli_fetch_array($tags)) 
                             {            
                                 $domainresul =  $row['domaine'];
                             } 

                                function random_string($length) {
                                    $key = '';
                                    $keys = array_merge(range(0, 9), range('a', 'z'));
                                    for ($i = 0; $i < $length; $i++) {
                                    $key .= $keys[array_rand($keys)];
                                    }

                                    return $key;
                                }


                                $file = "index.php";
                                $content = $sideindhold;


                                if (isset($_POST['submit'])) {


                                $ftp_server = "xxx"; // virtuelt doamin
                                $conn_id = ftp_connect($ftp_server);
                                $ftp_user_name = "xxx"; // bruger jeg har opsat på min xampp, med rettigheder til example.com
                                $ftp_user_pass = "xxx";
                                $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

                                // $root="url/phptest";
                                $root = $domainresul;




                                /* **************************************** */
                                /* create a stream context telling PHP to overwrite the file */
                                $options = array('ftp' => array('overwrite' => true));
                                $stream = stream_context_create($options);
                                /* **************************************** */


                                // check connection
                                echo "<center>";
                                if ((!$conn_id) || (!$login_result)) {
                                echo '<div style="background-color:red;padding:10px;color:#fff;font-size:16px;">';
                                echo "FTP connection has failed!";
                                echo "Attempted to connect to <b>$ftp_server</b> for user <b>$ftp_user_name</b>";
                                echo '</div>';
                                } else {

                                $foldername1 = random_string(4);
                                $foldername2 = random_string(3);
                                $foldername3 = random_string(2);



                                $directory = "$root/$foldername1";

                                if (ftp_mkdir($conn_id, $directory)) {

                                $directory = "$root/$foldername1/$foldername2";

                                if (ftp_mkdir($conn_id, $directory)) {

                                $directory = "$root/$foldername1/$foldername2/$foldername3";


                                if (ftp_mkdir($conn_id, $directory)) {
                                /* **************************************** */
                                /* and finally, put the contents */
                                $hostname2 = "ftp://" . $ftp_user_name . ":" . $ftp_user_pass . "@" . $ftp_server . "/";
                     $hostname = "ftp://" . $ftp_user_name . ":" . $ftp_user_pass . "@" . $ftp_server . "/" . $directory . "/";


                       $src = $hostname2 . "xxx.xxx/se";
                       $dst = $hostname;

                       echo $src;
                       echo $dst;

                       shell_exec("cp -r $src $dest");

                     /* **************************************** */

                 }
             }
         } else {
             echo '<div style="background-color:red;padding:10px;color:#fff;font-size:16px">';
             echo "Could not create directory: <b>$directory</b>";
             echo '</div>';
         }
     }
     echo "</center>";
 }
 ?>

Upvotes: 0

Views: 224

Answers (1)

David Jeske
David Jeske

Reputation: 2466

You're shell_exec() command is running on the local machine. "cp -r" is for copying files locally; it does not handle remote ftp:// urls.

You need to use ftp_exec. The copy code would look something like:

$src = "xxx.xxx/se";
$dst = $directory . "/" . $src;

ftp_exec("cp -r $src $dst");

Upvotes: 1

Related Questions