Put directory path in a php variable

3

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 "boards" folder, however, I want to modify it to a directory that is in another path, specifically in "C: \ xampp \ htdocs \ C: \ xampp \ htdocs \ boards "; and it did not work.

    
asked by anonymous 19.10.2017 / 17:44

2 answers

2

Treat the link bar like this:

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

The DIRECTORY_SEPARATOR variable is global from php and it takes the "directory separator" from the system, it may be that the system uses slash or against slash, so I put the two in the slash array to handle any type of url.

And if after that the url does not work, make sure 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);

by

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

And wherever you use bars to separate directory.

    
19.10.2017 / 17:54
-3

I know, but I put the error and suddenly try to use the scandir, I think it is more performative and such as the example:

$directory = '/path/to/my/directory';
$excluded = ['.', '..', 'nome_do_arquivo_ou_pasta'];
$scanned_directory = array_diff(scandir($directory), $excluded);

Ref: link

    
19.10.2017 / 17:52