php 移动脚本路径后,扫描功能未按预期返回结果:
php Scan function not returning results as expected after moving script path:
注意.. 所有文件夹 chmod 设置为 777 进行测试。
好的,所以我一直在尝试设计一个简单的云存储文件系统,在 php.After 用户登录后,他们可以上传和浏览他们帐户中的文件。
我的 php 扫描用户存储区域的代码有问题。我有一个名为 scan.php 的脚本,它会调用 return 他们保存的所有用户文件和文件夹。
我最初将扫描脚本放在名为 files 的目录中并且它工作正常,当用户登录时扫描脚本使用 "scan(files/usernamevalue)" 扫描了用户文件。
但是我决定我更愿意将扫描脚本移动到文件区域内,这样 php 脚本只需使用 "scan(usernamevalue)" 调用扫描。但是现在我的脚本没有 return 用户文件和文件夹。
<?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}
您忘记在 scan
函数中 return 文件或文件夹,只是回显值。这就是为什么您在响应中得到 null
值的原因。
可能的解决方案是在所有情况下 return $files
变量。
注意.. 所有文件夹 chmod 设置为 777 进行测试。
好的,所以我一直在尝试设计一个简单的云存储文件系统,在 php.After 用户登录后,他们可以上传和浏览他们帐户中的文件。
我的 php 扫描用户存储区域的代码有问题。我有一个名为 scan.php 的脚本,它会调用 return 他们保存的所有用户文件和文件夹。
我最初将扫描脚本放在名为 files 的目录中并且它工作正常,当用户登录时扫描脚本使用 "scan(files/usernamevalue)" 扫描了用户文件。
但是我决定我更愿意将扫描脚本移动到文件区域内,这样 php 脚本只需使用 "scan(usernamevalue)" 调用扫描。但是现在我的脚本没有 return 用户文件和文件夹。
<?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}
您忘记在 scan
函数中 return 文件或文件夹,只是回显值。这就是为什么您在响应中得到 null
值的原因。
可能的解决方案是在所有情况下 return $files
变量。