Unlink: No such file or directory

Asked

Viewed 785 times

1

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

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 folder system where is stored the information, but suddenly this error appears when I go to delete the publication, sometimes not all, and I do not find the error, being that its file is there, as in the image: Print My Code that deletes posts:

<?php
require "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 pattern utf8mb4 in the bank, if this is important

  • I may be totally wrong, but some operating systems combined with problematic PHP can give problems when using paths/files using accented characters (non-ASCII).

  • Read this post http://answall.com/questions/60316/pontos-e-acentos-em-urls-com-mod-rewrite

  • @Saul, I’m not sure, but I believe you’ll have to escape in the directory name due to the use of spaces

  • I was able to solve using utf8_decode

1 answer

2


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

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

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

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

The second problem is more complicated as it depends a lot on the operating system and the charset you are going through. To avoid handling all possible cases, I recommend that you use a string without special characters.

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

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

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

Browser other questions tagged

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