how to use unlink to delte a file (with a hyperlink)

  • barry
  • Graduate
  • Graduate
  • User avatar
  • Posts: 115
  • Loc: scotland

Post 3+ Months Ago

Hi all

i am confused..

I want to write a script that will delete a file form a folder. Ok simple with unlink in PHP but i also want to be able to click a link and it will delete the file for me

I have tried to do it calling a function but its not happening

here is what i have so far, this shows the files in the directory as an unordered list

Code: [ Select ]
<ul>
<?php


$dir = '../upload';
$files = scandir($dir);
foreach($files as $ind_file){
?>
<li><a href="../upload/<? echo $ind_file;?>"><?php echo $ind_file;?></li>
<?php
}
?>
</ul>
  1. <ul>
  2. <?php
  3. $dir = '../upload';
  4. $files = scandir($dir);
  5. foreach($files as $ind_file){
  6. ?>
  7. <li><a href="../upload/<? echo $ind_file;?>"><?php echo $ind_file;?></li>
  8. <?php
  9. }
  10. ?>
  11. </ul>



I want to change the link to 2 links, one for downloading the file and a link for deleteing the file too.

Any help would be greatly appreciated
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

You could add another link that looks like this:

Code: [ Select ]
<a href="/delete.php?file=$ind_file">Delete</a>


Then in delete.php

Code: [ Select ]
$dir = '../upload'; 
unlink($dir.'/'.$_GET['file'];
 
  1. $dir = '../upload'; 
  2. unlink($dir.'/'.$_GET['file'];
  3.  
  • barry
  • Graduate
  • Graduate
  • User avatar
  • Posts: 115
  • Loc: scotland

Post 3+ Months Ago

yea it worked after a little tweaking

heres what i have now

Code: [ Select ]
<ul>
<?php


$dir = '../upload';
$files = scandir($dir);
foreach($files as $ind_file){
?>
<li><a href="../upload/<? echo $ind_file;?>"><?php echo $ind_file;?> </a> | <a href="/delete.php?file=<?=$ind_file?>">Delete</a></li>
<?php
}
?>
</ul>
  1. <ul>
  2. <?php
  3. $dir = '../upload';
  4. $files = scandir($dir);
  5. foreach($files as $ind_file){
  6. ?>
  7. <li><a href="../upload/<? echo $ind_file;?>"><?php echo $ind_file;?> </a> | <a href="/delete.php?file=<?=$ind_file?>">Delete</a></li>
  8. <?php
  9. }
  10. ?>
  11. </ul>

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 42 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.