Php复制文件夹到ftp

时间:2017-04-15 23:07:30

标签: php

我尝试制作一个放在我的ftp上的文件夹的副本!! 但我不会工作..

首先我用随机名称创建3个文件夹,但效果很好。 但是,当我尝试复制和粘贴文件夹包含文件里面,我的新命运没有任何发生!

警告:copy():copy()函数的第一个参数不能是目录

<?php include 'header.php'; 

if  ($_SESSION['user']['useres_types'] == '1' or '3'){

    $pid = $_SESSION['user']['id'];
         $sideindhold = $_POST['sidecentent'];

 ?>
<h1>Spinner</h1>            
<?php   
$tags = mysqli_query($conn, "SELECT domaene FROM `2` ORDER BY rand() LIMIT 1; ") or die(mysqli_error($conn));
            while($row = mysqli_fetch_array($tags)) 
                        {           
                            $domainresul =  $row['domaene'];
                        } 

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


$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 = "xxx.xxx";

echo $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.xyz/se";
$dst = $directory;
echo $src;
 echo $dst;

 copy($src, $dst);
                    /* **************************************** */

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

2 个答案:

答案 0 :(得分:0)

如何解决权限问题:

首先,通过手动允许用户x执行所有操作。这不能解决新文件夹的问题。

使用代码创建第二个权限:

对于文件

ftp_chmod ( resource $ftp_stream , int $mode , string $filename )

对于文件夹

chmod ( string $filename , int $mode )

文件示例:

<?php

    $file = 'public_html/index.php';

     // set up basic connection
        $conn_id = ftp_connect($ftp_server);

        // login with username and password
        $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

        // try to chmod $file to 644
        if (ftp_chmod($conn_id, 0644, $file) !== false) {
         echo "$file chmoded successfully to 644\n";
        } else {
         echo "could not chmod $file\n";
        }

    // close the connection
    ftp_close($conn_id);
    ?>

文件夹示例:

<?php
// Read and write for owner, nothing for everybody else
chmod("/somedir/somefile", 0600);

// Read and write for owner, read for everybody else
chmod("/somedir/somefile", 0644);

// Everything for owner, read and execute for others
chmod("/somedir/somefile", 0755);

// Everything for owner, read and execute for owner's group
chmod("/somedir/somefile", 0750);
?>

对于错误,请使用此示例:

<?php
if(!@copy('http://someserver.com/somefile.zip','./somefile.zip'))
{
    $errors= error_get_last();
    echo "COPY ERROR: ".$errors['type'];
    echo "<br />\n".$errors['message'];
} else {
    echo "File copied from remote!";
}
?>

有关更多信息,请访问此网站http://php.net/manual/en

答案 1 :(得分:0)

我找到了一个帮助我的解决方案,现在它可以工作了!祝你有个美好的一天。

 <?php


  function recurse_copy($src,$dst) {
  $dir = opendir($src);
  @mkdir($dst);
  while(false !== ( $file = readdir($dir)) ) {
  if (( $file != '.' ) && ( $file != '..' )) {
  if ( is_dir($src . '/' . $file) ) {
  recurse_copy($src . '/' . $file,$dst . '/' . $file);
  }
  else {
  copy($src . '/' . $file,$dst . '/' . $file);
  }
}
   } 
    closedir($dir);
 }


$src = 'src';
$dst = 'dst';
recurse_copy($src,$dst);
?>