Jump to content

file_exists & if, else, echo


Thomisback

Recommended Posts

Hi, I am trying to make a php script which retrieves a variable via flash and checks if it exists and returns back a different variable. For some reason it always says the does not exist, even if it does.

My code:

[code]
<?
$usr=$_POST['usr'];
$filename = 'http://www.mydomain.com/$usr/test.txt';

if (file_exists($filename)) {
$info = "Already exists";
echo("&info=$info");
} else {
    $info = "Success!";
echo("&info=$info");
}

?>

Does anyone see what it wrong with the code?

 

Thank you very much![/code]

Link to comment
https://forums.phpfreaks.com/topic/95069-file_exists-if-else-echo/
Share on other sites

use double quotes when inserting variables without concatenation:

 

$filename = 'http://www.mydomain.com/$usr/test.txt';

 

should be either:

$filename = "http://www.mydomain.com/$usr/test.txt";

or concatenate:

$filename = 'http://www.mydomain.com/'.$usr.'/test.txt';

 

 

hope this helps,

In addition to what uniflare posted to get the $usr variable to work, the last time I checked, file_exists() does not work with http/https protocols.

 

If this is a local file, you need to use a file system path, not a URL, in the file_exists() function.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.