29, May 2012

need help writing a script that can simply clear or delete a file - webmaster forum

 
Webdigity webmaster forums
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: need help writing a script that can simply clear or delete a file
« previous next »
Pages: [1] Print
Instabuck - The easy way to sell digital products online

Author Topic: need help writing a script that can simply clear or delete a file  (Read 1027 times)
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 886
1148 credits
Members referred : 4



« on: Dec 13, 2005, 02:22:31 am »

i want to write a script that performs a simple task. when u click a link or button, it will automatically delete a file, or clear that file. is this possible? and if so how would it be done? im using a form to write to this file, its CSV but the same would work with a txt file. i want to be able to clear it from the website by just clicking a link/button.


Last blog : phpHaze 1.59.1 in Development
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #1 on: Dec 13, 2005, 08:45:25 am »

First of all you must be carefull with this script (reason is obvious...)

Let's say that you store the filename in a variable like :

Code:
<?php
 $file 
'/var/www/html/filetodelete.txt';

then to clear the file you need this :

Code:
<?php
 $fp 
fopen$file'w' );
 
fwrite $fp '' );
 
fclose $fp );

to delete it just use this :

Code:
<?php
 unlink 
$file );

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 886
1148 credits
Members referred : 4



« Reply #2 on: Dec 13, 2005, 06:07:24 pm »

ok but how would i make a link that performs that task


Last blog : phpHaze 1.59.1 in Development
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #3 on: Dec 13, 2005, 07:35:56 pm »

It seems that you do not know much of PhP but it's ok Smiley

I can write this for you, but you have to tell me some basic stuff.

Do you want it to delete specific files each time or a variable file?

If the file is variable does all your files exist in a specific directory? This has to do with the security of the script

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 886
1148 credits
Members referred : 4



« Reply #4 on: Dec 13, 2005, 08:36:36 pm »

its just a single file that creates itself everytime u save changes on the script i have written. so the php script to delete it could delete it completely, or just clear it. doesnt matter. currently the file is located in the same dir as the script, its a csv file.


Last blog : phpHaze 1.59.1 in Development
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #5 on: Dec 13, 2005, 08:44:58 pm »

So here is your code :

Code:
<?php
 $filename 
'change_this_with_your_filename';
 if ( isset( 
$_GET['delete'] ) )
 {
     
unlink './' $filename );
 }
?>


Put this on a php file (eg. del.php) and you should call it this way :

site.com/del.php?delete=1

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 886
1148 credits
Members referred : 4



« Reply #6 on: Dec 13, 2005, 09:02:11 pm »

i get this error when trying to delete the file with the link

Warning: unlink(./db.csv): Permission denied in /hsphere/local/home/meth0d42/meth0d.justinlove.net/del.php on line 5


Last blog : phpHaze 1.59.1 in Development
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #7 on: Dec 13, 2005, 09:11:41 pm »

It seems that you don't have enough permissions.

Use this :

Code:
<?php
$filename 
'change_this_with_your_filename';

if ( isset( 
$_GET['delete'] ) )
{
 
$fp fopen$filename'w' );
 
fwrite $fp '' );
 
fclose $fp );
}
?>


Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 886
1148 credits
Members referred : 4



« Reply #8 on: Dec 13, 2005, 09:14:22 pm »

Warning: fopen(db.csv): failed to open stream: Permission denied in /hsphere/local/home/meth0d42/meth0d.justinlove.net/del.php on line 6

Warning: fwrite(): supplied argument is not a valid stream resource in /hsphere/local/home/meth0d42/meth0d.justinlove.net/del.php on line 7

Warning: fclose(): supplied argument is not a valid stream resource in /hsphere/local/home/meth0d42/meth0d.justinlove.net/del.php on line 8


Last blog : phpHaze 1.59.1 in Development
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #9 on: Dec 13, 2005, 09:21:47 pm »

You should contact with your hosting company.

This is a permission problem. If you have shell access to your server, then run this:

Code:
chown apache:apache db.csv

If this wont work try


Code:
chmod 755 db.csv

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 886
1148 credits
Members referred : 4



« Reply #10 on: Dec 13, 2005, 09:25:30 pm »

great man it works now! i should of known to chmod it im not that stupid.. but how to we tell the script to reload the page u were on once its finished clearing it? because it shows a blank page if its successful when i want it to show either the same page (index.htm) or a pop-up confirmation or something


Last blog : phpHaze 1.59.1 in Development
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #11 on: Dec 13, 2005, 09:29:03 pm »

<?
 header ( "Location: http://urlToRedirect.com/" );
?>

By the way I don't wont to offend you, but you should not start a hosting service without have knowledge of this stuff. It is a bit risky.

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=980
Tags : php pop up hosting Bookmark this thread : Digg Del.icio.us Dzone more....

Pages: [1] Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: need help writing a script that can simply clear or delete a file
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 29, 2012, 01:20:15 am





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!






Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2012 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.