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

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

 

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

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.

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.

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

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.

 

 

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

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.

Archived

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

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