PHP将文件夹从ftp上的另一个地方复制到新目的地ø

时间:2017-04-13 23:29:21

标签: php mysql

我已经让这个脚本生成了3个带有随机名称的文件夹,最后,我粘贴了一个完整的文件夹,我在ftp中放了另一个地方。

文件夹真的很棒,但是我无法在结束文件夹中复制粘贴!

你能解释一下如何解决它吗?

<?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>";
 }
 ?>

1 个答案:

答案 0 :(得分:1)

您的shell_exec()命令正在本地计算机上运行。 &#34; cp -r&#34;用于本地复制文件;它不处理远程ftp:// urls。

您需要使用ftp_exec。复制代码如下所示:

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

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