Unlink/File_exists says the file does not exist, but the file does exist

Asked

Viewed 262 times

0

Hello,

I have a problem with PHP, I need a script that deletes a file in a directory, all passed by URL, as in the script I made below:

<?php

$file = !isset($_GET["f"])?0:$_GET["f"];
$dir = !isset($_GET["dir"])?"":$_GET["dir"];

var_dump($dir.$file);

if ($file === 0)
{
    echo 0;
    die();
}

if (file_exists($dir.$file)) 
    echo unlink($dir.$file);
else 
    echo 0;

?>

The problem is in the following, the function file_exists() and unlink(), of which I pass the path by parameter, say that the file I am trying to access does not exist. Using var_dump() I realized that the return is as follows:

inserir a descrição da imagem aqui

string(30) "C: xampp htdocs aaaa teste.txt"

The file exists in the specified directory, but the functions say no. See: inserir a descrição da imagem aqui

The file exists, the directory is correct, I’ve tried with (use the character deviation) but the same. Please help me!

Addendum to attempts already made:

  • Exchange file_exists() for is_file()
  • Use clearstatcache() before checking the file
  • Switch to \\

None have yielded results, all have failed.

  • In this case, the exit is 0, due to echo 0 in the else? Have you tried to indicate only the relative path of the archive?

  • Yes, the output 0 is due to echo 0, and the relative path I tried right now didn’t work either. And I’m using PHP 7 as well.

2 answers

1

I had the same problem these days.

Try to use urldecode() when reading the GET parameters:

$file = !isset($_GET["f"])?0:urldecode($_GET["f"]);
$dir = !isset($_GET["dir"])?"":urldecode($_GET["dir"]);

It may be that bars are being converted to %2F

  • Didn’t work out, buddy :/

  • @Joãoregis What appears on var_dump() of $dir.$file putting the url_decode()?

  • The same previous return, "string(30) "C: xampp htdocs aaaa teste.txt" "

  • Try trading for /

0


I managed to solve, I was doing tests on my local computer, some kind of lock or permission (I couldn’t identify yet) was preventing the function is_file() or file_exists() to check the file.

I moved the files to the company’s production server, and now it worked, thank you all very much.

I was confused because the folders were with full access permission (from my local computer), but the solution for me was to test on the production server.

Browser other questions tagged

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