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

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 as follows, the function file_exists() and unlink() , of which I pass the path by parameter, say that the file I'm trying to access does not exist. Using var_dump() I noticed that the return is as follows:

  

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

The file exists in the specified directory, but the functions say no. Look:

The file exists, the directory is correct, I have already tried it with \\ (use character deviation) but it is still in it. Please help me!

Addition of attempts already made:

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

No results, all failed.

    
asked by anonymous 04.07.2017 / 16:18

2 answers

0

I was able to troubleshoot, I was testing on my local computer, some sort of block or permission (I could not identify yet) was preventing the is_file () or file_exists () function from scanning the file.

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

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

    
04.07.2017 / 16:55
1

I had the same problem these days ago.

Try using 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 the bars are being converted to% 2F

    
04.07.2017 / 16:31