简体   繁体   中英

PHP script to upload all files and folders to FTP server

I have a problem and i cant figure it out. I have a folder in local server that i want to upload it via FTP to a remote server. When i run the script in XAMP-apache in Windows i can upload it with no problems the subfloders and all files, now i try to use the same PHP script for the same folder to the same destination from linux apache but i get "file not found, please try again" Any help will be much appreciated. Thank you!

<?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>'; 
?>

>

You've got a lot of backslashes in there. Are you using backslashes as your directory separator? That won't work on Linux. You either have to use / or DIRECTORY_SEPARATOR .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM