php file upload: use of unlink in a list to delete an element from the server -
i have php upload file, want add button @ right of each element of list if click on delete element list , server. possible? thanks
<html> <body> <form action="" method="post" enctype="multipart/form-data"> select file upload: <input type="file" name="filetoupload" id="filetoupload"> <input type="submit" value="load" name="submit"> </form> <div> <?php $dir = 'up/'; $files = scandir($dir); echo 'uploaded files<br><hr>'; $i = 1; foreach ($files $key) { if ($i>2) { $j = $i-2; echo $j."  <a href='up/".$key."'>".$key."</a><hr>"; } $i++; } echo ' '; ?> </div> </body> </html> <?php if (isset($_post['submit'])) { echo '<center><h1>upload succesful!</h1></center>'; $structure = 'up/'; $target_file = $structure.basename($_files["filetoupload"]["name"]); move_uploaded_file($_files["filetoupload"]["tmp_name"], $target_file); } ?>
you can unlink files need it.
here easy steps ;)
step 1 : create anchor tag gives path of image/file
<a href='index.php?delete=up/<?php echo $key?>'>delete now</a>
step 2 : on clicking anchor tag, page redirect file's name
step 3 : file , unlink (delete) it
unset($_get['delete']);
step 4 : refresh page once have fresh list of files.
header("refresh:0; url=".$url);
here's eval , here's code need have
<html> <body> <form action="" method="post" enctype="multipart/form-data"> select file upload: <input type="file" name="filetoupload" id="filetoupload"> <input type="submit" value="load" name="submit"> </form> <div> <?php error_reporting(0); session_start(); $dir = 'up/'; $files = scandir($dir); echo 'uploaded files<br><hr>'; $i = 1; foreach ($files $key) { if ($i>2) { $j = $i-2; echo $j."  <a href='up/".$key."'>".$key."</a>"; if (isset($_get['delete'])) { unlink($_get['delete']); $_session['delete'] = $_get['delete']; unset($_get['delete']); $url = $_server['script_name'].http_build_query($_get); header("refresh:0; url=".$url); } ?> <a href='index.php?delete=up/<?php echo $key?>'>delete now</a> <hr> <?php } $i++; } ?> </div> </body> </html> <?php if (isset($_post['submit'])) { echo '<center><h1>upload succesful!</h1></center>'; $structure = 'up/'; $target_file = $structure.basename($_files["filetoupload"]["name"]); move_uploaded_file($_files["filetoupload"]["tmp_name"], $target_file); } ?>
Comments
Post a Comment