Jump to content

[SOLVED] PHP Filesystem help, glob ect.


jamesxg1

Recommended Posts

Hiya,

 

Basically i am trying to make a code that will find every .php formatted file in the directory and login it in a .txt formatted file like so,

 

FILENAME.php - FILESIZE

 

 

 

Here is the code i made so far,

<?php

$a_str = glob("*.php");

foreach ($a_str as $filename);

$size = filesize($filename);

$contents = implode(PHP_EOL, $a_str);

$contents = "$filename - $size";

$contents .= PHP_EOL . PHP_EOL;

file_put_contents("datafiles.txt", $contents);

print("$contents");

?>

 

It works except for the slight fact that it is only putting one line :( !,

 

this is copyed from the text file,

 

"write.php - 289"

 

and it wont do them all line after line how do i fix this ?,

 

Many thanks,

 

James.

 

Link to comment
https://forums.phpfreaks.com/topic/166090-solved-php-filesystem-help-glob-ect/
Share on other sites

you need braces around your foreach statement. I also shortened it up a little:

<?php
  $contents = "";
  foreach(glob("*.php") as $filename){
    $contents .= $filename . " - " . filesize($filename) . PHP_EOL . PHP_EOL;
  }
  file_put_contents("datafiles.txt", $contents);
  print $contents;
?>

Sorry for the extra post,

 

this works on screen (print"") but dont put it in the .txt file.

<?php

$a_str = glob("*.php");

foreach ($a_str as $filename) {

$size = filesize($filename);

$contents = "$filename - $size";

file_put_contents("datafiles.txt", $contents);

print("$contents<br>");
}
?>

 

you need braces around your foreach statement. I also shortened it up a little:

<?php
  $contents = "";
  foreach(glob("*.php") as $filename){
    $contents .= $filename . " - " . filesize($filename) . PHP_EOL . PHP_EOL;
  }
  file_put_contents("datafiles.txt", $contents);
  print $contents;
?>

 

 

Lol just seen the same lol,

 

but that's a lot shorter :D cheers mate thanks for your help,

 

rhodesa is a '  *  ' .

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.