Jump to content

file_exists Help Please


spires

Recommended Posts

Hi

 

For some reason file_exists() can not see the file on my server.

The file (.jpg) is on the server at the correct path.

 

Can some one please point me in the right direction?

 

 

If I remove the file_exists($item_benefit_1) from the code, it works fine.

 

The code below brings up the following page:

https://comcalc.31e.com/comcalc/executive_summary_test.php?user=MEdwards&job_id=1&client=XTes

<!-- Benefits Page -->
<?PHP

	$get_item_sql = "SELECT DISTINCT xxxxxxx FROM xxxxxxx 
	WHERE xxxxxxx='$usName' && xxxxxxx='$client' && xxxxxxx='$job_id' 
	ORDER by stored_id ASC";
	$get_item_result = mysql_query($get_item_sql) or die ("query 3 failed".mysql_error());
	$get_item_count = mysql_num_rows($get_item_result);				

for ($a = 0; $a<$get_item_count; $a++){
	$get_item_row = mysql_fetch_assoc($get_item_result);
	$get_item_item = $get_item_row['xxxxxxx'];

	$expItemList = explode(' ', $get_item_item, 2);
	$expItemList2 = explode(' - ', $expItemList[1], 2);


	trim($expItemList[0]);
	trim($expItemList2[0]);
	trim($expItemList2[1]);


	$benefits_img_sql = "SELECT * FROM xxxxxxx 
	WHERE xxxxxxx='$expItemList[0]' && xxxxxxx='$expItemList2[0]' && xxxxxxx='$expItemList2[1]' 
	ORDER by xxxxxxx ASC";
	$benefits_img_result = mysql_query($benefits_img_sql) or die ("query 3 failed".mysql_error());
	$benefits_img_count = mysql_num_rows($benefits_img_result);				


	for ($b = 0; $b<$benefits_img_count; $b++){
	$benefits_img_row = mysql_fetch_assoc($benefits_img_result);
	$item_benefit_1 = str_replace("https://comcalc.31e.com/comcalc/", "",$benefits_img_row['xxxxxxx']);


	if ($item_benefit_1!='' && file_exists($item_benefit_1)) {

?>
<table width="580" border="0" cellspacing="0" cellpadding="0">
<tr>
 	<td height="820"><img src="<?=$item_benefit_1?>" width="580" height="820" border="0"/></td>
</tr>
</table>
<?PHP
                }else{
		echo "File " . $item_benefit_1 . " Does Not Exist" . "<br>";	
	}

}

}


?>

 

 

Any Help would be much appreciated.

 

Thanks

Link to comment
Share on other sites

Thanks for your help.

 

I have used:

$item_benefit_1 = str_replace("https://comcalc.31e.com/comcalc/", "",$benefits_img_row['item_benefit_1']);

 

This is to remove 'https://comcalc.31e.com/comcalc/' from the full path, leaving me with 'GFX/booklet/benefits/solo/BusinessSolo30-18months-Page3.jpg'

 

the GFX folder is in the same place as the .php file, so this should work.

 

Could there be a server issue?

 

 

Do you have any other ideas?

 

Thanks

Link to comment
Share on other sites

Have you echoed $item_benefit_1 to see what exactly is in it?

 

Anything unusual on the server, such as url_rewriting or an add on doamin that could make the actual path where the files are at different from where they appear to be when accessed using a URL (assuming that when you remove the file_exists() test that the page works...)

 

What does the following show -

 

echo getcwd();
echo $_SERVER['DOCUMENT_ROOT'];

Link to comment
Share on other sites

No, as far as i'm aware the server is quit standard, it's Linux running PHP 5.6

 

getcwd()  =  /var/www/html/comcalc

$_SERVER['DOCUMENT_ROOT']  =  /var/www/html

$item_benefit_1  =  GFX/booklet/benefits/solo/BusinessSolo30-18months-Page3.jpg

 

 

Thanks for your help with this, it's much appreciated.  :)

Link to comment
Share on other sites

The getcwd() appears to be correct.

 

This sounds like a open_basedir/safe_mode problem (your php code cannot access the file, but the web server can when the browser requests the image.)

 

What does adding the following two lines of code immediately after the first opening <?php tag show -

 

ini_set("display_errors", "1");
error_reporting(E_ALL);

Link to comment
Share on other sites

Everything you have shown says it should work.

 

I suspect there is something on the page in the portion of the code that you have not shown that is either prevented the error_reporting/display_errors code from exposing an error or is causing the problem (such as the page redirecting or being requested twice...) Could you post the whole code. xxxxx out any sensitive information, such as the database hostname/username/password, but it is not necessary to hide table and column names (assuming your code is protecting against sql injection.)

 

 

Link to comment
Share on other sites

OK, this is all of the code:

 

<?PHP
ini_set("display_errors", "1");
error_reporting(E_ALL);

session_start();

$host1='xxxxx';
$db1='xxxxx';
$dbusername1='xxxxxb';
$dbpassword1='xxxxx';
$sql1=mssql_connect($host1, $dbusername1, $dbpassword1) or die ('Could Not Connect to database 1');
mssql_select_db($db1) or die ('could not select database');



$host2='xxxxx';
$db2='xxxxx';
$dbusername2='xxxxx';
$dbpassword2='xxxxx';
$sql2=mysql_connect($host2, $dbusername2, $dbpassword2) or die ('Could not connect to database 2');
mysql_select_db($db2) or die ('could not select database');



$user = $_SESSION["usName"];
$access = $_SESSION["access"];
$client = urldecode($_GET['client']);
$usName = urldecode($_GET['user']);
$job_id = $_GET['job_id'];
$email = 'xxxxx';


?>
<html>
<head><title></title></head>
<body>


<!-- Benefits Page -->
<?PHP

			$get_item_sql = "SELECT DISTINCT xxxxx FROM csv_stored 
			WHERE stored_user='$usName' && stored_client='$client' && stored_job_id='$job_id' 
			ORDER by stored_id ASC";
			$get_item_result = mysql_query($get_item_sql) or die ("query 3 failed".mysql_error());
			$get_item_count = mysql_num_rows($get_item_result);				

			for ($a = 0; $a<$get_item_count; $a++){
				$get_item_row = mysql_fetch_assoc($get_item_result);
				$get_item_item = $get_item_row['stored_item'];

				$expItemList = explode(' ', $get_item_item, 2);

				$expItemList2 = explode(' - ', $expItemList[1], 2);


				trim($expItemList[0]);
				trim($expItemList2[0]);
				trim($expItemList2[1]);


				$benefits_img_sql = "SELECT * FROM xxxxx 
				WHERE item_network='$expItemList[0]' && item_internal_name='$expItemList2[0]' && item_contract_length='$expItemList2[1]' 
				ORDER by item_id ASC";
				$benefits_img_result = mysql_query($benefits_img_sql) or die ("query 3 failed".mysql_error());
				$benefits_img_count = mysql_num_rows($benefits_img_result);				


				for ($b = 0; $b<$benefits_img_count; $b++){
						$benefits_img_row = mysql_fetch_assoc($benefits_img_result);
						$item_benefit_1 = str_replace("https://comcalc.31e.com/comcalc/", "",$benefits_img_row['item_benefit_1']);


					if ($item_benefit_1!='' && file_exists($item_benefit_1)) {

?>
<table width="580" border="0" cellspacing="0" cellpadding="0">
<tr>
 	<td height="820"><img src="<?=$item_benefit_1?>" width="580" height="820" border="0"/></td>
</tr>
</table>
<?PHP
					}else{
						echo "File " . $item_benefit_1 . " Does Not Exist" . "<br>";	
					}

}

}


?>

</body>
</html>

Link to comment
Share on other sites

Ah ha. When all other common reasons have been eliminated, the likely reason becomes apparent...

 

There are probably some non-printing characters stored with the data that when used in file_exists() does not match a file, but when put into a URL don't matter.

 

Check for things like \r\n on the end of the data.

 

As a quick test, use the trim() function -

 

 $item_benefit_1 = trim(str_replace("https://comcalc.31e.com/comcalc/", "",$benefits_img_row['item_benefit_1']));

 

You can also use var_dump() on $item_benefit_1 to see if the length matches the number of visible characters.

             

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.