Unlink: No such file or directory

1

Hello, I was developing the pages and suddenly the following Warning occurred:

Warning: unlink(upload/publicacoes/Empresa Demonstração LTDA/Utilitários/Informações/2017/149029774958d4239521de6883c7046854e04138c55680ffde90a61.pdf): No such file or directory in C:\wamp64\www\MettaCont\html\deletePublicationA.php on line 30

In the system I develop is created a system of folders where the information is stored, but suddenly this error appears when I delete the publication, sometimes not all, and I do not find the error, and his file is there, as in the image: MyCodeDeletingPublications:

<?phprequire"conexao.php";
$pdo = conectar();

$codigo=$_GET['cod_publicacao'];
try{

$buscaSQL = $pdo->prepare("SELECT * FROM tbl_publicacao WHERE cod_publicacao = ?");
$buscaSQL->bindValue(1, $codigo, PDO::PARAM_INT);
$buscaSQL->execute();
$row = $buscaSQL->fetch(PDO::FETCH_ASSOC);

$publicacao = $row['publicacao'];
$ano = $row['ano'];
$tipo_publicacao = $row['fk_tipo'];
$fk_empresa = $row['fk_empresa'];
$arq=$row['arquivo'];

$searchSQL = $pdo->prepare("SELECT tbl_tipo_publicacao.cod_tipo_public, tbl_tipo_publicacao.tipo_publicacao, tbl_empresa.cod_empresa, tbl_empresa.razao_social FROM tbl_tipo_publicacao, tbl_empresa WHERE cod_tipo_public = ? AND cod_empresa = ?");

$searchSQL->bindValue(1, $tipo_publicacao, PDO::PARAM_INT);
$searchSQL->bindValue(2, $fk_empresa, PDO::PARAM_INT);
$searchSQL->execute();
$line = $searchSQL->fetch(PDO::FETCH_ASSOC);

$tipo_public = $line['tipo_publicacao'];
$razao_social = $line['razao_social'];
echo $arq;
$pasta = "upload/publicacoes/{$razao_social}/{$tipo_public}/{$publicacao}/{$ano}/";
unlink ($pasta.$arq);

$deleteSQL=$pdo->prepare("DELETE FROM tbl_publicacao WHERE cod_publicacao=:codigo");
$deleteSQL->bindValue(':codigo', $codigo);
//$deleteSQL->execute();

if($deleteSQL):
echo"<script>alert('Deletado com Sucesso!')</script>";
//echo "<script>window.history.back()</script>";
else:
echo"<script>alert('Falha ao deletar publicação!')</script>";
echo "<script>window.history.back()</script>";
endif;

}catch(PDOException $e){
    echo "ERROR: " .$e->getMessage()."<br>";
    echo "ERROR: " .$e->getCode();

}
?>

I'm using default utf8mb4 in the database, if this is important

    
asked by anonymous 23.03.2017 / 20:49

1 answer

2

As you can see this question in SOen, the problem is that the operating system expects the spaces to be escaped.

Another problem I see is the use of non-ANSII characters, such as ç .

To heal the problem, you must first escape the spaces. This code should solve this problem:

$filepath = str_replace(" ", "\ ", $filepath);

The second problem is more complicated because it depends a lot on the operating system and charset you are passing. To avoid treating all possible cases, I recommend that you use a string with no special characters.

A quick and easy way to do this is to convert the current string:

$filepath = iconv('UTF-8', 'ANSI//IGNORE', $filepath);

Note: I'm considering that your string is in UTF-8. Adjust if necessary.

    
24.03.2017 / 12:40