PHP File renaming gone wrong -


i have 12 css files in system, , rename them whenever users want change style. store current style in database, , rename style.css style$color.css, renaming style$requestedcolor.css style.css right after that. here's code:

if ($_post['blue'] == "blue") {     if(file_exists('./css/styleblue.css'))     {     $old = './css/style.css';     $new = './css/style'.$color.'.css';     $old1 = './css/styleblue.css';     $new1 = './css/style.css';     rename($old, $new);     rename($old1, $new1);     $newcolor = "blue";     $idnum = "1";     mysql_query("update company set color = '$newcolor' id = '$idnum'");     } } 

problem is, (not always, finding hard diagnose when , why) end loosing file, while system grabs random .css file, renames style.css , writes correct $newcolor database. maybe renaming goes fast? or should grab each of these ifs (there's 12) info $color value?

i have sneaking suspicion $color doesn't set before start renaming files, verifying query completes , returns result before running block of code you've posted?

a better idea instead of renaming files change filename in <link> tag output on page? way don't have worry renaming process going sideways, or new style.css not overwriting old style.css in client's cache.

i have second sneaking suspicion you're not making full use of css inheritance either, given you're using generic filename of 'style.css'. better approach be:

php/html

<? $color = 'blue'; ?> <head>   <link rel="stylesheet" type="text/css" href="style.css">   <link rel="stylesheet" type="text/css" href="theme.<?= $color ?>.css"> </head> 

style.css

contains layout-related directives , default colors, eg:

body {   font-family: sans;   background-color: grey; }  #container {   width: 900px;   margin: 0px 20px;   background-color: white; } 

theme.blue.css

contains color-related directives override defined, eg:

body {   background-color: blue; }  #container {   background-color: purple; } 

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

c++ - Clear the memory after returning a vector in a function -

erlang - Saving a digraph to mnesia is hindered because of its side-effects -