I am making a content management application for a client to update his web site without having to use a ftp client.
On one page, the client can upload images and also delete images already stored on the server.
The script I have fills an list box with the files (images) currently stored on the server in the directory and then allows him to select and delete them. The script I use to connect to the server needs password and username to connect to it.
I have the 'update' file password protected using .htpasswd and .htaccess
Code below in red is where the domain, username and password is entered in code
Is there a better way to do this?
<?php
$conn = ftp_connect("[COLOR="Red"]ftp.mydomain.com") [/COLOR]or die("Could not connect");
ftp_login($conn,"[COLOR="red"]myusername","mypassword[/COLOR]"); //USERNAME AND PASSWORD TYPED HERE
$content=(ftp_nlist($conn,"public_html/testing"));
ftp_close($conn);
?>
<font color='red'>Array filled selectbox: </font>
<br>
<form method="POST" action="<?php echo $PHP_SELF;?>">
<?php
selectboxArrayFill($content, "Arraybox"); //Test the Array fill
function selectboxArrayFill(&$array, $selboxname){
echo '<SELECT size="8" name="'. $selboxname.'" >';
reset($array);
while(list($key, $value)= each($array)){
if(trim($value)!=="." && trim($value)!=="..")
{
echo "\n <OPTION value='".$value."'>".$value."</OPTION>";
}
}
echo "\n</SELECT>\n";
}
?>
<input type="submit" name="delete" value="Select files and delete">
</form>
<br><br>
<?php
if (isset($_POST['delete']))
{
$fileToDelete= $_POST['Arraybox'];
// set up the settings
$file = 'public_html/testing/';
$file = $file . $fileToDelete;
$ftp_server = [COLOR="red"]'mydomainname.com'[/COLOR];
$ftpuser = [COLOR="red"]'myusername[/COLOR]';
$ftppass = '[COLOR="red"]mypassword'[/COLOR];
// 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 successfully\n";
} else {
echo "could not delete $file\n";
}
// close the connection
ftp_close($conn_id);
}
?>