spires Posted March 31, 2010 Share Posted March 31, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/197105-file_exists-help-please/ Share on other sites More sharing options...
PFMaBiSmAd Posted March 31, 2010 Share Posted March 31, 2010 File_exists() expects a file or a directory and does not work with most URL protocols - http://php.net/file_exists It does not work with http/https URL wrappers. You would need to use a local file system path in file_exists() Quote Link to comment https://forums.phpfreaks.com/topic/197105-file_exists-help-please/#findComment-1034639 Share on other sites More sharing options...
spires Posted March 31, 2010 Author Share Posted March 31, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/197105-file_exists-help-please/#findComment-1034649 Share on other sites More sharing options...
PFMaBiSmAd Posted March 31, 2010 Share Posted March 31, 2010 Is the code you posted being included by another page that is at a different path relative to where the images are at? Quote Link to comment https://forums.phpfreaks.com/topic/197105-file_exists-help-please/#findComment-1034654 Share on other sites More sharing options...
spires Posted March 31, 2010 Author Share Posted March 31, 2010 No, the code is on a stand alone file. All files are in the same folder, along with the GFX folder. Any other ideas? Quote Link to comment https://forums.phpfreaks.com/topic/197105-file_exists-help-please/#findComment-1034657 Share on other sites More sharing options...
PFMaBiSmAd Posted March 31, 2010 Share Posted March 31, 2010 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']; Quote Link to comment https://forums.phpfreaks.com/topic/197105-file_exists-help-please/#findComment-1034668 Share on other sites More sharing options...
spires Posted March 31, 2010 Author Share Posted March 31, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/197105-file_exists-help-please/#findComment-1034673 Share on other sites More sharing options...
PFMaBiSmAd Posted March 31, 2010 Share Posted March 31, 2010 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); Quote Link to comment https://forums.phpfreaks.com/topic/197105-file_exists-help-please/#findComment-1034686 Share on other sites More sharing options...
spires Posted March 31, 2010 Author Share Posted March 31, 2010 It shows nothing. No errors. Is this something that needs to be set on the server then? Quote Link to comment https://forums.phpfreaks.com/topic/197105-file_exists-help-please/#findComment-1034689 Share on other sites More sharing options...
PFMaBiSmAd Posted March 31, 2010 Share Posted March 31, 2010 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.) Quote Link to comment https://forums.phpfreaks.com/topic/197105-file_exists-help-please/#findComment-1034713 Share on other sites More sharing options...
spires Posted March 31, 2010 Author Share Posted March 31, 2010 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> Quote Link to comment https://forums.phpfreaks.com/topic/197105-file_exists-help-please/#findComment-1034762 Share on other sites More sharing options...
PFMaBiSmAd Posted March 31, 2010 Share Posted March 31, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/197105-file_exists-help-please/#findComment-1034783 Share on other sites More sharing options...
spires Posted March 31, 2010 Author Share Posted March 31, 2010 Your an absolute Legend I can't believe that's all it was, I've never heard of that before. Thank you so much for sticking with me though out this problem, it's much appreciated Quote Link to comment https://forums.phpfreaks.com/topic/197105-file_exists-help-please/#findComment-1034791 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.