Unlink command is not working if enabled via include

0

I'm using a file in the main directory of the site, with the following code:

<?php
$diretorioFuncoes = $_SERVER['DOCUMENT_ROOT']."/buscar"; // Dir dos arquivos
$arrayExcecoes = array(); // * Coloque aqui os arquivos que você quer que não sejam incluidos

if ($handle = opendir($diretorioFuncoes))
{
    while (false !== ($file = readdir($handle)))
    {
        if(strpos($file,".php")) // * Só inclui arquivos PHP
        {
            if(!in_array($file,$arrayExcecoes))
            {
                include($diretorioFuncoes."/".$filesize);
            }
        }
    }
    closedir($handle);
}
?>

This code above does "include" all files with .php extensions inside the "search" directory. And it's working perfectly.

Inside the "search" directory, I have several files with a code that executes a sql query inside the database. So far so good. I would like that after the command is executed, the file would auto-exclude itself, and I am trying to do this:

<?php

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

    if (new DateTime() > new DateTime("2015-04-17")) {
    # current time is greater than 2010-05-15 (Ano - Mês - Dia)

    $sql = "UPDATE 'url' SET 'type' = 'splash' WHERE 'url'.'custom' = '$custom'; ";

    if ($conn->query($sql) === TRUE) {
    unlink ("$custom.php");
    echo ("$custom")," atualizado com sucesso<br><br>";
    } else {
    echo "Error updating record: " . $conn->error;
}

}
else {
echo ("$custom")," - ainda não está na hora.<br>";
}

$conn->close();
?>

For the file to be deleted automatically, I am adding this line:

unlink ("$custom.php");

But when this file is executed through include, the unlink command returns this error:

  

Warning: unlink (meiamaratonasantoandre2016.php) [function.unlink]: No such file or directory in /public_html/search/meiamaratonasantoandre2016.php on line 21

One important note is that if I open the file directly through the browser, not through the include, the unlink command works perfectly.

Does anyone know what I might be doing wrong?

    
asked by anonymous 09.03.2016 / 20:11

2 answers

1

If you want the auto file to be deleted, you can do so

unlink(__FILE__);

The constant __FILE__ returns the absolute path of the current file.

However, there are factors that can affect the way you want to work, such as access and permission levels in files and directories.

If the permissions are properly configured, there should be no problem.

In another case a little further from the scenario you have, the file in question is locked for editing or removal by some other high-priority process.

note:

Observando o código que postou há diversas coisas estranhas.

De onde vem a variável '$custom'?

unlink ("$custom.php"); // vem de onde?
echo ("$custom")," atualizado com sucesso<br><br>"; // aqui vemos que está bem bagunçado.

Um pouco mais acima temos a query sql:

$sql = "UPDATE 'url' SET 'type' = 'splash' WHERE 'url'.'custom' = '$custom'; ";

Parece que '$custom', de onde quer que venha, parece que é um URL.

Talvez esteja tentando excluir uma URL, o que não é possível dessa forma pois URLs são caminhos virtuais.

São apenas suposições superficiais baseadas no que postou.

Sem mais delongas, acredito que a sugestão usando '__FILE__' deve resolver.
    
09.03.2016 / 21:14
0

You need to declare the absolute directory path.

$delete = "foo/bar/$custom";
unlink($delete);
    
09.03.2016 / 21:21