Home > Software engineering >  PHP's filesize() throws warning stat
PHP's filesize() throws warning stat

Time:10-10

I want to print the file size of the songs using the filesize() function in PHP. This is my code:

<?php
    class parent_class{

        function file_size($filename){
                $size=filesize($filename);
            if( $size>1023 && $size<1048575){
                $size = $size/1024;
                $size = " (". $size." kb)";
            }
            else if(file_exists($filename) && $size>1048575){
                $size = $size/1048576;
                $size = " (". round($size, 2) ." mb)";
            }
            else{
                $size = " (". $size." b)";
            }

            return $size;
            }

        function displaysong(){
             $songs = glob('songs/*.mp3');
            foreach ($songs as $song){ ?>
                <li >
                    <a href="<?php echo $song; ?>"> <?php echo basename($song); echo $this->file_size($song);
                    ?></a>
                </li>
            <?php } 
        } 

         function display_playlists(){
                $playlists = glob('songs/*.txt');
                foreach ($playlists as $file){ ?>
                    <li >
                        <a href="music.php?playlist=<?php echo basename($file); ?>"> <?php echo basename($file) ; echo $this->file_size($file);?></a>
                    </li>
             <?php } 
         }

         function display_indivsong($file){ ?>
            <li >
                <?php $path = "songs/$file";?>
                <a href="<?php echo $path?>"> <?php echo $file; echo $this->file_size($path);?></a>
            </li>

         <?php }
        
} ?>

<body>
    
    <div id="header">

        <h1>190M Music Playlist Viewer</h1>
        <h2>Search Through Your Playlists and Music</h2>
    </div>
    
    <div id="listarea">
    <?php $song = NULL?>
        <form method="$_REQUEST" >
            <ul id="musiclist" name="playlist">                   
            <?php
            $song = new parent_class();
            if(!empty($_REQUEST["playlist"])){
                $playlist = $_REQUEST["playlist"];
                $path = pathinfo($playlist, PATHINFO_EXTENSION);
            }
            
            if(empty($playlist)){
                $song->displaysong();
                $song->display_playlists();
            } 
            else if(!empty($playlist) && $path == "txt"){
                $list = $playlist;
                $content = file("songs/$list");
                    foreach($content as $indiv_song){
                        $song->display_indivsong($indiv_song);
                    } 
            } 
            ?>

Everything works alright when I display the songs and their sizes. However, when display_indivsong() is called, filesize() throws an error. Note, inside display_indivsong(), the file is used again and it throws an error. But for the first time it is used in the same file, the sizes of the files are returned with no problems.

CodePudding user response:

In display_indivsong() you are appending "songs/" to the filename before calling $this->file_size($path). Instead of passing $path, you should instead pass the original $file:

function display_indivsong($file){ ?>
  <li >
  <?php $path = "songs/$file";?>
    <a href="<?php echo $path?>"> <?php echo $file; echo $this->file_size($file);?></a>
  </li>
<?php
}

This is because the argument passed to $file already contains the path.

Also, you're reading filenames from a textfile and then looping through those filenames:

$content = file("songs/$list");
foreach($content as $indiv_song){
  $song->display_indivsong($indiv_song);
} 

This will cause problems if you don't trim the trailing EOL. You should change that line to:

  $song->display_indivsong(trim($indiv_song));
  •  Tags:  
  • php
  • Related