Page 1 of 1

problem in deleting files from server once uploaded

Posted: Mon Sep 21, 2009 1:45 pm
by ejazshah
Hi All,
I have setup an apache server on XP with simple functionalities like upload, download and delete. I am done with upload and download part. I dont know how to delete files. I know about unlink. But I have failed to implement it in my situation. In my situation, all files are displayed in directory called uploads, the path is as under:
http://localhost:8085/test/Upload/uploaddocs/ (when this is entered in browser you can view all files uploaded).
If I need to delete any selected file, How to go about it? I have spent 3 days working on it. I know about unlink but I have failed to figure it out how to use it in my situation.KIndly help me.

Ejaz

Re: problem in deleting files from server once uploaded

Posted: Mon Sep 21, 2009 3:00 pm
by jackpf

Code: Select all

unlink('name_of_file');
How did you fail at that? :P

Re: problem in deleting files from server once uploaded

Posted: Mon Sep 21, 2009 3:02 pm
by califdon
Most likely either a path or permissions issue.

Re: problem in deleting files from server once uploaded

Posted: Mon Sep 21, 2009 3:06 pm
by jackpf
Yeah. Post your code, explain what is/isn't happening, and make sure error reporting is turned on, and post any errors :)

I guess I should have said that in my first post...

Re: problem in deleting files from server once uploaded

Posted: Mon Sep 21, 2009 3:35 pm
by ejazshah
I was sure you ppl will get it wrong. The unlink is working fine but my issue is as under:
the code is exactly the same as you have mentioned. But there are 2 problems:
1) I want the file name to be selected by the user accessing my server. So i cant specify the file name. It should be passed as a paramter on click or if there is any other suitable way (and I dont know how to do that).
2) This piece of code is working fine but only for the file name specified. there are many files listed on the server. If some user want to delete one file he wont be having access to the unlink code to specify the filename. I want it to work as if as a user you click on a file to delete, it pops up a message asking Are you sure you want to delete file? and on pressing ok it should remove it from the server

Need your help..

Re: problem in deleting files from server once uploaded

Posted: Mon Sep 21, 2009 3:57 pm
by jackpf
Pass the name of the file in the query string, like page.php?nameoffile=somefile.txt, then use

Code: Select all

$_GET['nameoffile']
to access it.

Make sure you secure it though ;) This is an easy subject to get hacked on.

Re: problem in deleting files from server once uploaded

Posted: Mon Sep 21, 2009 4:20 pm
by ejazshah
My upload.html file is:
<html>
<body>
<form enctype="multipart/form-data" action="upload_file.php" method="POST">
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
<a href="uploaddocs/"><h1>Documents</h1></a>
</form>
</body>
</html>
My upload_file.php is:
<?php
$target_path = "uploaddocs/";
/* Add the original filename to our target path. Result is "uploads/filename.extension" */
$target_path = $target_path.basename($_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'],$target_path)){
echo "The file ".basename($_FILES['uploadedfile']['name'])." has been uploaded";
}
else{
echo "There was an error uploading the file, please try again!";
}
?>
My delete.php file is:
<?php
if(unlink($filename)){
print "Deleted $filename!\n";
}
else{
print "Deletion of $filename failed!\n";
}
?>
Now where to place the GET and the href thing?
The file once loaded automatically generates href for the address http://localhost/test/Upload/uploaddocs...
How can I change it? I hope you understand my problem :cry:

Re: problem in deleting files from server once uploaded

Posted: Mon Sep 21, 2009 4:23 pm
by jackpf
*cough* use [ php] tags *cough*.
 
Umm...well, wherever you want the link for someone to delete the file, put

Code: Select all

echo '<a href="delete.php?file='.$the_filename.'">Delete</a>';
then in delete.php

Code: Select all

if(isset($_GET['file']) && file_exists('uploads/'.basename($_GET['file'])))
    unlink('uploads/'.basename($_GET['file']));
I'm not really sure how to put it any simpler. If you don't understand, go and read a tutorial ;)

Re: problem in deleting files from server once uploaded

Posted: Tue Sep 22, 2009 11:47 am
by ejazshah
One last thing that I need to ask is that where to place this line:
echo '<a href="delete.php?file='.$the_filename.'">Delete</a>';
My files are3 displayed in a directory. The path where all files are displayed after upload is:
http://localhost:8085/test/Upload/uploaddocs/
The html generated for this is automatic which is as under:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<title>Index of /test/Upload/uploaddocs</title>
</head>
<body>
<h1>Index of /test/Upload/uploaddocs</h1>
<ul><li><a href="/test/Upload/"> Parent Directory</a></li>
<li><a href="Installation%20Guide%20for%20Apache%202.2.13.docx"> Installation Guide for Apache 2.2.13.docx</a></li>
<li><a href="Installation_Guide_for_Apache_2.2.13.pdf"> Installation_Guide_for_Apache_2.2.13.pdf</a></li>
<li><a href="MAG%20Installation,%20Reference,%20and%20Miscellaneous%20Linux%20Commands(build).doc"> MAG Installation, Reference, and Miscellaneous Linux Commands(build).doc</a></li>
<li><a href="MAG%20Team%20Code%20Review%20Process.doc"> MAG Team Code Review Process.doc</a></li>
<li><a href="Olympic%20memo%20AEP.pdf"> Olympic memo AEP.pdf</a></li>
<li><a href="del.php"> del.php</a></li>
<li><a href="delete.php"> delete.php</a></li>
<li><a href="delete1.php"> delete1.php</a></li>
<li><a href="delete2.php"> delete2.php</a></li>
<li><a href="delete3.php"> delete3.php</a></li>
<li><a href="flowplayer-3.1.3/"> flowplayer-3.1.3/</a></li>
<li><a href="pic.jpg"> pic.jpg</a></li>
<li><a href="sample.xlsx"> sample.xlsx</a></li>
</ul>
</body></html>
How can I input this line here? I cant get help from tutorials on this. Since I know the problem but I cant find any implementation to this ? It is an automatic process, once file is uplocaded it shows up in the directory the html code of which cannot be modified. How can I insert the line "echo '<a href="delete.php?file='.$the_filename.'">Delete</a>'; "
in it so that it automatically works for any file uploaded?

Kindly help me. I have been stuck on this for last 4-5 days now.

Re: problem in deleting files from server once uploaded

Posted: Tue Sep 22, 2009 11:59 am
by jackpf
I can't tell you where you want your delete link :/

Only you know your own mind.

Re: problem in deleting files from server once uploaded

Posted: Tue Sep 22, 2009 2:45 pm
by ejazshah
Yeah I know where to place it. But its just that the html doc for directory file listing place is automatically generated due to apache configuration settings for that. I dont know if there is anything that can enable delete on that page? Do you have any idea about that?

Re: problem in deleting files from server once uploaded

Posted: Tue Sep 22, 2009 3:35 pm
by jackpf
I don't understand...how is it generated?

Is there not a PHP script that creates the HTML for the file browser page? If so, just place it somewhere in there.

Re: problem in deleting files from server once uploaded

Posted: Tue Sep 22, 2009 5:05 pm
by ejazshah
Yes. The html is automatically generated with href to each file uploaded(the code i posted in previous mail). The only thing I worked on was the uplaod and download scripts. Even download script is not needed. Any file that is uploaded is placed in the directory and when you go to the following directory it shows up as a href. When you click on it, it automatically downloads. So I dont need to use download script for that. I think it has some thing to do with the php 5 and apache 2.2.13 configuration settings. Download functionality is in build in it. I got it very clearly how to use unlink but how to implement it in this situation I need help :( ....hope u undersatnds the problem..

Re: problem in deleting files from server once uploaded

Posted: Tue Sep 22, 2009 5:15 pm
by jackpf
I've shown you how to use unlink() above.

I still don't understand what your problem is...

Ok, so the page is generated with a download link. Display a delete link next to it. What's the problem??

Re: problem in deleting files from server once uploaded

Posted: Wed Sep 23, 2009 1:26 pm
by ejazshah
hey I am done finally. It was so simple :) ....

Thanks for the help, without that, it wouldnt have been possible.
I just need to write a php script to retrieve all files and then add that unlink to it.
It working fine now..

Thanks