I have a strange problem with testing file_exists.

When I hardcode the file name 007-PCSafety.zip file_exists works fine but when I post the filename 007-PCSafety.zip it doesn't recognise the file exists.

here's the code. I'm swapping the $p variable to test.

if($_POST){

$url = $_POST['dl'];

$filename = basename($url).PHP_EOL;

$p=$filename;
//$p="007-PCSafety.zip";

if(file_exists($p)){
	echo $p.' File already exists.<br><form method=post action="exists.php"><input type=text name=dl value=""><input type=submit></form>';
 
} else

	echo $p.' File does not exist.<br><form method=post action="exists.php"><input type=text name=dl value=""><input type=submit></form>';
}

Recommended Answers

All 8 Replies

Can you do a "var_dump($url)" on line 4 and post the results.

<form method="post" action="exists.php"><input type="text" name="dl" value=""><input type="submit"></form> depending on the html version you are writing to, proper form is important
without the quotes $_post may not contain anything

Can you do a "var_dump($url)" on line 4 and post the results.

reads string(16) "007-PCSafety.zip"

<form method="post" action="exists.php"><input type="text" name="dl" value=""><input type="submit"></form> depending on the html version you are writing to, proper form is important
without the quotes $_post may not contain anything

Thanks. It's returning the filename ok.

echo $p.' File already exists....';


Edit:
Ahah! I think I see the problem: Try removing the .PHP_EOL it looks like you are adding a line ending to the file name. Let me know if that does the trick.


Edit:
Ahah! I think I see the problem: Try removing the .PHP_EOL it looks like you are adding a line ending to the file name. Let me know if that does the trick.

Many thanks!! That was it. Really appreciate that, it was doing my head in :)

You're quite welcome! I'm having one of those nights myself. I misplaced a period and spent half an hour trying to find it. :P

You're quite welcome! I'm having one of those nights myself. I misplaced a period and spent half an hour trying to find it. :P

Haha, Nothing worse than a missplaced period. Particularly if ur one of the 51% of the population. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.