Jump to content

[SOLVED] ftp_mkdir() expects parameter 1 to be resource


fanfavorite

Recommended Posts

I am trying to ftp an entire folder and all it's contents.  Now the problem with this script is the fact that it won't create directories or copy the files to the remote server.  Now I know the connection is established and the $conn_id is ftp.clientsdomain.com, but I get the errors:

 

ftp_mkdir() expects parameter 1 to be resource, null given in ...

ftp_put() expects parameter 1 to be resource, null given in ...

 

Any ideas on why I am getting this error?  Thanks!

 

-JC

 

$connUser = $g[username];
$connPass = $g[Password];
$startpos = $g[startDir]."/clients";
$connServer = "ftp.";
$connServer .= $g[Domain];

$conn_id = ftp_connect($connServer);
$login_result = ftp_login($conn_id, $connUser, $connPass);

function copyDir($dirName,$startpos) {
if(file_exists($dirName)) {
	$dir = dir($dirName);
     		while($file = $dir->read()) {
          		           if($file != '.' && $file != '..') {
             			if(is_dir($dirName.'/'.$file)) {
				ftp_mkdir($conn_id, $startpos.'/'.$file);
        			             copyDir($dirName.'/'.$file,$startpos.'/'.$file);
             			} else {
        			             ftp_put($conn_id, $startpos.'/'.$file, $filetoupload, FTP_BINARY);
             			}
           		           }
     		}
	$dir->close();
} else {
	return false;
   	}
return true;
}

if you want to use the connection resource from within a function, you either have to pass it to the function or you have to globalize it:

 

function copyDir($connection,$dirName,$startpos) {
if(file_exists($dirName)) {
	$dir = dir($dirName);
     		while($file = $dir->read()) {
          		           if($file != '.' && $file != '..') {
             			if(is_dir($dirName.'/'.$file)) {
				ftp_mkdir($connection, $startpos.'/'.$file);
        			             copyDir($dirName.'/'.$file,$startpos.'/'.$file);
             			} else {
        			             ftp_put($connection, $startpos.'/'.$file, $filetoupload, FTP_BINARY);
             			}
           		           }
     		}
	$dir->close();
} else {
	return false;
   	}
return true;
}

 

it's all about scope.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.