ghurty Posted July 1, 2009 Share Posted July 1, 2009 I have the following code to pull up a "vendor" logo: while($vendor = $GLOBALS['ISC_CLASS_DB']->Fetch($result)) { $GLOBALS['VendorId'] = $vendor['vendorid']; $GLOBALS['VendorLogo'] = GetConfig('ShopPath').'/'.GetConfig('ImageDirectory').'/'."vendor_images/". $vendor['vendorid'] ."_logo.png"; Any suggestions how I can make it that if it does not find a file with that name, it will assign a different file to that variable instead? Thanks Link to comment https://forums.phpfreaks.com/topic/164343-how-if-file-not-found-assgin-different-file-instead/ Share on other sites More sharing options...
abdfahim Posted July 1, 2009 Share Posted July 1, 2009 make use of the function file_exists Link to comment https://forums.phpfreaks.com/topic/164343-how-if-file-not-found-assgin-different-file-instead/#findComment-866914 Share on other sites More sharing options...
ghurty Posted July 1, 2009 Author Share Posted July 1, 2009 Can you point me to a guide that explain the use of that in simple to understand ways? Thanks Link to comment https://forums.phpfreaks.com/topic/164343-how-if-file-not-found-assgin-different-file-instead/#findComment-866917 Share on other sites More sharing options...
abdfahim Posted July 1, 2009 Share Posted July 1, 2009 check the link http://php.net/manual/en/function.file-exists.php Link to comment https://forums.phpfreaks.com/topic/164343-how-if-file-not-found-assgin-different-file-instead/#findComment-866918 Share on other sites More sharing options...
ghurty Posted July 1, 2009 Author Share Posted July 1, 2009 So should this code work: while($vendor = $GLOBALS['ISC_CLASS_DB']->Fetch($result)) { $GLOBALS['VendorId'] = $vendor['vendorid']; $GLOBALS['VendorLogotemp'] = GetConfig('ShopPath').'/'.GetConfig('ImageDirectory').'/'."vendor_images/". $vendor['vendorid'] ."_logo.png"; if(file_exists( $GLOBALS['VendorLogotemp'] )){ $GLOBALS['VendorLogo'] = GetConfig('ShopPath').'/'.GetConfig('ImageDirectory').'/'."vendor_images/". $vendor['vendorid'] ."_logo.png"; } else { $GLOBALS['VendorLogo'] = GetConfig('ShopPath').'/'.GetConfig('ImageDirectory').'/'."vendor_images/"."defaultlogo.png"; } Thanks Link to comment https://forums.phpfreaks.com/topic/164343-how-if-file-not-found-assgin-different-file-instead/#findComment-866919 Share on other sites More sharing options...
abdfahim Posted July 1, 2009 Share Posted July 1, 2009 yup, though u r putting some extra line, u can do it like below <?php while($vendor = $GLOBALS['ISC_CLASS_DB']->Fetch($result)) { $GLOBALS['VendorId'] = $vendor['vendorid']; $GLOBALS['VendorLogo'] = GetConfig('ShopPath').'/'.GetConfig('ImageDirectory').'/'."vendor_images/". $vendor['vendorid'] ."_logo.png"; if(!file_exists( $GLOBALS['VendorLogo'] )){ $GLOBALS['VendorLogo'] = GetConfig('ShopPath').'/'.GetConfig('ImageDirectory').'/'."vendor_images/"."defaultlogo.png"; } } ?> Link to comment https://forums.phpfreaks.com/topic/164343-how-if-file-not-found-assgin-different-file-instead/#findComment-866921 Share on other sites More sharing options...
ghurty Posted July 1, 2009 Author Share Posted July 1, 2009 I forgot about using "!". Thanks a mil Link to comment https://forums.phpfreaks.com/topic/164343-how-if-file-not-found-assgin-different-file-instead/#findComment-866922 Share on other sites More sharing options...
ghurty Posted July 1, 2009 Author Share Posted July 1, 2009 I tried $GLOBALS['VendorId'] = $vendor['vendorid']; $GLOBALS['VendorLogo'] = GetConfig('ShopPath').'/'.GetConfig('ImageDirectory').'/'."vendor_images/". $vendor['vendorid'] ."_logo.png"; if(!file_exists( $GLOBALS['VendorLogo'] )){ $GLOBALS['VendorLogo'] = GetConfig('ShopPath').'/'.GetConfig('ImageDirectory').'/'."vendor_images/"."defaultlogo.png"; } And no matter what, it goes to the defaultlogo. However if I comment out the whole if !file_exists part, it displays the real logo. For some reason when checking, it considers the logos not found. Thanks Link to comment https://forums.phpfreaks.com/topic/164343-how-if-file-not-found-assgin-different-file-instead/#findComment-866949 Share on other sites More sharing options...
abdfahim Posted July 1, 2009 Share Posted July 1, 2009 see, you have to give absolute path (like "C:\apache\htdocs\logo\logo.jpg") or relative path (like "logo/logo.jpg" provided both your php file and the logo Folder resides on same folder) or use dot (../) to go up the directory. Link to comment https://forums.phpfreaks.com/topic/164343-how-if-file-not-found-assgin-different-file-instead/#findComment-866955 Share on other sites More sharing options...
ghurty Posted July 1, 2009 Author Share Posted July 1, 2009 But what gets me confused is that how come when I just have: $GLOBALS['VendorId'] = $vendor['vendorid']; $GLOBALS['VendorLogo'] = GetConfig('ShopPath').'/'.GetConfig('ImageDirectory').'/'."vendor_images/". $vendor['vendorid'] ."_logo.png"; It does pull up and display the logo properly. The php file and images are in two different directories. Link to comment https://forums.phpfreaks.com/topic/164343-how-if-file-not-found-assgin-different-file-instead/#findComment-866964 Share on other sites More sharing options...
abdfahim Posted July 1, 2009 Share Posted July 1, 2009 I suggest, 1) print $GLOBALS['VendorLogo'] variable 2) then hardcode that thing to file_exists function and check whether file_exists working properly. Link to comment https://forums.phpfreaks.com/topic/164343-how-if-file-not-found-assgin-different-file-instead/#findComment-866970 Share on other sites More sharing options...
ghurty Posted July 1, 2009 Author Share Posted July 1, 2009 Very interesting. In order for the if filexists to work, I had to code in the exact path. the following example DID work. THank you for all the help. if(!file_exists( '/homepages/2/d3452/htdocs/test'.'/'.GetConfig('ImageDirectory').'/'."vendor_images/". $vendor['vendorid'] ."_logo.png")){ Link to comment https://forums.phpfreaks.com/topic/164343-how-if-file-not-found-assgin-different-file-instead/#findComment-867253 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.