Place directory path in a php variable

Asked

Viewed 805 times

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 folder "boards", however,I want to modify to a directory that is in another path, more specifically in "C: xampp htdocs boards", I tried to put $directory = "C: xampp htdocs boards"; and it didn’t work.

2 answers

2


Treat the link bar this way:

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

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

And if after that the url does not 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 use bars to separate directory.

  • It didn’t work. The if to test if the directory exists works with any other directory I put, except the boards folder inside the xamp/htdocs. Is it because it’s inside XAMP or something like that?

  • When you say that it does not work it is because you have displayed the message "The $directory folder does not exist", or it is because it has given error?

  • Why did the message appear that the directory does not exist...

  • The folder C: mpp htdocs boards does not exist

  • Try putting the url this way: "C:/xampp/htdocs/boards"

  • That was it... funny that I was copying the address... Thanks!

  • It is complicated to use this bar because it is used for some things like n line break or t tab, it is possible that x do something too, here came the problem.

  • I did some testing here too, with simple quotes also works 'C: xampp htdocs boards'

  • 1

    php is crazy too kkkkkkkkkkkkkk the program was working before I changed the directory, changed, tested, worked. Now it’s not working anymore, not even with the original code.

Show 5 more comments

-3

I don’t know, but put the error and suddenly try to use scandir, I think he’s more performatic and tals like in the example:

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

Ref.: http://php.net/manual/en/function.scandir.php

Browser other questions tagged

You are not signed in. Login or sign up in order to post.