Letters

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Silver_Eclipse
Forum Commoner
Posts: 61
Joined: Sun Aug 18, 2002 7:26 pm

Letters

Post by Silver_Eclipse »

how would i make a script that prints the whole file? i have tried the fread and fgets but when i try to print it, it only prints out one letter.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Code: Select all

$filename="test.txt";
$fd = fopen($filename, "rb");
while($line = fgets($fd, 1024))
   print($line);

Code: Select all

$filename="test.txt";
$lines = file($filename);
foreach($lines as $line)
   print($line);

Code: Select all

$filename="test.txt";
while($data = fread($fd, 1024))
   print($data);

Code: Select all

$filename="test.txt";
readfile($filename);
all these should read and output a file you have read permissions on.
Silver_Eclipse
Forum Commoner
Posts: 61
Joined: Sun Aug 18, 2002 7:26 pm

Post by Silver_Eclipse »

yeah got it to work just cant figure out how to get it to delete the file. and still need to find out how to remove certain lines and input lines in their place.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

using file() you have an array you can work on.
You may walk the array with foreach() writing the lines you want to keep, leaving those you want to delete out or change those you want to alter.

unlink() deletes a file.
Silver_Eclipse
Forum Commoner
Posts: 61
Joined: Sun Aug 18, 2002 7:26 pm

Post by Silver_Eclipse »

but with unlink() do i use the filepointer, or just type the file in or what?
Silver_Eclipse
Forum Commoner
Posts: 61
Joined: Sun Aug 18, 2002 7:26 pm

Post by Silver_Eclipse »

err doh unlink() failed permission denied on line 25
Silver_Eclipse
Forum Commoner
Posts: 61
Joined: Sun Aug 18, 2002 7:26 pm

Post by Silver_Eclipse »

did i mention i am on my own server Apache, with newest version of php.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

int unlink ( string filename) and you (/the script) need(s) write and/or modify permissions.

assuming apache on linux:
What are the owner/group/other permissions on that file.
and what effective uid/gid is your script running under?
Last edited by volka on Mon Aug 19, 2002 9:58 pm, edited 1 time in total.
Silver_Eclipse
Forum Commoner
Posts: 61
Joined: Sun Aug 18, 2002 7:26 pm

Post by Silver_Eclipse »

yeah but my server should have permissions because it reads and writes to the file but wont delete it.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

do you still have an open filedescriptor on that file when you try to delete it?
Silver_Eclipse
Forum Commoner
Posts: 61
Joined: Sun Aug 18, 2002 7:26 pm

Post by Silver_Eclipse »

filedescriptor? if thats like an open filepointer i probably do because i cant get fclose to work either argh
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

fopen returns an filedescriptor.
Can't use fclose? uhh?
Silver_Eclipse
Forum Commoner
Posts: 61
Joined: Sun Aug 18, 2002 7:26 pm

Post by Silver_Eclipse »

Warning: fclose(): 1 is not a valid File-Handle resource in file.php on line 19
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Code: Select all

$filename="test.txt"; 
$fd = fopen($filename, "rb") or die('failed to open $filename"); 
while($line = fgets($fd, 1024)) 
   print($line);
fclose($fd);
^ this doesn't work? 8O
Silver_Eclipse
Forum Commoner
Posts: 61
Joined: Sun Aug 18, 2002 7:26 pm

Post by Silver_Eclipse »

here's a chunk of my code:

Code: Select all

while(list($array) = each($file)){

if ($array > $a and $array < $b)&#123;
fputs ($fe, $file&#1111;$array], 100000);
fclose ($fe);
&#125;
Post Reply