I need a working code for deleting an uploded file on the ftp server.I do have a script but it dosn't work..

<?php

// set up the settings
$file = 'old.txt';

$ftp_server = someone.net';
$ftpuser = 'user';
$ftppass = 'passwd';

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftpuser, $ftppass);

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

// close the connection
ftp_close($conn_id);

?>

this code is not able to find the old.txt,but this file is placed in the same directory where the script is located.:-/

Recommended Answers

All 4 Replies

Hi,

Surely the FTP will map your public_html folder, within which will be your PHP scripts. So can you not simply use the unlink function in PHP?

Sorry if I am over simplifying the problem.

R

Hi,

Surely the FTP will map your public_html folder, within which will be your PHP scripts. So can you not simply use the unlink function in PHP?

Sorry if I am over simplifying the problem.

R

unlink() function in PHP will be run under the Apache user, however, you can have FTP run under any user as long as you have their authentication details.

ie: If you use unlink(), it will not unlink from FTP session, it will unlink from the PHP process.

this code is not able to find the old.txt,but this file is placed in the same directory where the script is located.

When you log into FTP it will most likely put you into the user directory of the user that authenticates, or keep you in the public ftp directory (may also depend on the ftp application running on the server and settings). You will not be placed into the directory your script is in.

So you will have to log in via FTP, then change the directory to the folder your script is in.

http://www.php.net/manual/en/function.ftp-chdir.php

This works perfectly!

<?php
$file = 'public_html/plentyport.com/jobs/employee/resumes/4216:54:38Resume (1).docx';
$ftp_server="your server address";
// set up basic connection
$conn_id = ftp_connect($ftp_server);
$ftp_user_name="your user server name";
$ftp_user_pass="your server password";
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

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

// close the connection
ftp_close($conn_id);
?>

Cool :). Wow! this thread was posted 6 years ago. This is way older than lastMitch could dig. You are the Man..

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.