Hi, I created a script which I use to clean my database from missing entries. With it I both check if a file on the server is missing a database entry and if a database entry exist with no corresponding file on the server. The user first submits a form where he chooses if what exactly he wants to clean and if files with no corresponding database entry should be deleted or registered in the database.
The error I receive is:
Warning: in_array() expects parameter 2 to be array, null given in C:\xampp\htdocs\storage\php\file_upload\clean_database.php on line 59
...or line 48 if the user chooses to delete the files instead of registering them.
This is the script for the database cleaning:
<?php
require "../database/config.php";
require "../database/connect.php";
ini_set('display_errors', 1);
error_reporting(E_ALL);
$redirect = "http://".$_SERVER['HTTP_HOST']."/storage/"; //Where the browser shall be redirected to after execution
$myDirectory = $_SERVER['DOCUMENT_ROOT'].'/storage/uploaded_files/'; //Directory that should be cleaned
$httpDirectory = "http://".$_SERVER['HTTP_HOST']."/storage/uploaded_files/"; //Same directory but as url
//Settings for the clean up from the user
$option_cleanOrDelete = $_POST['submit'];
$option_entryMissing = $_POST['database_entry_missing'];
//Getting all the files from the directory into an array
$openDirectory = opendir($myDirectory);
while($entryName = readdir($openDirectory)){
$dirArray[] = $entryName;
#Making sure that no hidden files gets included if that option is choosed
if (!isset($_POST['clean_hidden'])){
if(substr(end($dirArray), 0, 1) == "."){
array_pop($dirArray);
}
}
}
closedir($openDirectory);
//Fetching database contents
$result = mysql_query("SELECT * FROM files");
$fileName_database = mysql_fetch_array($result);
//Checks if the user wants to delete the data or clean it
if ($option_cleanOrDelete == 'Clean'){
#checks if the user wants the database cleaned
if (isset($_POST['clean_database'])){
while($fileName_database = mysql_fetch_array($result)){
if (!in_array($fileName_database['filename'],$dirArray)){
mysql_query("DELETE FROM files WHERE filename='$fileName_database[filename]'") or die("Error :".mysql_error());
}
}
}
//File cleaning
#Checks if the files should be cleaned
if (isset($_POST['clean_files']) && !empty($dirArray)){
#If the user wants to delete files missing database entries
if ($option_entryMissing == "delete"){
for ($i = 0; $i < count($dirArray); $i++){
if (!in_array($dirArray[$i],$fileName_database['filename'])){
$fileName = $myDirectory.$dirArray[$i];
if (!unlink($fileName)){
echo ("Error deleting $fileName");
}
}
}
}
#if the user wants to register missing database entries
else {
for ($i = 0; $i < count($dirArray); $i++){
if (!in_array($dirArray[$i],$fileName_database['filename'])){
$permission = "1";
if (substr($dirArray[$i], 0, 1) == "."){
$hiddenFile = "yes";
}
else {
$hiddenFile = "no";
}
mysql_query("INSERT INTO files (fileName, size, filetype, directory, url, date_added, date_edited, hidden, permission) VALUES ('$dirArray[$i]','".filesize($myDirectory.$dirArray[$i])."','".filetype($myDirectory.$dirArray[$i])."','".$myDirectory.$dirArray[$i]."','".$httpDirectory.$dirArray[$i]."',NOW(),NOW(),'$hiddenFile','$permission')") or die('Error: '.mysql_error());
}
}
}
}
}
//This is if the user wants to delete all the content
else {
for ($i = 0; $i < count($dirArray); $i++){
$fileName = $myDirectory.$dirArray[$i];
if (!unlink($fileName)){
echo ("Error deleting the content of the directory and the database.");
}
}
while($fileName_database = mysql_fetch_array($result)){
mysql_query("DELETE FROM files") or die("Error :".mysql_error());
}
}
header("Location: ".$redirect);
?>
Any ideas? Any suggestions on improving the code is much appreciated!
I also have a question regarding the filetype() function I use to register the files in the database... is there anyway to get the same result written into the database as when you use $_POST[$fieldname] ("image/jpg" instead of just "file" as I get now)?
Thank you very much!