Unlink command is not working if enabled via include

Asked

Viewed 229 times

0

I am 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 above code makes the "include" of all files with extensions .php within the "search" directory. And it’s working perfectly.

Within the "search" directory, I have several files with a code that executes an sql query within the database. So far so good. I would like that after the command runs, the file deletes itself, and I am trying this way:

<?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();
?>

In order for the file to be deleted automatically, I am adding this line:

unlink ("$custom.php");

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

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

An important note, is that if I open the file directly by the browser, and not by include, the unlink command works perfectly.

Does anyone know what I might be doing wrong?

2 answers

1


If you want the file to auto delete, 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 functioning of what you want, such as access and permission levels in files and directories.

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

In another case a little farther away from being the scenario you have, is the file in question being blocked 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.

  • Hello Daniel, With the above tip worked too, but I ended up noticing the problem in the original code. It turns out I was running the file through include in a file from another directory, so the mentioned error.

  • Therefore the recommendation in the use of absolute path. Because regardless of the directory where it is executed, the path will always be found.

0

You need to declare the absolute path of the directory.

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

Browser other questions tagged

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