View Full Version : Deleting, clearing, and creating files
04-17-2004, 08:26 PM
Is it possible to delete, clear, and create files with php?
04-17-2004, 09:09 PM
Yes..you can also create folders.
04-17-2004, 09:36 PM
http://php.net/filesystem is the filesystem function reference, though you'll probably be only interested in fopen (http://ca3.php.net/manual/en/function.fopen.php) for now, as it has a list of the basic functions at the bottom (before the comments)
04-18-2004, 01:35 PM
WOW! Php is very powerful. That is amazing :eek:
Is it possible to tell php to delete something in the middle of a file, or start the cursor there?
04-18-2004, 02:34 PM
see fseek() (http://www.php.net/fseek)
04-18-2004, 06:00 PM
Sorry, I didn't see that.
I took one of there example scripts and get errors
$fp = fopen('test_file.txt');
// read some data
$data = fgets($fp, 4096);
// move back to the beginning of the file
// same as rewind($fp);
Warning: fopen() expects at least 2 parameters, 1 given in /data/hosted/stophon4/ftell.php on line 3
Warning: fgets(): supplied argument is not a valid stream resource in /data/hosted/stophon4/ftell.php on line 6
Warning: fseek(): supplied argument is not a valid stream resource in /data/hosted/stophon4/ftell.php on line 10
Is it just my servers problem or am I not understanding this and missing something?
EDIT: I got a better idea. What code would I need to delete a line or insert a line somewhere (and type on that linee)?
I forgot to say earlier: All help is greatly appreicated :p
04-18-2004, 06:31 PM
you still need to open with a mode , here r+ if you want to seek and write
$fp = fopen( 'test_file.txt' , 'r+' ) ;
04-18-2004, 10:49 PM
I don't really get that, what code would I need to delete line 3 of a document ?
04-19-2004, 04:19 AM
just a line ? easist is along the lines of , note that '2' here is 'line 3' as the $file array counts from 0;
$filename = 'text.txt' ;
/*read file into memory as an array*/
$array= file( $filename ) ;
/*clear the contents of line 3*/
$fp = fopen( $filename , 'w' ) ;
fputs( $fp , implode( '' , $array ) ) ;
fclose( $fp );
note this is only efficient for small files , when dealing with big files the above method gets resource intensive & fseek etc is a beter option
04-19-2004, 08:59 PM
I am sending a deletion command for a line, but I am using a for loop variable. How do I retreive which line they chose to delete from the for loop?
$lines = count(file('guestbook.php')) + 1;
$filename = "guestbook.php";
$array= file( $filename ) ;
for ($i = 1; $i < $lines; ++$i)
echo "<form method='post' action='gb.php'><INPUT type='Submit' Name='delete' Value='Delete Post $i'></form>";
Powered by vBulletin® Version 4.2.2 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.