php Scan function not returning results as expected after moving script path:

927 views Asked by At

note.. all folders chmod set to 777 for testing.

Okay, so i have been trying to design a simple cloud storage file system in php.After users log in they can upload and browse files in their account.

I am having an issue with my php code that scans the user's storage area. I have a script called scan.php that is called to return all of the users files and folders that they saved.

I originally placed the scan script in the directory called files and it worked properly, when the user logged in the scan script scanned the users files using "scan(files/usernamevalue)".

However I decided that I would prefer to move the scan script inside the files area that way the php script would only have to call scan using "scan(usernamevalue)". However now my script does not return the users files and folders.

enter image description here

<?php
session_start();
$userfileloc = $_SESSION["activeuser"];
$dir = $userfileloc;
// Run the recursive function 

$response = scan($dir);


// This function scans the files folder recursively, and builds a large array

function scan($dir)
{
 
 
 $files = array();

 // Is there actually such a folder/file?
 $i=0;
 if(file_exists($dir))
 {
  
  foreach(scandir($dir) as $f) 
  {
  
   if(!$f || $f[0] === '.') 
   {
    continue; // Ignore hidden files
   }
   

   
   
   if(!is_dir($dir . '/' . $f)) 
   {
    // It is a file

    $files[] = array
    (
     "name" => $f,
     "type" => "file",
     "path" => $dir . '/' . $f,
     "size" => filesize($dir . '/' . $f) // Gets the size of this file
    );
    //testing that code actually finding files
    echo "type = file, ";
    echo $f .", ";
    echo $dir . '/' . $f. ", ";
    echo filesize($dir . '/' . $f)." ";
    echo"\n";
   }   
   else 
   {

    
    // The path is a folder

    $files[] = array
    (    
     "name" => $f,
     "type" => "folder",
     "path" => $dir . '/' . $f,
     "items" => scan($dir . '/' . $f) // Recursively get the contents of the folder
    );
    
    //testing that code actually finding files
    echo "type = folder, ";
    echo $f .", ";
    echo $dir . '/' . $f. ", ";
    echo filesize($dir . '/' . $f)." ";
    echo"\n";
   }
   
   
  }
 
 }
 else
 {
  echo "dir does not exist";
 }
 
}



// Output the directory listing as JSON
if(!$response)
{ echo"failes to respond \n";}


header('Content-type: application/json');
echo json_encode(array(
 "name" => $userfileloc,
 "type" => "folder",
 "path" => $dire,
 "items" => $response
));
?>

As you can see i added i echoed out all of the results to see if there was any error in the scan process, here is what i get from the output as you can see the function returns null, but the files are being scanned, i cant seem to figure out where i am going wrong. Your help would be greatly appreciated. Thank you.


type = file, HotAirBalloonDash.png, test/HotAirBalloonDash.png, 658616

type = folder, New directory, test/New directory, 4096

type = file, Transparent.png, test/Transparent.png, 213

failes to respond

{"name":"test","type":"folder","path":null,"items":null}


1

There are 1 answers

0
Jozef Dúc On BEST ANSWER

You forgot to return files or folders in scan function, just echo values. That is the reason why you get null values in the response.

Possible solution is to return $files variable in all cases.