...

View Full Version : Creating an editable style sheet



ttttt
08-30-2006, 07:31 PM
Hi,
I'm trying to create a script that opens a css style sheet and inside a form field, inserts the data from the style sheet, so that it can be edited as a whole. I'm also trying to have a submit button that writes the whole thing back to the file.
I know to open the file, I need to use fopen(), but that's about as far as my knowledge spreads. And I also know that the script isn't unique to a css stly sheet but could be used for any file.
I would appreciate some help with my problem greatly,
Thanks,

Mwnciau
08-30-2006, 07:46 PM
<?php
$file = $_GET['file'];
$css = file_get_contents($file);
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<textarea name="css"><?php echo $css; ?></textarea>
<input type="hidden" name="file" value="<?php echo $file; ?>" />
<input type="submit" />
</form>
<?php
if (isset($_POST['css']) && isset($_POST['file'])){
$file = $_POST['file'];
$css = $_POST['css'];
$f_handle = fopen($file, 'w+');
fwrite($f_handle, $css);
}
?>

ttttt
08-30-2006, 08:01 PM
Thanks for the quick reply, but when I run the script, I get an error on the line:

fwrite($f_handle, $css);
Can you help?
Also, where do I put the path to the file and can I put it as an URL or do I have to use a server path?
Thanks,

GJay
08-30-2006, 08:14 PM
<?php
//you know where the CSS is, so no need to let the user choose the file
$file='/var/www/mysite/css/style.css';

//perhaps confusingly, deal with the submission first
if(isset($_POST['the_css'])) {
$fp=fopen($file,'w+'); //open the file for *w*riting
$fwrite($fp,$_POST['css']); //write the contents
fclose($fp); //remember to close the file
}


//to display the form
$css=file_get_contents($file); //read in the file contents

//no need for php to get in the way for a while...
?>
<form action="" method="post">
<textarea rows="10" cols="40" name="the_css">
<?php echo $css; ?>
</textarea>
<input type="submit" value="Submit" />
</form>


the file specified in $file must be writable by the web-server.
Accepting the file location from the user, is a really really stupid thing to do.
Remember though, that people could put things in the CSS that you might not want them to.

ttttt
08-30-2006, 08:20 PM
I still get this error:
Warning: fopen(http://versa-soft.com/css.css) [function.fopen]: failed to open stream: HTTP wrapper does not support writeable connections. in
public_html/admin/css/editcss.php on line 29
Fatal error: Call to undefined function: () in public_html/admin/css/editcss.php on line 30
This error appears when the page is submitted. The inclusion of the css file works fine though.
Thanks for the help,

GJay
08-30-2006, 08:23 PM
as my example showed, it needs to be a file-system location, not a url.

ttttt
08-30-2006, 08:26 PM
This is a really stupid question:
How do I get to the public_html directory with a filesystem?
Thanks,

ttttt
08-30-2006, 08:58 PM
Ok, I've found out the last question, but I still get errors. I've totally changed the file permissions and I tried ignoring the errors, but then it totally wipes my style sheet.
Thanks for all of the help,

GJay
08-30-2006, 08:59 PM
it depends.
typically, you'll have:
/home/ttttt/public_html
(where ttttt is the username you log in with).
typing 'pwd' at a command line will tell you...think this will work with ftp as well as shell access.
if you're using a gui ftp client, then there must be some way of determining the current directory...

GJay
08-30-2006, 08:59 PM
without giving the errors, it's really difficult to help...

ttttt
08-31-2006, 09:14 AM
The error is:
Fatal error: Call to undefined function: () in /home/ttttt/public_html/admin/css/editcss.php on line 30
Thanks,

ttttt
08-31-2006, 09:43 AM
The thought suddenly dawned on me, I'm only using PHP 4. Does this make a difference when fdealing with the errors?
Thanks,

Lee Stevens
08-31-2006, 11:21 AM
Have you chnaged the permissions? to 777?

GJay
08-31-2006, 12:38 PM
typo on my part,


$fwrite($fp,$_POST['css']); //write the contents

should have been


fwrite($fp,$_POST['css']); //write the contents

ttttt
08-31-2006, 04:31 PM
It still doesn't work.
No PHP errors appear, but it doesn't write the style sheet, it doesn't wipe totally.
So far, I am using this code:


<?php
//you know where the CSS is, so no need to let the user choose the file
$file='/home/ttttt/public_html/css.css';

//perhaps confusingly, deal with the submission first
if(isset($_POST['the_css'])) {
$fp=fopen($file, 'a+'); //open the file for *w*riting
fwrite($fp,$_POST['the_css']); //write the contents
fclose($fp); //remember to close the file
}


//to display the form
$css=file_get_contents($file); //read in the file contents

//no need for php to get in the way for a while...
?>
<form action="" method="post">
<textarea rows="10" cols="40" name="the_css">
<?php echo $css; ?>
</textarea>
<input type="submit" value="Submit" />
</form> </p>
Thanks,

googleit
08-31-2006, 04:40 PM
try this

$fp=fopen($file,'a');

ttttt
08-31-2006, 04:44 PM
No that doen't work either.
The file is opened fine, but the contents aren't saved to the file. No PHP error messages either!
Thanks,

googleit
08-31-2006, 04:45 PM
what php version have you got installed?

ttttt
08-31-2006, 04:47 PM
PHP version : 4.4.4

googleit
08-31-2006, 05:02 PM
hmm maybe you cant save to a css file

Mwnciau
08-31-2006, 05:09 PM
Thats not it...



$file = 'hi.css';
$f_handle = fopen($file, 'a+');
fwrite($f_handle, 'Hi there, some css here!');
fclose($f_handle);
echo file_get_contents($file);

Try that... Anything output?

ttttt
08-31-2006, 05:32 PM
I get the file in the text box fine. No PHP errors, no wiping of the style sheet. However, the new contents aren't saved to the file.
Thanks,

GJay
08-31-2006, 06:32 PM
another type, $_POST['css'] on the fwrite line should be 'the_css'

ttttt
08-31-2006, 06:36 PM
Still the same as the last post.
Thanks,

Mwnciau
08-31-2006, 07:01 PM
You could just keep the stylesheet in a mysql database...

ttttt
08-31-2006, 07:20 PM
Could I also do that with xml?
And how would I do that?
Also,
we could try putting the form on one page and the writing to the css file on another page and have them linked by the submit button?
Thanks,

GJay
08-31-2006, 08:19 PM
The code I posted, with the 2 typos I've pointed out corrected works, I ran it on my own machine (something I don't normally do when giving answers, as I assume the questioner will actually read the code in an attempt to understand it and so spot simple mistakes...), so I can only assume the file isn't writable by the server.

ttttt
08-31-2006, 08:49 PM
It works!!!!
The final code is (by GJay):

<?php
//you know where the CSS is, so no need to let the user choose the file
$file='/home/ttttt/public_html/css.css';

//perhaps confusingly, deal with the submission first
if(isset($_POST['the_css'])) {
$fp=fopen($file,'w+'); //open the file for *w*riting
fwrite($fp,$_POST['the_css']); //write the contents
fclose($fp); //remember to close the file
}


//to display the form
$css=file_get_contents($file); //read in the file contents

//no need for php to get in the way for a while...
?>
<form action="" method="post">
<textarea rows="10" cols="40" name="the_css">
<?php echo $css; ?>
</textarea>
<input type="submit" value="Submit">
</form>
Thank you so much!,

Lee Stevens
08-31-2006, 08:51 PM
$fp=fopen($file, 'a+');


Should be



$fp=fopen($file, 'w');

googleit
08-31-2006, 08:53 PM
at last it works :P

Lee Stevens
08-31-2006, 08:57 PM
Heh, no problem, help 5 people in two days ;) I like this forum lol



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum