注意..所有文件夹chmod都设置为777进行测试。
好的,所以我一直在尝试在php中设计一个简单的云存储文件系统。用户登录后,他们可以上传和浏览他们帐户中的文件。
我的php代码存在问题,扫描用户的存储区域。我有一个名为scan.php的脚本,它被调用以返回他们保存的所有用户文件和文件夹。
我最初将扫描脚本放在名为files的目录中,当用户登录扫描脚本使用“scan(files / usernamevalue)”扫描用户文件时,它正常工作。
但是我决定我更喜欢在文件区域内移动扫描脚本,因为php脚本只需要使用“scan(usernamevalue)”调用扫描。但是现在我的脚本没有返回用户文件和文件夹。
<?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
));
?>
正如你所看到的,我补充说,我回应了所有的结果,看看是否存在 是扫描过程中的任何错误,这是我从输出中得到的 可以看到该函数返回null,但正在扫描文件,我不能 似乎弄清楚我哪里出错了。你的帮助会很大 赞赏。谢谢。
type = file,HotAirBalloonDash.png,test / HotAirBalloonDash.png,658616
type = folder,New directory,test / New directory,4096
type = file,Transparent.png,test / Transparent.png,213
错误回应
{ “名称”: “测试”, “类型”: “文件夹”, “路径”:NULL, “项目” 日期null}
答案 0 :(得分:2)
您忘记在scan
功能中返回文件或文件夹,只是回显值。这就是您在响应中获得null
值的原因。
可能的解决方案是在所有情况下都返回$files
变量。