2017-04-15 46 views
1

我尝试复制放置在我ftp上的文件夹! 但我不会工作..Php复制文件夹到ftp上的另一个地方

首先我打包3个随机名称的文件夹,它运作良好。 但是,当我试图使复制和粘贴的文件夹包括里面的文件,到我的新destanation没有任何事情!

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

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

您的代码中没有任何错误可以检测到。问题在于文件夹的权限。我有同样的问题,但经过一些研究后,我能够解决问题。 –

+0

我想我需要在$ src和$ dst中运行$ conn_id?莫比? –

+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([email protected]('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

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); 
?> 
相关问题