Jump to content

[SOLVED] Change location of a file, without having to update all pages


stickynote427

Recommended Posts

On my Web site, each page includes a navigation pane, and links to a certain style sheet. However, sometimes I wish to change the location of these files, and in order for every page to still find these files, I have to go in and edit every single file.

 

So, if I have:

  • background.png
  • stylesheet.css, which contains body {background: url(background.png);}
  • ten different PHP files, which contain <link href="stylesheet.css" rel="stylesheet">

and I move stylesheet.css to stylesheets/stylesheet.css, I don't want to have to go in to each of those ten PHP files and change ...href="stylesheet.css"... to ...href="stylesheets/stylesheet.css...".

 

Is there some file, maybe similar to .htaccess that is automatically applied to all the files in a directory, that I can use to define variables or something for the content I need in my PHP files? For example, maybe <link href="%stylesheetURL%" rel="stylesheet"> could go in the PHP files, and in another file, I could type in something like var stylesheetURL="stylesheets/stylesheet.css". Then, if I needed to change the location of the style sheet file, I could just change ..."stylesheets/stylesheet.css" to something else.

 

Please tell me if you need more information.

Link to comment
Share on other sites

there are multiple ways for you to do what you want.

One put all the urls in a database, and have the php load those page urls every time a user accesses the page

 

or create one master file which has all the urls in it as variables, and just change that variable on that page whenever the location changes

 

create a text file that has all the urls in it.

 

etc.

 

Hope that helped!

Link to comment
Share on other sites

Okay, so if I were to have a urls.txt file, containing:

//link to style sheet
http://www.whatever.com/stylesheets/stylesheet.css
//link to navigation file
http://www.whatever.com/nav.php

How would I read each of these in to my pages? I'm guessing I'd use fgets(), but I can't seem to find any way to specify which line to read.

 

Any help?

Link to comment
Share on other sites

Remove the //link to etc.

 

Use file to read them into an array, and iterate through the array to display those links.

 

$links = file('urls.txt');

foreach ($links as $link) {
    echo '<a href="' . $link . '" target="_blank">' . $link . '</a><br />';
}

 

Simple as that. If, however, you want to define the links so you can use them later on, this may work for you:

 

urls.txt

style:http://www.whatever.com/stylesheets/stylesheet.css
nav:http://www.whatever.com/nav.php

 

$links = file('urls.txt');

$linkArray = array();
foreach ($links as $link) {
    list($name, $link) = explode(":", $link);
    $linkArray[$name] = $link;
}

// other code
echo '<link href="' . $linkArray['style'] . '" rel="stylesheet">';
include($linkArray['nav']);

 

Not sure if that is what you are getting at. Hopefully that helps you figure out what you need. (I would put the foreach and above into an include file, then include that file on each page you wish to use the variables.) Then below the other code would be the code in the file that you want to be "dynamic".

Link to comment
Share on other sites

Aha! I played around with it and got it to work!

 

index.php

<?php

$myFile=file('http://www.whatever.com/urls.txt');
$GLOBALS['style']=$myFile[0];
$GLOBALS['leftcol']=$myFile[1];

?>

<link rel="stylesheet" href=<?=$style?>>

<div id="leftcol"><?php include($leftcol)?></div>

(There are HTML, HEAD, and BODY tags in there too, but didn't include them because they're not required, so please don't correct me on that. :))

 

Now I just have to edit that for all of my pages.

 

Thanks a ton!

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.