Kritical_V Posted November 6, 2011 Share Posted November 6, 2011 Hello. This is my first post here and I was hoping someone can give me a hand, I am trying to develop a small piece of code that will display and image on my blog however if it cannot be found it shows some text. <?php $filename = '<?php bloginfo('template_directory');?>/images/hubs/<?php single_cat_title();?>.jpg'; if (file_exists('bloginfo('$filename')) { //show it } else { echo "Hello World"; } dc ?> The problem is with the second line, I am using two wordpress functions to generate the URL however I am new to PHP and there is something wrong with my code. Can anyone help Please... Thank You Link to comment https://forums.phpfreaks.com/topic/250561-php-if-file-exists-url-problem-really-need-help/ Share on other sites More sharing options...
haku Posted November 6, 2011 Share Posted November 6, 2011 You are using PHP tags in your second line. You shouldn't be. It should be this: $filename = bloginfo('template_directory') . '/images/hubs/' . single_cat_title() . '.jpg'; Link to comment https://forums.phpfreaks.com/topic/250561-php-if-file-exists-url-problem-really-need-help/#findComment-1285524 Share on other sites More sharing options...
haku Posted November 6, 2011 Share Posted November 6, 2011 Double post Link to comment https://forums.phpfreaks.com/topic/250561-php-if-file-exists-url-problem-really-need-help/#findComment-1285525 Share on other sites More sharing options...
Kritical_V Posted November 6, 2011 Author Share Posted November 6, 2011 Parse error: syntax error, unexpected T_VARIABLE Thats on the line you just gave me any ideas whats wrong? <?php $filename = bloginfo('template_directory') . '/images/hubs/' . single_cat_title() . '.jpg'; if (file_exists('bloginfo('$filename')) { //show it } else { echo "IMAGE NOT FOUND"; } dc ?> Link to comment https://forums.phpfreaks.com/topic/250561-php-if-file-exists-url-problem-really-need-help/#findComment-1285528 Share on other sites More sharing options...
Kritical_V Posted November 6, 2011 Author Share Posted November 6, 2011 Okay I fixed that problem, <?php $filename = bloginfo('template_directory') . '/images/hubs/' . single_cat_title() . '.jpg'; if (file_exists($filename)) { //show it } else { } dc ?> However the following is not printing: . '/images/hubs/' . . '.jpg' again any ideas Thanks Link to comment https://forums.phpfreaks.com/topic/250561-php-if-file-exists-url-problem-really-need-help/#findComment-1285534 Share on other sites More sharing options...
Kritical_V Posted November 6, 2011 Author Share Posted November 6, 2011 Still need a fix if anyone can help Link to comment https://forums.phpfreaks.com/topic/250561-php-if-file-exists-url-problem-really-need-help/#findComment-1285586 Share on other sites More sharing options...
haku Posted November 6, 2011 Share Posted November 6, 2011 what does the output look like? Link to comment https://forums.phpfreaks.com/topic/250561-php-if-file-exists-url-problem-really-need-help/#findComment-1285588 Share on other sites More sharing options...
xyph Posted November 6, 2011 Share Posted November 6, 2011 What's the issue now? What errors are you getting? The code looks fine, but I don't know what your functions are returning so I can't help. . '/images/hubs/' . . '.jpg' will never be output in your script. Link to comment https://forums.phpfreaks.com/topic/250561-php-if-file-exists-url-problem-really-need-help/#findComment-1285589 Share on other sites More sharing options...
Kritical_V Posted November 7, 2011 Author Share Posted November 7, 2011 It outputs the url wrong: http://www.mywebsite.com/content/themesEntertainment (Entertainment is the category name) It should print http://www.mywebsite.com/content/themes/images/hubs/Entertainment.jpg The /images/hubs/ & .jpg does not print. Link to comment https://forums.phpfreaks.com/topic/250561-php-if-file-exists-url-problem-really-need-help/#findComment-1285726 Share on other sites More sharing options...
Kritical_V Posted November 7, 2011 Author Share Posted November 7, 2011 Still not working, Anyone I really need help. Link to comment https://forums.phpfreaks.com/topic/250561-php-if-file-exists-url-problem-really-need-help/#findComment-1285758 Share on other sites More sharing options...
Kritical_V Posted November 7, 2011 Author Share Posted November 7, 2011 Okay been playing with it for the last hour and still nothing so I ask again for help. Please someone lend a hand. Link to comment https://forums.phpfreaks.com/topic/250561-php-if-file-exists-url-problem-really-need-help/#findComment-1285818 Share on other sites More sharing options...
haku Posted November 7, 2011 Share Posted November 7, 2011 You've got me. It's obviously returning values from the function calls, I can't imagine why it wouldn't be concatenating the text. Are you showing us your actual code there, or is it dummy code? I suppose you could break it down like this: $filename = bloginfo('template_directory'); $filename .= '/images/hubs/'; $filename .= single_cat_title(); $filename .= '.jpg'; But I really don't think this should be necessary. Link to comment https://forums.phpfreaks.com/topic/250561-php-if-file-exists-url-problem-really-need-help/#findComment-1285826 Share on other sites More sharing options...
Kritical_V Posted November 7, 2011 Author Share Posted November 7, 2011 No I have already tried that its still not outputting. Thats the actual code, The URL isn't tho. I'm considering rewarding whoever can do this... Link to comment https://forums.phpfreaks.com/topic/250561-php-if-file-exists-url-problem-really-need-help/#findComment-1285897 Share on other sites More sharing options...
xyph Posted November 7, 2011 Share Posted November 7, 2011 We'd have to see your exact chunk of code. There's no output in the snippet you've provided. <?php $filename = bloginfo('template_directory') . '/images/hubs/' . single_cat_title() . '.jpg'; if (file_exists($filename)) { echo 'Exists - '.$filename; } else { echo 'Not Exists - '.$filename; } ?> Link to comment https://forums.phpfreaks.com/topic/250561-php-if-file-exists-url-problem-really-need-help/#findComment-1285909 Share on other sites More sharing options...
Kritical_V Posted November 7, 2011 Author Share Posted November 7, 2011 Okay, <?php $filename = bloginfo('template_directory') . '/images/hubs/' . single_cat_title() . '.jpg'; if (file_exists('bloginfo('$filename')) { //show it } else { echo "IMAGE NOT FOUND"; } dc ?> That's the exact code I'm using, It's outputting the following as text: http://www.mysitesadress.com/content/themes/mythemeEntertainment The http://www.mysitesadress.com/content/themes/mytheme is from the bloginfo('template_directory') bit and Entertainment is from the single_cat_title() bit It should out put the image found at: http://www.mysitesadress.com/content/themes/mytheme/images/hubs/Entertainment.jpg and if its not found should run the Else clause. Link to comment https://forums.phpfreaks.com/topic/250561-php-if-file-exists-url-problem-really-need-help/#findComment-1285958 Share on other sites More sharing options...
xyph Posted November 7, 2011 Share Posted November 7, 2011 Have you tried running my code? The following works fine for me <?php $filename = bloginfo('template_directory') . '/images/hubs/' . single_cat_title() . '.jpg'; if (file_exists($filename)) { echo 'Exists - '.$filename; } else { echo 'Not Exists - '.$filename; } function bloginfo($str) { return $str; } function single_cat_title() { return 'Entertainment'; } ?> Returns Not Exists - template_directory/images/hubs/Entertainment.jpg Link to comment https://forums.phpfreaks.com/topic/250561-php-if-file-exists-url-problem-really-need-help/#findComment-1285972 Share on other sites More sharing options...
haku Posted November 8, 2011 Share Posted November 8, 2011 I agree - the code you have been given and are showing will work fine. If it's not working, then you must have some other code that's doing something strange. I concatenate strings and function output all the time, and it works fine. For a test, just do this: <?php $filename = bloginfo('template_directory') . '/images/hubs/' . single_cat_title() . '.jpg'; die($filename); ?> If it doesn't output the $filename correctly, I'm a monkey's uncle. Link to comment https://forums.phpfreaks.com/topic/250561-php-if-file-exists-url-problem-really-need-help/#findComment-1286126 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.