delete file using PHP while staying on same page
Posted: Wed Nov 30, 2011 11:56 pm
Hello
I've created a form where I can upload file attachments using PHP. I also want to give users option to delete these attachments by clicking on a button next to each file attachment.
This is how I do it in the form:
// display link to file
echo "<a href='file://" . $file_url . "'>" . $file . "</a>";
// display delete button
echo "<a href='delete_file.php?file=" . addslashes($file_url) . "'><img src='images/remove.jpg' style='position:absolute; left:34%; cursor:pointer' title='Delete this file' alt='Delete this file' onclick=\"return confirm ('Are you sure you want to permanently delete this attachment?')\"></a>
I then have my delete_file.php which contains the following code:
<?php
$file_url = (string)$_GET['file'];
unlink($file_url);
?>
My problem is that whenever a file is deleted I am (of course) redirected to the delete_file.php page and I do not wish that. I would like to find a way to do the same thing while staying on the form itself.
Can you please help?
SECOND QUESTION (not sure if I'm allowed to post 2 questions in same post): Another issue I have is that with the file URL displayed above when I click on the URL the attachment does not open! If however I copy and paste the link into a different browser tab I do see the attachment. I'm using Mozilla 4.0.1. Do you know why?
// display link to file
echo "<a href='file://" . $file_url . "'>" . $file . "</a>";
Kind regards
Mishana
I've created a form where I can upload file attachments using PHP. I also want to give users option to delete these attachments by clicking on a button next to each file attachment.
This is how I do it in the form:
// display link to file
echo "<a href='file://" . $file_url . "'>" . $file . "</a>";
// display delete button
echo "<a href='delete_file.php?file=" . addslashes($file_url) . "'><img src='images/remove.jpg' style='position:absolute; left:34%; cursor:pointer' title='Delete this file' alt='Delete this file' onclick=\"return confirm ('Are you sure you want to permanently delete this attachment?')\"></a>
I then have my delete_file.php which contains the following code:
<?php
$file_url = (string)$_GET['file'];
unlink($file_url);
?>
My problem is that whenever a file is deleted I am (of course) redirected to the delete_file.php page and I do not wish that. I would like to find a way to do the same thing while staying on the form itself.
Can you please help?
SECOND QUESTION (not sure if I'm allowed to post 2 questions in same post): Another issue I have is that with the file URL displayed above when I click on the URL the attachment does not open! If however I copy and paste the link into a different browser tab I do see the attachment. I'm using Mozilla 4.0.1. Do you know why?
// display link to file
echo "<a href='file://" . $file_url . "'>" . $file . "</a>";
Kind regards
Mishana