Php 从FTP压缩文件夹

Php 从FTP压缩文件夹,php,Php,如果您在Linux上,并且不受php安全模式的限制(请使用phpinfo()查找),那么您可以简单地使用exec()从shell运行zip命令,如: <? $ftp_server = "name.info"; $ftp_user = "user"; $ftp_pass = "pass"; // set up a connection or die $conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_s

如果您在Linux上,并且不受php安全模式的限制(请使用phpinfo()查找),那么您可以简单地使用exec()从shell运行zip命令,如:

<?

$ftp_server = "name.info";
$ftp_user = "user";
$ftp_pass = "pass";

// set up a connection or die
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server"); 

// try to login
if (@ftp_login($conn_id, $ftp_user, $ftp_pass)) 
{
    ftp_chdir($conn_id, 'public_html/');
    $contents = ftp_nlist($conn_id, '-la .');
    print_r($contents);
    foreach($contents as $line){
      $file = preg_split('@\s+@', trim($line));
      $name = $file[8];
      $size = $file[4];
      $mode = $file[0];
      if(substr($mode, 0, 1) == '-'){
        //file
        $fd = fopen('d/'.$name, 'w');
        ftp_fget ($conn_id, $fd, $name, FTP_BINARY);
        fclose($fd);
      }else{
        //dir
      }
    }
}
else
    die ("Couldn't connect as $ftp_user\n");

?>

希望这有帮助


编辑:只是为了澄清一下,如果您有FTP访问权限,那么您可以将此语句作为独立脚本上传,并通过浏览器调用。

如果您在Linux上,并且不受php安全模式的限制(使用phpinfo()查找),那么您可以简单地使用exec()从shell运行zip命令,如:

<?

$ftp_server = "name.info";
$ftp_user = "user";
$ftp_pass = "pass";

// set up a connection or die
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server"); 

// try to login
if (@ftp_login($conn_id, $ftp_user, $ftp_pass)) 
{
    ftp_chdir($conn_id, 'public_html/');
    $contents = ftp_nlist($conn_id, '-la .');
    print_r($contents);
    foreach($contents as $line){
      $file = preg_split('@\s+@', trim($line));
      $name = $file[8];
      $size = $file[4];
      $mode = $file[0];
      if(substr($mode, 0, 1) == '-'){
        //file
        $fd = fopen('d/'.$name, 'w');
        ftp_fget ($conn_id, $fd, $name, FTP_BINARY);
        fclose($fd);
      }else{
        //dir
      }
    }
}
else
    die ("Couldn't connect as $ftp_user\n");

?>

希望这有帮助


编辑:只是澄清一下,如果你有FTP访问权限,那么你可以将此语句作为独立脚本上传,并通过浏览器调用。

这是我在慢速FTP服务器上使用的代码:它创建一个zip文件,然后我需要5分钟将其复制到本地计算机(取决于internet连接速度和压缩文件大小)



希望这有帮助

这是我在慢速FTP服务器上使用的代码:它创建一个zip文件,然后花5分钟将其复制到本地计算机(取决于internet连接速度和压缩文件大小)



希望这有帮助。

@thetaiko:它不会将文件复制到我的目录。@hey…对。具体来说,什么不起作用。连接?登录?fget?这里有很多函数在成功或失败时返回布尔值。如果我的解决方案有帮助,请接受它作为答案。如果没有,请评论并让我知道原因。谢谢@泰科:它不会把文件复制到我的目录。@hey…对。具体来说,什么不起作用。连接?登录?fget?这里有很多函数在成功或失败时返回布尔值。如果我的解决方案有帮助,请接受它作为答案。如果没有,请评论并让我知道原因。谢谢
<?php 

$archive = "2012.zip";
$directory = $_SERVER['DOCUMENT_ROOT'];
exec( "zip -r $archive $directory");

 ?>