Jump to content

[SOLVED] Deleting a file from server through a File List


jonoc33

Recommended Posts

I got a challenge for you PHP people.

I've been having trouble with this for a while. Ages ago I made an upload script that lets you upload to files/upload/ and then once you upload it, a piece of PHP will show the files in that folder.

 

In the admin section of my page i'm trying to find a way to delete the files off the server without having to dig into the FTP and do it manually.

Are you able to do that with PHP?

So basically I want it to say:

 

Uploaded Files:

-file.jpg               [DELETE]

 

The code that displays the files of the folder is this:

 

 

$folder = "../files/upload/"; 
$handle = opendir($folder); 

# Making an array containing the files in the current directory: 
while (false !== ($file = readdir($handle)))
{
    if ($file != '.' && $file != '..')
       $files[] = $file; 
} 
closedir($handle); 

#echo the files 
foreach ($files as $file) { 
    echo "<br />"."-<a href=$folder$file>$file</a>"; 
} 

 

If above is completely impossible, plan B would be to have a box where you could type in the name of the file, hit submit and it would take you to a page where the script deletes it off.

 

Any help would be great.

Jono

Link to comment
Share on other sites

I thought up something..

After it echos the file name it echos this:

echo " -<a href=delete.php?file=$file>Delete</a>";

$file has already been defined as the file name, so that works.

 

So now when it goes to delete.php, i'm not sure what to do. Unlink didn't seem to help me much

 

Link to comment
Share on other sites

Ok (sorry for double posting), i'm using this script for delete.php

 

<?php
$file = '';

// set up basic connection

$conn_id = ftp_connect($ftp_server);
$ftp_server = "localhost";
$ftp_user_name = "jonoc33";
$ftp_user_pass = "******";

// 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);
?> 

 

In $file, how do I get the name of the file from the previous page?

Link to comment
Share on other sites

Doesn't seem to work. It probably doesn't because there is no form or text box on the previous page called file. I've tried everything but nothing works.

 

EDIT: Works now, but it still does not delete it.

It says "Could not delete x.jpg" at least. Still, it won't delete.

Link to comment
Share on other sites

Well, the second parameter of ftp_delete is the path to the file. You appear to be just using the file name?

Thats probably why.. Let me take a look at it

 

EDIT:

$file = '../files/upload/'.$_GET['file'];

 

Echos:

Could not delete ../files/upload/x.jpg

 

*Sigh*

Why doesnt PHP just do what I want it to do!!!

lol

Link to comment
Share on other sites

Works now. I did the actual root of the website and it works fine now.

 

But it says this:

 

mainwebsite_html/squads/alpha/files/upload/x.jpg deleted successfully.

 

How would I make it so it doesn't show mainwebsite_html/squads/alpha/files/upload/ ?

Link to comment
Share on other sites

You can save name of file in different variable and echo that

 

like you did for path

 

<?php
$file = '../files/upload/'.$_GET['file'];
$filename = $_GET['file'];
?>

 

and you can echo the filename, just try it I am not too sure.

OR

you can try explode() and preg_split()

Link to comment
Share on other sites

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.