Jump to content

Recommended Posts

<?php
Class FTPClient
{
 private $connectionId;
 private $loginOk = false;
 private $messageArray = array();

 public function __construct() { }
 private function logMessage($message, $clear=true)
 {
  if ($clear) {$this->messageArray = array();}
  $this->messageArray[] = $message;
 }
 public function getMessages()
 {
  return $this->messageArray;
 }

 public function connect ($server, $ftpUser, $ftpPassword, $isPassive = false)
 {
  // *** Set up basic connection
  $this->connectionId = ftp_connect($server);
  // *** Login with username and password
  $loginResult = ftp_login($this->connectionId, $ftpUser, $ftpPassword);
  // *** Sets passive mode on/off (default off)
  ftp_pasv($this->connectionId, $isPassive);
  // *** Check connection
  if ((!$this->connectionId) || (!$loginResult)) {
   $this->logMessage('FTP connection has failed!');
   $this->logMessage('Attempted to connect to ' . $server . ' for user ' . $ftpUser, true);
   return false;
  } else {
   $this->logMessage('Connected to ' . $server . ', for user ' . $ftpUser);
   $this->loginOk = true;
   return true;
  }
 }
 public function makeDir($directory)
 {
  // *** If creating a directory is successful...
  if (@ftp_mkdir($this->connectionId, $directory)) {
   $this->logMessage('Directory "' . $directory . '" created successfully');
   return true;
  } else {
   // *** ...Else, FAIL.
   $this->logMessage('Failed creating directory "' . $directory . '"');
   return false;
  }
 }
 public function uploadFile ($fileFrom, $fileTo)
 {
  // *** Set the transfer mode
  $asciiArray = array('txt', 'csv');
  $extension = end(explode('.', $fileFrom));
  if (in_array($extension, $asciiArray)) {
   $mode = FTP_ASCII; 
  } else {
   $mode = FTP_BINARY;
  }
  // *** Upload the file
  $upload = ftp_put($this->connectionId, $fileTo, $fileFrom, $mode);
  // *** Check upload status
  if (!$upload) {
 $this->logMessage('FTP upload has failed!');
 return false;
   } else {
 $this->logMessage('Uploaded "' . $fileFrom . '" as "' . $fileTo);
 return true;
   }
 }

 public function ftp_putAll ($src_dir, $dst_dir)
 {

  $d = dir($src_dir);

  while($file = $d->read()) { // do this for each file in the directory

	 if ($file != "." && $file != "..") { // to prevent an infinite loop

	    if (is_dir($src_dir."/".$file)) { // do the following if it is a directory
		    if (!@ftp_chdir($this->connectionId, $dst_dir."/".$file)) {
			    ftp_mkdir($this->connectionId, $dst_dir."/".$file); // create directories that do not yet exist
		    }
			 $this->ftp_putAll($this->connectionId, $src_dir."/".$file, $dst_dir."/".$file); // recursive part
	    } else {
		    $upload = ftp_put($this->connectionId, $dst_dir."/".$file, $src_dir."/".$file, FTP_BINARY); // put the files
	    }

    }
   }

   $d->close();

 }
 public function changeDir($directory)
 {
  if (ftp_chdir($this->connectionId, $directory)) {
   $this->logMessage('Current directory is now: ' . ftp_pwd($this->connectionId));
   return true;
  } else {
   $this->logMessage('Couldn\'t change directory');
   return false;
  }
 }
 public function getDirListing($directory = '.', $parameters = '-la')
 {
  // get contents of the current directory
  $contentsArray = ftp_nlist($this->connectionId, $parameters . '  ' . $directory);
  return $contentsArray;
 }
 public function downloadFile ($fileFrom, $fileTo)
 {
  // *** Set the transfer mode
  $asciiArray = array('txt', 'csv');
  $extension = end(explode('.', $fileFrom));
  if (in_array($extension, $asciiArray)) {
   $mode = FTP_ASCII; 
  } else {
   $mode = FTP_BINARY;
  }
  // open some file to write to
  //$handle = fopen($fileTo, 'w');
  // try to download $remote_file and save it to $handle
  if (ftp_get($this->connectionId, $fileTo, $fileFrom, $mode, 0)) {
   return true;
   $this->logMessage(' file "' . $fileTo . '" successfully downloaded');
  } else {
   return false;
   $this->logMessage('There was an error downloading file "' . $fileFrom . '" to "' . $fileTo . '"');
  }
 }

 public function __deconstruct()
 {
  if ($this->connectionId) {
   ftp_close($this->connectionId);
  }
 }

}

       // *** Define your host, username, and password
       define('FTP_HOST', 'ftp.xxxx.co.uk');
       define('FTP_USER', 'xxxx.co.uk');
       define('FTP_PASS', 'xxxx');
       define('INSTALLPATH', '/public_html/');

       // *** Include the class
       include('ftp_class.php');

       // *** Create the FTP object
       $ftpObj = new FTPClient();

       // *** Connect
       if ($ftpObj -> connect(FTP_HOST, FTP_USER, FTP_PASS)) {

           $dir = INSTALLPATH . 'newshub';  
           // *** Make directory  
           $ftpObj->makeDir($dir);

           print_r($ftpObj -> getMessages());

           $ftpObj->ftp_putAll('/Applications/XAMPP/xamppfiles/htdocs/newshub', '/public_html/newshub');

       }else{

       print_r($ftpObj -> getMessages());

       }

?>

 

Array ( [0] => Failed creating directory "/public_html/newshub" )

Warning: dir() expects parameter 1 to be string, resource given in /Applications/XAMPP/xamppfiles/htdocs/ftp_class.php on line 93

 

Fatal error: Call to a member function read() on a non-object in /Applications/XAMPP/xamppfiles/htdocs/ftp_class.php on line 95

Link to comment
https://forums.phpfreaks.com/topic/270452-errors-are-driving-me-crazy/
Share on other sites

You are calling ftp_putAll near the bottom of your script, which is fine. Then inside that function itself, you are making a recursive call where you are passing $this->connectionID as the first parameter. This parameter is then used in PHP's dir() function, which expects the parameter to be a string. However, $this->connectionID is a resource set by ftp_connect(). It returns the following:

 

Returns a FTP stream on success or FALSE on error.

 

In your case it returns a stream, which you are then trying to use with dir(), which expects a string, not a resource.

 

Your warning tells you everything you need to know to find the problem.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.