Current location: Hot Scripts Forums » Programming Languages » PHP » delete file issue


delete file issue

Reply
  #1 (permalink)  
Old 09-01-10, 03:17 AM
jonnekke jonnekke is offline
Code Guru
 
Join Date: Oct 2005
Location: holland!
Posts: 704
Thanks: 0
Thanked 0 Times in 0 Posts
delete file issue

Hi there,

I'm looking for a way to delete a file from a folder on clicking a link
but without refreshing the page. This should be possible ( I think with
AJAX or Javascript ) but no idea how.

_j

Last edited by jonnekke; 09-01-10 at 03:27 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 09-01-10, 04:36 AM
Yeroon's Avatar
Yeroon Yeroon is offline
Code Master
 
Join Date: Aug 2007
Location: Netherlands, Nijmegen
Posts: 850
Thanks: 2
Thanked 20 Times in 20 Posts
Hi Jonneke,

See if this article helps you: Animated AJAX Record Deletion Using jQuery

You could add delete logic to the part where the database update happens:

PHP Code:

if(isset($_GET['delete'])) 

  
$query 'DELETE FROM my_table WHERE item_id = '.(int)$_GET['delete'];
  
$result mysql_query($query,$link);
  
//add delete here

__________________
Feel free to thank people if they help you by clicking thanks at a post.
=================================
Make it idiot proof and someone will make a better idiot.
=================================
Realise the impotence of proof reading everything you publish
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 09-01-10, 04:42 AM
jonnekke jonnekke is offline
Code Guru
 
Join Date: Oct 2005
Location: holland!
Posts: 704
Thanks: 0
Thanked 0 Times in 0 Posts
there is no database update. It's just a file in a folder which needs to be deleted..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 09-01-10, 10:13 AM
Yeroon's Avatar
Yeroon Yeroon is offline
Code Master
 
Join Date: Aug 2007
Location: Netherlands, Nijmegen
Posts: 850
Thanks: 2
Thanked 20 Times in 20 Posts
Well working with the article I posted above you could do the following:

First create the page. Let's call it filehandler.php. Add logic to display files from a folder:

PHP Code:

<?php

if ($handle opendir('/path/to/files')) {
    echo 
"Directory handle: $handle\n";
    echo 
"Files:\n";

    
/* This is the correct way to loop over the directory. */
    
while (false !== ($file readdir($handle))) {
        echo 
'<div class="record" id="'.$file.'">
              <a href="filehandler.php?delete='
.$file.'">'.$file.'</a></div>'

    }
    
closedir($handle);
}
?>
Then you need the delete logic. Delete=filename.extension gets set in the querystring from the delete link:

PHP Code:

if(isset($_GET['delete'])) 

  
$myFile $_GET['delete'];
unlink('/path/to/files/$myFile');


Now you want this with jQuery. This code replace standard link (a tag) with Jquery function

javascript Code:
  1. $(document).ready(function() {
  2.   $('a.delete').click(function(e) {
  3.     e.preventDefault();
  4.     var parent = $(this).parent();
  5.     $.ajax({
  6.       type: 'get',
  7.       url: 'filehandler.php',
  8.       data: 'ajax=1&delete=' + parent.attr('id'),
  9.       beforeSend: function() {
  10.         parent.animate({'backgroundColor':'#fb6c6c'},300);
  11.       },
  12.       success: function() {
  13.         parent.slideUp(300,function() {
  14.           parent.remove();
  15.         });
  16.       }
  17.     });
  18.   });
  19. });
__________________
Feel free to thank people if they help you by clicking thanks at a post.
=================================
Make it idiot proof and someone will make a better idiot.
=================================
Realise the impotence of proof reading everything you publish

Last edited by Yeroon; 09-01-10 at 10:34 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 09-01-10, 10:34 AM
Yeroon's Avatar
Yeroon Yeroon is offline
Code Master
 
Join Date: Aug 2007
Location: Netherlands, Nijmegen
Posts: 850
Thanks: 2
Thanked 20 Times in 20 Posts
Chancged above because the div id was not getting set. Should be set now.
__________________
Feel free to thank people if they help you by clicking thanks at a post.
=================================
Make it idiot proof and someone will make a better idiot.
=================================
Realise the impotence of proof reading everything you publish
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Uploadify Jquery pugin file name with single quotes issue sneha414 PHP 1 04-13-10 08:39 AM
how to delete particular block of text from a text file? umarmir_123 PHP 6 06-04-09 01:46 AM
Hello all, can you help me? K4ot1K Other Languages 2 01-23-09 06:23 AM
create php file on the fly and delete when session ends recedo PHP 0 01-06-06 11:28 PM
i know how to delete a file! but how to delete a directory? forcer JavaScript 3 01-28-04 08:00 AM


All times are GMT -5. The time now is 04:02 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.