untz Posted January 21, 2016 at 03:06 PM Report Share #592260 Posted January 21, 2016 at 03:06 PM (edited) boa tarde a todos, eu tenho um problema no meu site que estou fazendo, e gostaria de ter uma ajuda se fosse possivel no ficheiro delete.php eu tenho isto: <?php $id=$_GET['id']; $result = mysql_query("delete from ficherio where id_ficheiro=$id"); if($result){ header("Location: index.php?true=1"); } else { echo "ERROR"; } ?> isto faz com que eu consiga apagar o registo, mas o que gostaria tambem que apagasse era o ficheiro que contem o registo, eu tento utilizar o unlink mas eu não percebi muito bem do que li, se alguem podesse ajudar ficaria muito agradecido Edited January 21, 2016 at 04:22 PM by thoga31 GeSHi Link to comment Share on other sites More sharing options...
tiago.f Posted January 21, 2016 at 03:17 PM Report Share #592261 Posted January 21, 2016 at 03:17 PM Oi, qual a estrutura da tabela "ficherio" ? Para usares o unlink tens que lhe passar o caminho para o ficheiro...mas não vejo nenhum instrução "unlink" no teu código... Link to comment Share on other sites More sharing options...
untz Posted January 21, 2016 at 03:40 PM Author Report Share #592263 Posted January 21, 2016 at 03:40 PM (edited) eu retirei-o. eu coloquei-o assim if($result){ $path = 'img/'.$row->Ficheiro; unlink($path); header... } ficherio: id_ficheiro nome_ficheiro Ficheiro ->leva no nome do ficheiro uploaded dataa descricao documentos_relacionados id_area id_assunto id_tipo_de_ficheiro Edited January 21, 2016 at 04:22 PM by thoga31 Tags code + GeSHi Link to comment Share on other sites More sharing options...
tiago.f Posted January 21, 2016 at 03:47 PM Report Share #592265 Posted January 21, 2016 at 03:47 PM (edited) Qq do género disto devia dar: <?php $id=$_GET['id']; $result = mysql_query("delete from ficherio where id_ficheiro=$id"); if($result){ $row = mysql_fetch_assoc($result); $path = 'img/'.$row['Ficheiro']; unlink($path); header("Location: index.php?true=1"); } else { echo "ERROR"; } ?> Assumindo que: - $row['Ficheiro'] tem o nome completo (com extensão) do ficheiro. - que $path aponta para um ficheiro existente. Testa o retorno do comando 'unlink' para ver se foi bem sucedido. Se não foi (false) então tenta colocar o caminho completo...talvez o path relativo não seja relativo ao que tu pensas que é... Edited January 21, 2016 at 04:22 PM by thoga31 GeSHi 1 Report Link to comment Share on other sites More sharing options...
untz Posted January 21, 2016 at 03:59 PM Author Report Share #592267 Posted January 21, 2016 at 03:59 PM obrigado, mas eu tentei com o mas não consegui, na base de dados o campo Ficheiro aparece lá a extenção do ficheiro tentei com o caminho completo xampp/htdocs/... e nada não consigo eliminar :s Link to comment Share on other sites More sharing options...
tiago.f Posted January 21, 2016 at 04:59 PM Report Share #592276 Posted January 21, 2016 at 04:59 PM O caminho completo começa com "/"... Qual o retorno to unlink? True ou false? Link to comment Share on other sites More sharing options...
untz Posted January 22, 2016 at 09:20 AM Author Report Share #592302 Posted January 22, 2016 at 09:20 AM sim, devolveu true Link to comment Share on other sites More sharing options...
tiago.f Posted January 22, 2016 at 09:50 AM Report Share #592303 Posted January 22, 2016 at 09:50 AM sim, devolveu true Então, de acordo com a documentação, o ficheiro foi apagado com sucesso. http://php.net/manual/en/function.unlink.php Link to comment Share on other sites More sharing options...
untz Posted January 22, 2016 at 09:56 AM Author Report Share #592304 Posted January 22, 2016 at 09:56 AM mas quando vou a pasta, ele continua lá, deveria de ser eliminado Link to comment Share on other sites More sharing options...
tiago.f Posted January 22, 2016 at 10:35 AM Report Share #592305 Posted January 22, 2016 at 10:35 AM (edited) Mostra as permissões do ficheiro e do directório onde este está. cd /xamp/htdocs/img ls -la um screenshot to terminal era excelente... Edited January 22, 2016 at 10:41 AM by tiago.f Link to comment Share on other sites More sharing options...
untz Posted January 22, 2016 at 11:29 AM Author Report Share #592308 Posted January 22, 2016 at 11:29 AM (edited) Como assim, não percebi, pode mandar um exemplo ? sff Edited January 22, 2016 at 11:48 AM by untz Link to comment Share on other sites More sharing options...
tiago.f Posted January 22, 2016 at 11:57 AM Report Share #592309 Posted January 22, 2016 at 11:57 AM Ok, as minhas desculpas, assumi que estavas a linux mas olhando melhor, estás a usar o xamp por isso deves estar em windows. Em vez de header("Location: index.php?true=1"); põe echo $path; e coloca aqui o resultado Depois navega no explorador do windows até esta pasta e mostra as permissões do ficheiro que tás a tentar apagar. Algo semelhante a isto, para o teu ficheiro. Link to comment Share on other sites More sharing options...
untz Posted January 22, 2016 at 12:44 PM Author Report Share #592312 Posted January 22, 2016 at 12:44 PM o resultado que deu foi o nome da pasta \img e apareceu isto Link to comment Share on other sites More sharing options...
tiago.f Posted January 22, 2016 at 01:09 PM Report Share #592314 Posted January 22, 2016 at 01:09 PM ??? Esse $path está mal, obviamente e, depois desta conversa toda, tem um valor "impossível". Como é que a instrução $path = 'img/'.$row->Ficheiro; faz com que o path fique com "\img" ? De onde vem a '\' ?? Para resolveres isto tens que garantir que o que é passado no comando unlink é um caminho válida para o ficheiro que queres apagar... "\img" nem sequer tem o nome do ficheiro!!! Link to comment Share on other sites More sharing options...
untz Posted January 22, 2016 at 02:17 PM Author Report Share #592321 Posted January 22, 2016 at 02:17 PM eu alterei o caminha do path, $path = '\img'.$row['Ficheiro']; caminho do ficheiro é este C:\xampp\htdocs\estagio\img logo eu só coloquei \img, acho que assim está certo. , no campo na base de dados aparece lá o nome do ficheiro, logo, no $row['Ficheiro'] está o nome do ficheiro! Link to comment Share on other sites More sharing options...
tiago.f Posted January 22, 2016 at 03:03 PM Report Share #592324 Posted January 22, 2016 at 03:03 PM se echo $path resultou em '\img" então $row['Ficheiro'] está vazio. Põe mesmo unlink("C:\xampp\htdocs\estagio\img\exame3.pdf"); A ver se ele apaga. Já agora, se queres caminho relativo, retira a barra do inicio e tb acho que falta uma barra entre o directorio e o nome do ficheiro. Poe assim $path = 'img/' . $row['Ficheiro']; Link to comment Share on other sites More sharing options...
untz Posted January 22, 2016 at 03:11 PM Author Report Share #592326 Posted January 22, 2016 at 03:11 PM (edited) com o unlink("C:\xampp\htdocs\estagio\img\exame3.pdf"); o ficheiro é apagado! ao usar $path = 'img/' . $row['Ficheiro']; não apaga o ficheiro, não sei porque, o nome do campo na base de dados está correcto! Edited January 22, 2016 at 03:19 PM by untz Link to comment Share on other sites More sharing options...
tiago.f Posted January 22, 2016 at 03:22 PM Report Share #592327 Posted January 22, 2016 at 03:22 PM (edited) cambada de cegos. O teu query é um delete, logo não traz resultado/linha nenhuma. 1º tens que fazer o select para obteres o $row['Ficheiro'] 2º fazer o delete da BD 3º fazes o delete do ficheiro com a info que tiraste do 1º <?php $id=$_GET['id']; $result = mysql_query("select * from ficherio where id_ficheiro=$id"); if($result){ $row = mysql_fetch_assoc($result); $path = 'img/'.$row['Ficheiro']; if(unlink($path) && mysql_query("delete from ficherio where id_ficheiro=$id")) { header("Location: index.php?true=1"); exit; } } echo "ERROR"; ?> Edited January 22, 2016 at 03:24 PM by tiago.f Link to comment Share on other sites More sharing options...
untz Posted January 22, 2016 at 03:26 PM Author Report Share #592328 Posted January 22, 2016 at 03:26 PM (edited) muito obrigado já está funcionando, desculpe lá a massada! eu fiz diferente do seu código, e funciona.. obrigado! Edited January 22, 2016 at 03:33 PM by untz Link to comment Share on other sites More sharing options...
tiago.f Posted January 22, 2016 at 03:34 PM Report Share #592329 Posted January 22, 2016 at 03:34 PM muito obrigado já está funcionando, desculpe lá a massada! eu fiz diferente do seu código, e funciona.. obrigado! Coloca aqui como ficou o resultado final para que os próximos possam aprender. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now