【问题标题】:PHP script to upload all files and folders to FTP server将所有文件和文件夹上传到 FTP 服务器的 PHP 脚本
【发布时间】:2016-01-12 00:46:50
【问题描述】:

我有一个问题,我想不通。我在本地服务器中有一个文件夹,我想通过 FTP 将其上传到远程服务器。当我在 Windows 的 XAMP-apache 中运行脚本时,我可以毫无问题地上传 subfloders 和所有文件,现在我尝试使用相同的 PHP 脚本将相同的文件夹从 linux apache 发送到相同的目的地,但我得到“文件不是找到了,请重试” 任何帮助都感激不尽。谢谢!

<?php 
ob_start(); 
set_time_limit(0); 

$sourcedir="source_folder"; //this is the folder that you want to upload with all subfolder and files of it.

$ftpserver="192.168.1.150"; //ftp domain name
$ftpusername="user";  //ftp user name 
$ftppass="user"; //ftp passowrd
$ftpremotedir="destination_folder"; //ftp main folder


$ftpconnect = ftp_connect($ftpserver); 
$ftplogin = ftp_login($ftpconnect, $ftpusername, $ftppass); 

if((!$ftpconnect) || (!$ftplogin))  
{ 
  echo "cant connect!"; 
  die(); 
} 


function direction($dirname) 
{ 
  global $from,$fulldest,$ftpremotedir,$ftpconnect,$ftpremotedir; 

  chdir($dirname."\\"); 
  $directory = opendir("."); 

  while($information=readdir($directory))  
  { 
    if ($information!='.' and $information!='..' and $information!="Thumbs.db") 
    {  
        $readinfo="$dirname\\$information"; 

        $localfil=str_replace("".$from."\\","",$dirname).""; 
        $localfold="$localfil\\$information"; 
        $ftpreplacer=str_replace(array("\\\\","\\"),array("/","/"),"$ftpremotedir\\".str_replace("".$fulldest."","",$dirname)."\\$information"); 

        if(!is_dir($information)) 
        { 
          $loading = ftp_put($ftpconnect, $ftpreplacer, $readinfo, FTP_BINARY); 

          if (!$loading)  
          { 
              echo "<font color=red>Files not found... Please try again...</font>"; echo "<br>";  fls(); 
          }  
          else  
          { 
              echo "<font color=green> Please wait... Uploading files</font>"; echo "<br>"; fls(); 
          } 
        } 
        else 
        {  
          ftp_mkdir($ftpconnect, $ftpreplacer); 
          direction("$dirname\\$information"); 
          chdir($dirname."\\"); 
          fls(); 
        } 
    } 
  } 
  closedir ($directory); 
} 

function fls() 
{ 
    ob_end_flush(); 
    ob_flush(); 
    flush(); 
    ob_start(); 
} 

$from=getcwd(); 
$fulldest=$from."\\$sourcedir"; 
direction($fulldest); 
ftp_close($ftpconnect); 
echo '<font color=red>Your folder is now ready for use <font color=red>'; 
?>

>

【问题讨论】:

  • 我注意到您正在使用 \ 和 chdir,但这不是 Unix 系统上的目录分隔符。 chdir 最后不需要 \ 或 /,所以只需将其省略即可。此外,ftp_put 可能会以无法找到文件以外的方式失败,因此错误消息可能是错误的。添加错误处理程序以获取 ftp 错误/警告消息。

标签: php linux apache ftp


【解决方案1】:

那里有很多反斜杠。您是否使用反斜杠作为目录分隔符?这在 Linux 上是行不通的。您必须使用/DIRECTORY_SEPARATOR

【讨论】:

  • 它也可以在 Windows 上运行,因为 PHP 在 Windows 系统上运行时会自动执行从 / 到 `\` 的任何必要转换
  • 我刚刚用 '/' 替换了所有的 '\',现在它工作得很好:)。我是php新手,谢谢大家的回答!
  • 别忘了接受答案,然后查看stackoverflow.com/help/someone-answers
猜你喜欢
  • 1970-01-01
  • 2014-03-22
  • 2018-09-28
  • 1970-01-01
  • 1970-01-01
  • 2016-10-09
  • 1970-01-01
  • 1970-01-01
  • 2010-12-26
相关资源
最近更新 更多