n the following program i have the error
Warning: ftp_delete() [function.ftp-delete]: Access is denied. in G:\WEBROOT\wilbourn\ftptest\uploadfunctionpage.php on line 89
could not delete wilbourn/ftptest/t.DOC


<?php 

//Move or Upload Folder Using PHP FTP Functions
function moveFolder($_server, $_user_name, $_user_pass, $local_dir, $remote_dir) {

echo"$_user_name <br/>";
echo"$_user_pass <br/>";
echo" directories are : <br/>";
echo"$local_dir<br/>";
echo"$remote_dir <br/>";



// set up basic connection
$_conn_id = ftp_connect($_server);

// login with username and password
$_login_result = ftp_login($_conn_id, $_user_name, $_user_pass);

// check connection
if ((!$_conn_id) || (!$_login_result)) {
$_error = "FTP connection has failed!";
$_error .= "Attempted to connect to $_server for user $_user_name";
$result = false;
} else {
$_error = "Connected to $_server, for user $_user_name";
}

$conn_id = $_conn_id;

$file = "t.DOC";
echo "Current directory: " . ftp_pwd($conn_id) . "\n";
echo "<br/>";


echo "<br/>";
if (ftp_chdir($conn_id, "$remote_dir")) {

    echo "Current directory is now After changes: " . ftp_pwd($conn_id) . "\n";
	
echo "<br/>";	
echo "<br/>";
echo "<br/>";	
	
	
	// try to delete $file
if (ftp_delete($conn_id, $file)) {
 echo "$file deleted successful\n";
} else {
 echo "could not delete $file\n";
}


}
 else { 
    echo "Couldn't change directory\n";
}
Member Avatar for Rhyan

Check permissions for read/write/delete on the folder on the remote machine using the name / pass.

i have complete permission on all ftp server and all folder over there.
still telling me permission demied
i change the delete code

// try to delete $file
if (ftp_delete($conn_id, $file)) {
 echo "$file deleted successful\n";
} else {
 echo "could not delete $file\n";
}

to

unlink($file);

but still i have problom

Member Avatar for Rhyan

Hmmm.. I have had problem with file creation using php, so I had to change folder permissions to 777 and just then it worked.

I think also that you do not change the ftp_delete command, as it is protocol dependent, not OS dependent. In my opinion directory permissions are the key

yes bro you are right ,that was the problom it is ok now.
i am now going to upolad my folder to ftp, but it give the follwoing errors
successfully created /wilbourn/ftptest
Warning: readdir(): supplied argument is not a valid Directory resource in G:\WEBROOT\wilbourn\ftptest\uploadfunctionpage.php on line 54

Warning: closedir(): supplied argument is not a valid Directory resource in G:\WEBROOT\wilbourn\ftptest\uploadfunctionpage.php on line 65

Notice: Undefined variable: f in G:\WEBROOT\wilbourn\ftptest\uploadfunctionpage.php on line 66

Notice: Undefined variable: result in G:\WEBROOT\wilbourn\ftptest\uploadfunctionpage.php on line 83


the is following

<?php 

//Move or Upload Folder Using PHP FTP Functions
function moveFolder($_server, $_user_name, $_user_pass, $local_dir, $remote_dir) {

echo"$_user_name <br/>";
echo"$_user_pass <br/>";
echo" directories are : <br/>";
echo"$local_dir<br/>";
//echo"$remote_dir <br/>";



// set up basic connection
$_conn_id = ftp_connect($_server);

// login with username and password
$_login_result = ftp_login($_conn_id, $_user_name, $_user_pass);

// check connection
if ((!$_conn_id) || (!$_login_result)) {
$_error = "FTP connection has failed!";
$_error .= "Attempted to connect to $_server for user $_user_name";
$result = false;
} else {
$_error = "Connected to $_server, for user $_user_name";
}

$conn_id = $_conn_id;

//$file = 'vaf3.pdf';

echo "Current directory: " . ftp_pwd($conn_id) . "\n";
echo "<br/>";
	





echo "<br/>";
if (ftp_chdir($conn_id, "$remote_dir")) {

    echo "Current directory is now After changes: " . ftp_pwd($conn_id) . "\n";
	
	echo "<br/>";	echo "<br/>";	echo "<br/>";	
	
//unlink($file); 

@ftp_mkdir($conn_id, $remote_dir);
echo "successfully created $remote_dir\n";

$handle = opendir($local_dir);
while (($file = readdir($handle)) !== false) {
if (($file != '.') && ($file != '..')) {
if (is_dir($local_dir . $file)) {
//recursive call
moveFolder($conn_id, $local_dir . $file . '/', $remote_dir . $file . '/');
echo "successfully uploaded $file\n";
} else
$f[] = $file;
}     

}
closedir($handle);
if (count($f)) {
sort($f);
@ftp_chdir($conn_id, $remote_dir);
foreach ($f as $files) {
$from = @fopen("$local_dir$files", 'r+');
$moveFolder = ftp_fput($conn_id, $files, $from, FTP_ASCII);
// @ftp_fput($conn_id, $files, $from, FTP_BINARY);
// check upload status
if (!$moveFolder) {
$this->_error = "FTP upload has failed! From:" . $local_dir . " To: " . $remote_dir;
$result = false;
} else {
$this->_error = "Uploaded $local_dir to $remote_dir as $this->_server";
$result = true;
}
}
}
return $result;





	
// get the file list for 	
$buff = ftp_rawlist($conn_id, ".");
echo "<br/>";	
var_dump($buff);

}
 else { 
    echo "Couldn't change directory\n";
}


ftp_close($conn_id);

}

?>
Member Avatar for Rhyan

Well, honestly, I do not like while loops, so I would go with scandir() function in php that returns an array of all files. Then I will manipulate the array and upload file by file, rather than complete dir.
Also, I would like to use copy instead of move and once copy is successful, I would go with delete, calling another function.

But from the errors you get, I think the problem is here

$from = @fopen("$local_dir$files", 'r+');

I think this should look like

$from = @fopen("$local_dir/$files", 'r+');

ok i wil try on your idea then see what happened, i wil let you now later.
thanks

now i havw following errors
successfully created wilbourn/ftptest/
Warning: ftp_fput() expects parameter 3 to be resource, boolean given in G:\WEBROOT\wilbourn\ftptest\uploadfunctionpage.php on line 79

Fatal error: Using $this when not in object context in G:\WEBROOT\wilbourn\ftptest\uploadfunctionpage.php on line 83

i have noe error,
but the programe not understand my local directory
c:/ exampledirectory
it gives me the following errors

Warning: opendir(C:\1) [function.opendir]: failed to open dir: No such file or directory in G:\WEBROOT\wilbourn\ftptest\uploadfunctionpage.php on line 59

Warning: readdir(): supplied argument is not a valid Directory resource in G:\WEBROOT\wilbourn\ftptest\uploadfunctionpage.php on line 60
i will show you the form i am using for data entry


<body>
<form method="post" action="upload.php" enctype="multipart/form-data" >

Move or Upload Folder Using PHP FTP Functions<br />
</p>
<table width="373" border="1">
<tr>
<td width="184">server address </td>
<td width="173"><input type="text" id="server_name" name="server_name" /></td>
</tr>
<tr>
<td>user name </td>
<td><input type="text" name="user_name" id="user_name" /></td>
</tr>
<tr>
<td>user password</td>
<td><input type="text" id="user_password" name="user_password" /></td>
</tr>
<tr>
<td>local directory</td>
<td><input type="text" name="local_dir" id="local_dir" /></td>
</tr>
<tr>
<td>remote directory</td>
<td><input type="text" name="remote_dir" id="remote_dir" /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input name="upload" type="submit" id="upload" /></td>
</tr>
</table>
<p>&nbsp; </p>
</form>

</body>


do you think is there any syntax error or logical error.
waiting for reply

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.