-1

I`m making a php file to search images in a directory introducing the name, but the function preg_match returns me this error: "Warning: preg_match(): Delimiter must not be alphanumeric or backslash". The code is this:

<?php
$ruta='fotos'; 
// Usamos dir 
$dir=dir($ruta);     
// Archivo a Buscar 

$busqueda=$_POST['busqueda'] ;
$buscar = $busqueda; 

// Recorremos los Archivos del Directorio 
while ($elemento = $dir->read()) 
{     
     // Evitamos el . y ... 
    if ( ($elemento != '.') and ($elemento != '..')) 
    { 
        // Vemos si Existe el Archivo 
        if (preg_match($buscar, $elemento) AND is_file($ruta.$elemento)  ) 
        { 
            echo " Archivo : $elemento <br>"; 
        } 


    }       
}     
?>

It gives me the warning for each iteration in the loop. Ive trying to fix it but I cant. Can anybody help me, please?

Metalaria
  • 17
  • 1
  • 4

2 Answers2

0

You didn't do enough research on this question.

$buscar = "arica"; 

is your pattern I guess. I don't see any regex here but anway it needs to have delimiter.

From php manual:

When using the PCRE functions, it is required that the pattern is enclosed by delimiters. A delimiter can be any non-alphanumeric, non-backslash, non-whitespace character.

Often used delimiters are forward slashes (/), hash signs (#) and tildes (~). The following are all examples of valid delimited patterns.

So you need to use one of delimiters for example

$buscar = "/arica/"; 

however in your case remove preg_match() and use simply

$buscar == $elemento

It will do the same.

Morover, you should consider using DirectoryIterator. With it you could change your code to

$ruta='fotos'; 
$busqueda=$_POST['busqueda'] 
$iterator = new DirectoryIterator($ruta);
foreach ($iterator as $fileinfo) {
  if ($fileinfo->isFile() && $fileinfo->getFileName() == $busqueda) {
       echo " Archivo : $elemento <br>"; // here can be added 'break' I guess there is only 1 file with name you search for.
  }
}

I don't know what exactly you want to achieve but maybe you should just use file_exists();

Community
  • 1
  • 1
Robert
  • 19,800
  • 5
  • 55
  • 85
0

The error is cause by preg_match($buscar, $elemento). That call succeeds or fails depending on what the value of $buscar is. However, $buscar comes from the user, because of

$busqueda=$_POST['busqueda'] ;
$buscar = $busqueda;

First of all, not many users are able to formulate regular expressions, so asking a user for a regular expression is likely not a good idea. I don't know if you even intended to, because the variable $buscar is previously defined as $buscar = "arica" (it's not a good idea to reuse the same variable for different purposes, it confuses the developers.)

Secondly, strings that you pass to preg_match() as pattern must contain a delimiter. I don't know whether the a in $buscar = "arica" is a valid delimiter, it certainly is an unusual one (usually, / is chosen as delimiter, but others work as well). So it should be

$buscar = "/arica/";

but note that /arica/ won't be used as regular expression anway, because of what I said earlier.

Oswald
  • 31,254
  • 3
  • 43
  • 68