开发者

modifying a js file on my server and reloading it?

If I loaded a js file in a <textarea开发者_Python百科> with a save button below it, can I save that to an existing js file on my server and then reload it. Basically, what I want to do would work just like a wysiwyg except I want to be able to save the js to an existing file.

I am loading the js file using document.write ("<script src='file.js'></script>") which works.

BTW, this site would not let me put the '<' in the document write.


Short answer: Yes, but from your post I don't think you'd find it easy.

In some more depth, you'll need to use a server-side language such as PHP for this.

I won't go into how to get set up with PHP, we'll assume you know that, and if you don't then you have a great excuse to go Googling :) However, here's a simple working example script for you.

WARNING: There are a lot of pitfalls with allowing people to write to files on your server. You CERTAINLY don't want to make something like this accessible to anyone except HIGHLY trusted people. I can't over-emphasise how careful you should be with something like this unless you understand the implications of it.

<?php

// Change this to the file you want to be editing
// This path is relative to this script
// So if this script is in /somewhere/something/scripts/save.php
// And your file is at /somewhere/something/files/hello.txt
// This should read: $fname = "../files/hello.txt";
$fname = "something.txt";
// Now don't touch anything else :)



// This checks if we've submitted the file
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    // Get the posted content
    $content = $_POST['content'];

    // Open the file, or exit with an error message if we can't
    if (!$handle = fopen($fname, 'w')) {
        echo "Cannot open file ($filename)";
        exit;
    }
    // Write the new content into the file
    fwrite($fhandle, $content);
    fclose($fhandle);
}

// Get the contents of the file
$content = file_get_contents($fname);

?>
<form action="" method="post">
    <p>Editing <?php echo $fname; ?></p>
    <textarea name="content"><?php echo $content; ?></textarea>
    <input type="submit" value="Save" />
</form>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜