Jump to content

Archived

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

Kritical_V

PHP if file exists url problem really need help!

Recommended Posts

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

Share this post


Link to post
Share on other sites

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';

Share this post


Link to post
Share on other sites
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 
?>

 

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Okay been playing with it for the last hour and still nothing so I ask again for help. Please someone lend a hand.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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;
}
?>

Share this post


Link to post
Share on other sites

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.

 

 

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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