2015-06-20 70 views
1

注意..所有文件夾chmod設置爲777進行測試。php掃描功能沒有按照預期的結果移動腳本路徑後返回結果:

好的,所以我一直在試圖設計一個簡單的雲存儲文件系統在php.After用戶登錄後,他們可以上傳和瀏覽他們的帳戶中的文件。

我有一個問題,我的PHP代碼,掃描用戶的存儲區域。我有一個名爲scan.php的腳本,它被調用來返回它們保存的所有用戶文件和文件夾。

我最初將掃描腳本放在名爲files的目錄中,當用戶登錄掃描腳本時使用「scan(files/usernamevalue)」掃描用戶文件,它工作正常。

但是我決定,我寧願移動文件區域內的掃描腳本,這樣php腳本只需使用「scan(usernamevalue)」調用掃描即可。但是現在我的腳本不會返回用戶文件和文件夾。

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) 
 
{ 
 
\t 
 
\t 
 
\t $files = array(); 
 

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

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

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

 
\t \t \t \t 
 
\t \t \t \t // The path is a folder 
 

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

 

 

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

 

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

正如你可以看到我加入我附和了所有的結果,看看是否有 是在掃描過程中的任何錯誤,這裏是我從輸出得到,因爲你可以看到函數返回null,但文件正在被掃描,我不能 似乎找出我要去哪裏錯了。您的幫助將大大促進 。謝謝。


類型=文件,HotAirBalloonDash.png,測試/ HotAirBalloonDash.png,658616

類型=文件夾,新建目錄,測試/新目錄,4096

類型=文件,Transparent.png ,測試/ Transparent.png,213個

failes響應

{ 「名稱」: 「測試」, 「類型」: 「文件夾」, 「路徑」:NULL, 「項目」 日期null}


+1

你沒有返回任何東西在你的功能,只是迴應的東西。所以返回文件或文件夾的數組。 –

+0

神聖的廢話不能相信我沒有發現!非常感謝你真的把我的頭髮拉出來。有時,我猜想有一個新的眼睛很好。不能相信我在這段代碼上苦苦掙扎了多久。 – ree

+0

你可以讓你的評論@JozefDúc答案,我可以接受它。 – ree

回答

2

你忘了在scan函數返回文件或文件夾,只是回波值。這就是爲什麼你在響應中得到null值的原因。

可能的解決辦法是在所有情況下返回$files變量。