5, September 2008

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

 
Webdigity webmaster forums
This forum shares its ad revenue with its members!
[ 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

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



« on: Dec 13, 2005, 03: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.

Visit through proxy Visit through proxy Visit through proxy

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



« Reply #1 on: Dec 13, 2005, 09: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 Visit through proxy

Last blog : MIA - Where Nick and Tim
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 884
1636 credits
Members referred : 4



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

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

Visit through proxy Visit through proxy Visit through proxy

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



« Reply #3 on: Dec 13, 2005, 08: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 Visit through proxy

Last blog : MIA - Where Nick and Tim
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 884
1636 credits
Members referred : 4



« Reply #4 on: Dec 13, 2005, 09: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.

Visit through proxy Visit through proxy Visit through proxy

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



« Reply #5 on: Dec 13, 2005, 09: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 Visit through proxy

Last blog : MIA - Where Nick and Tim
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 884
1636 credits
Members referred : 4



« Reply #6 on: Dec 13, 2005, 10: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

Visit through proxy Visit through proxy Visit through proxy

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



« Reply #7 on: Dec 13, 2005, 10: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 Visit through proxy

Last blog : MIA - Where Nick and Tim
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 884
1636 credits
Members referred : 4



« Reply #8 on: Dec 13, 2005, 10: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

Visit through proxy Visit through proxy Visit through proxy

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



« Reply #9 on: Dec 13, 2005, 10: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 Visit through proxy

Last blog : MIA - Where Nick and Tim
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 884
1636 credits
Members referred : 4



« Reply #10 on: Dec 13, 2005, 10: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

Visit through proxy Visit through proxy Visit through proxy

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



« Reply #11 on: Dec 13, 2005, 10:29:03 PM »

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

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 Visit through proxy

Last blog : MIA - Where Nick and Tim
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....

Topic sponsors:
Get a permanent link here for $1.99!


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?
Sep 05, 2008, 09:46:48 PM





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!


Forum Statistics
Total Posts: 36.294
Total Topics: 7.477
Total Members: 3.899
Tutorials : 56
Resources : 143
Designs : 220
Latest Member: speedy5044

25 Guests, 5 Users online :

14 users online today:



Readers

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