开发者

fopen Called From AJAX Request [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜