filesize() returning false in php

Course Queries Syllabus Queries 3 years ago

4.68K 1 0 0 0

User submissions are the sole responsibility of contributors, with TuteeHUB disclaiming liability for accuracy, copyrights, or consequences of use; content is for informational purposes only and not professional advice.

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 3 years ago


i'm trying to make a jason of the files and there size, its working perfectly for the directory that this php file is saved in but when i change the directory the filesize() returns false. this works fine:

php
  header('Content-Type: application/json');
  $dir= "./"; //path
  $list = array(); //main array

if(is_dir($dir)){
   if($dh = opendir($dir)){
       while(($file = readdir($dh)) != false){

           if($file == "." or $file == ".."){
               //...
           } else { //create object with two fields
               $list3 = array(
               'file' => $file, 
               'size' => filesize($file));
               array_push($list, $list3);
           }
       }
   }

   $return_array = array('files'=> $list);
   echo json_encode($return_array);
}
?>

but this doesn't work:

php
 header('Content-Type: application/json');
  $dir= "Syllabus/Undergraduate Courses/BCA/Semester 1"; //path
  $list = array(); //main array

if(is_dir($dir)){
   if($dh = opendir($dir)){
       while(($file = readdir($dh)) != false){

           if($file == "." or $file == ".."){
               //...
           } else { //create object with two fields
               $list3 = array(
               'file' => $file, 
               'size' => filesize($file));
               array_push($list, $list3);
           }
       }
   }

   $return_array = array('files'=> $list);
   echo json_encode($return_array);
}
?>

error message: 
Warning: filesize(): stat failed for Communicat_GeneralEnglish1stSemsterCopyCopy2Copy.pdf in C:\xampp\htdocs\college\listFiles.php on line 17

Warning: filesize(): stat failed for Environmental Studies_AECC-Environment-Studies-BCA.pdf in C:\xampp\htdocs\college\listFiles.php on line 17

Warning: filesize(): stat failed for Foundation Course On Computers_B.CA_.pdf in C:\xampp\htdocs\college\listFiles.php on line 17

Warning: filesize(): stat failed for Fundamentals Of Mathematics_B.CA_.3.pdf in C:\xampp\htdocs\college\listFiles.php on line 17

Warning: filesize(): stat failed for Problem Solving And Programming With C_B.CA_.2.pdf in C:\xampp\htdocs\college\listFiles.php on line 17
{"files":[{"file":"Communicat_GeneralEnglish1stSemsterCopyCopy2Copy.pdf","size":"0 bytes"},{"file":"Environmental Studies_AECC-Environment-Studies-BCA.pdf","size":"0 bytes"},{"file":"Foundation Course On Computers_B.CA_.pdf","size":"0 bytes"},{"file":"Fundamentals Of Mathematics_B.CA_.3.pdf","size":"0 bytes"},{"file":"Problem Solving And Programming With C_B.CA_.2.pdf","size":"0 bytes"}]}

0 views
0 shares

No matter what stage you're at in your education or career, TuteeHUB will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Similar Forum