Jump to content

PHP if file exists url problem really need help!


Kritical_V

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

Link to comment
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 
?>

 

Link to comment
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

Link to comment
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.

Link to comment
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;
}
?>

Link to comment
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.

 

 

Link to comment
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

Link to comment
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.

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.