将所有文件和文件夹上传到 FTP 服务器的 PHP 脚本

标签 php linux apache ftp

我有一个问题,我无法解决。我在本地服务器上有一个文件夹,我想通过 FTP 将其上传到远程服务器。当我在 Windows 的 XAMP-apache 中运行脚本时,我可以毫无问题地上传子文件和所有文件,现在我尝试对同一文件夹使用相同的 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>'; 
?>

>

最佳答案

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

关于将所有文件和文件夹上传到 FTP 服务器的 PHP 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34733789/

相关文章:

Python 模块权限

php - UTF-8贯穿始终

unix - nginx 错误连接到 php5-fpm.sock 失败 (13 : Permission denied)

php - 选择另一列中的数据在最小值的记录

linux - 尾部反转/打印除最后 n 行之外的所有内容?

linux - 我可以在没有硬件生成器的情况下使用 rng-daemon 来增加 linux 熵吗?

apache - http Pragma的有效值是多少

apache - .htaccess 提示输入密码

php - 如何在具有透明背景的网站顶部创建可关闭的 div?

php - 通知接口(interface)方法是通过__call实现的?