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
delete file using PHP while staying on same page
Moderator: General Moderators
-
mishana_sb
- Forum Newbie
- Posts: 3
- Joined: Wed Nov 30, 2011 11:48 pm
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: delete file using PHP while staying on same page
Code: Select all
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>Code: Select all
<?php
if ($_GET['action'] != '') {
if ($_GET['action'] == 'delete') {
// delete file
}
}
?>“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
-
mishana_sb
- Forum Newbie
- Posts: 3
- Joined: Wed Nov 30, 2011 11:48 pm
Re: delete file using PHP while staying on same page
Do you mean the a href should be as follows:
echo "<a href='delete_file.php#?file=" . addslashes($file_url) . "&action='delete''><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>
Is the hash correctly positioned? Since I've tried with adding the hash only for the time being and it still re-routes me to the other page.
echo "<a href='delete_file.php#?file=" . addslashes($file_url) . "&action='delete''><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>
Is the hash correctly positioned? Since I've tried with adding the hash only for the time being and it still re-routes me to the other page.
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: delete file using PHP while staying on same page
Use only the hash, while delete_page.php is in there it will keep going to the page (delete_page.php)
Have a look at this url for a better explanation and also a javascript solution
What-does-href-quot-quot-mean
Hth
Have a look at this url for a better explanation and also a javascript solution
What-does-href-quot-quot-mean
Hth
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
-
mishana_sb
- Forum Newbie
- Posts: 3
- Joined: Wed Nov 30, 2011 11:48 pm
Re: delete file using PHP while staying on same page
Hello
Thanks a lot
I finally understood what you were saying and it now works perfectly!
Since my original URL already had a parameter here is what I do:
// get back the page URL
$current_url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].":".$_SERVER["SERVER_PORT"].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].":".$_SERVER["SERVER_PORT"].$_SERVER['REQUEST_URI'];
// display link to file
echo "<a href='file://" . $file_url . "'>" . $file . "</a>";
// display delete button
echo "<a href='" . $current_url . "&file=" . addslashes($file_url) . "&action=delete'><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>
<br><br>";
And the code to delete the file is as follows:
if ($_GET['file'] != '')
{
echo "file set";
$file_url = (string)$_GET['file'];
// delete file attachment if parameter set
if ($_GET['action'] != '')
{
if ($_GET['action'] == 'delete')
{
unlink($file_url);
}
}
}
Thanks a lot
Since my original URL already had a parameter here is what I do:
// get back the page URL
$current_url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].":".$_SERVER["SERVER_PORT"].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].":".$_SERVER["SERVER_PORT"].$_SERVER['REQUEST_URI'];
// display link to file
echo "<a href='file://" . $file_url . "'>" . $file . "</a>";
// display delete button
echo "<a href='" . $current_url . "&file=" . addslashes($file_url) . "&action=delete'><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>
<br><br>";
And the code to delete the file is as follows:
if ($_GET['file'] != '')
{
echo "file set";
$file_url = (string)$_GET['file'];
// delete file attachment if parameter set
if ($_GET['action'] != '')
{
if ($_GET['action'] == 'delete')
{
unlink($file_url);
}
}
}