1

I have this line of CSS code here...

background-image:url(upload/<?php echo $array['image']; ?>);

but the dang thing ain't working, all I get back is a blank screen, The image is in the database and also on the server in the right file. Does PHP not work with CSS?

This is not a CSS File

Thanks in advanced, J

here is the full line of code...

<div style="width:100%; position:relative; margin:0 auto; padding-top:80px; height:350px; background-image:url(upload/<?php echo $array['image']; ?>); background-repeat:no-repeat; background-size:100%; text-align:center; padding-bottom:40px;">
4
  • I think you can set your htaccess file to run php on a css file. And if this is in an html page, then you should be able to set php to run on those too. Commented Apr 28, 2012 at 20:26
  • @M. Laing: interesting. But that would leave the client with a static css-cached version of one run unless your force the client to reload the css earch time. Commented Apr 28, 2012 at 20:27
  • can you just tell if the value you are printing is the path or the image in encoded format? Commented Apr 28, 2012 at 20:32
  • Could you please var_dump this "upload/$array['image']" with <?php var_dump("upload/" . $array['image']); ?> in your page? Commented Apr 28, 2012 at 20:33

7 Answers 7

5

Step 1:

I think you just missed quotes for url('something') so it should be like this:

<div style="width:100%; position:relative; margin:0 auto; padding-top:80px; height:350px; background-image:url('upload/<?php echo $array['image']; ?>'); background-repeat:no-repeat; background-size:100%; text-align:center; padding-bottom:40px;">

Step 2: If that doesnt work, show us exact output, not just "its not working", copy part of source code of displayed page so we can see if the value is printed correctly for example

Step 3: If that still doesnt work, i suggest you using "inspect element" function of some browsers to see what is problem. I can suggest Firebug extension for Firefox

Sign up to request clarification or add additional context in comments.

2 Comments

It really doesn't matter if you don't put quotes to a css url rule.
I tried it too on me, and you are true, it doesnt matter. But it could depend 1) on browser or 2) on url. I believe that some more complicated urls (without being 'quoted') might lead to wrong interpretation of CSS.
2

Inside a CSS file? no. (edit - see comments): might be technically feasible but still looks like a plan with a lot of downsides.

Inlined in output generated by PHP (or others, like html templates): yes.

10 Comments

There's no reason why PHP couldn't parse a CSS file as long as the server is set up to parse .css files or the CSS file has a .php file extension.
If it's in a html file: what gets generated as a value for background-image once you load the html page (view source)?
@Juhana: I did not know that was a common practice. But wouldn't that leave you with gigantic client-side caching issues since the php is bound to be used for the 'dynamic' parts of the CSS. Or would you force the client to reload the css for each and every request? Just informing...
Yeah, I'm not saying it's good practice (and it certainly isn't/shouldn't be common), but there's nothing technically preventing you to use PHP for whatever you want. PHP doesn't care what content it generates.
Could try try copy-pasting upload/imageintro-imageservices-1Product 320120320100APPLE_IMG_0032.jpg just after the html-page you are calling and seeing if your browser renders the image?
|
0

PHP doesn't care what it works with. You can use it for CSS files, JS files, images, whatever oyu you want. The important thing here is that the script has an extension that your webserver will identify as PHP and that the correct mime-type headers are sent.

If you are embedding the CSS in your html then it should be even much easier. If it doesn't work, then you need to verify if the output file that gets serves is indeed correct and if the selectors are indeed correct. Try hardcoding the background-image property first to see if that works.

Comments

0

Most likely your server does not use the php interpretor for HTML files. Change the suffix of your html file to .php and it should work.

Comments

0

Just make your current .css file a .php (or whatever PHP will execute, .php is about guaranteed to work), and then make the server declare it to be the right MIME type. The extension doesn't actually matter.

blah.php (was blah.css)

<?php
header('Content-type: text/css');
?>
.thing {blah;}
.other {blah;}
body {
     background-image:url(upload/<?php echo $array['image']; ?>);
}

Comments

0

If you want to have a dynamic "css" file you can create a php file and handle variables. To render it optimal, include header("Content-type: text/css"); before throwing anything, then put your css rules, and add your variables.

You can now call your file like <link rel="stylesheet" type="text/css" href="your_php_file.php"> and also you can handle parameters like <link rel="stylesheet" type="text/css" href="your_php_file.php?layout=minimal">

Your final php would be like:

<?php
    header("Content-type: text/css");
?>

    body{
            width: 0px; // your coded rules 
            background: url(upload/<?php echo $array['image']; ?> // the dynamic one
    }

It doesn't matter if you add or not quotes to the path.

Comments

-2

No, PHP does not work with CSS, but using templating engines or str_replace or preg_replace you can output the CSS in a <style> tag in the HTML. It's not the best idea though.

2 Comments

Again, saying that PHP doesn't work with CSS is just plain wrong.
If you read the question you see that the css styling is inside the html code, not a seperate .css file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.