Put directory path in php variable

Question:

I have the following code

<?PHP
$directory = "placas";
//Get each file and add its details to two arrays
$results = array();
$handler = opendir($directory);
while ($file = readdir($handler)) {
    if ($file != '.' && $file != '..' && $file != "robots.txt" && $file != ".htaccess"){
        $currentModified = filectime($directory."/".$file);
        $file_names[] = $file;
        $file_dates[] = $currentModified;
    }
}
closedir($handler);
if(substr($file_dates[0],0, -2)==substr(time(),0, -2)){
    echo "confere no BD se existe a placa ". $file_names[0];

}
echo "<meta http-equiv='refresh' content='2;url=identificar_placa.php'>";
?>

the variable "directory" is containing the folder "boards", however, i want to change to a directory that is in another path, more specifically in "C:\xampp\htdocs\boards", I tried to put $directory = "C:\ xampp\htdocs\plates"; and it didn't work.

Answer:

Treat the link bar like this:

$directory = 'C:/xampp/htdocs/placas';
$barras = array("/", "\\");
$directory = str_replace($barras, DIRECTORY_SEPARATOR, $directory);
echo $directory;

The variable DIRECTORY_SEPARATOR is global in php it takes the "directory separator" of the system, maybe the system uses slash or backslash, so I put both in the slash array to handle any type of url.

And if after that the url doesn't work, check if the path is correct:

if (file_exists($directory)) {
    echo "O pasta $directory existe";
} else {
    echo "O pasta $directory não existe";
}

One more thing, replace:

$currentModified = filectime($directory."/".$file);

for

$currentModified = filectime($directory.DIRECTORY_SEPARATOR.$file);

And anywhere you want to use slashes to separate the directory.

Scroll to Top