fopen Called From AJAX Request [closed]
I'm building a WebApp that needs to send a text and a filename through AJAX to the PHP script(on the sa开发者_JAVA技巧me place as the Javascript source of course) and the PHP script should save this file on the server, but how to make this?
That sounds very simple actually. You just send your AJAX request:
$.post("file.php", {filename:"text1.txt", text:"..."});
And in PHP only need:
file_put_contents($dir.basename($_POST["filename"]), $_POST["text"]);
Obviously you need a bit more authorization, a pre-defined save $dir
and using basename()
is only the minimum security precaution.
Use jQuery and you'll do it like
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript">
$.post('yourscript.php', {filename: 'output.txt', content: 'hello world'});
</script>
Instead of constants you can use textfields for your values. e.g.
$.post('yourscript.php', {filename: $('#filename').val(), content: $('#content').val()});
filename and content within the $-function are the ids of your textfields.
精彩评论