Created
October 20, 2014 19:15
Save Ajax POST data as a file with PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//error_reporting(E_ALL); | |
//var_dump($_SERVER); | |
$post_data = $_POST['data']; | |
if (!empty($post_data)) { | |
$dir = 'YOUR-SERVER-DIRECTORY/files'; | |
$file = uniqid().getmypid(); | |
$filename = $dir.$file.'.txt'; | |
$handle = fopen($filename, "w"); | |
fwrite($handle, $post_data); | |
fclose($handle); | |
echo $file; | |
} | |
?> |
Hi! I have just read 'Save File with Ajax and PHP' on http://techslides.com/save-file-with-ajax-and-php and I found it very useful. I tested and it works. However, I notice that there are two major limitations:
The first is that files are only saved on server in .txt form, not in .html or .php.
And the second is that files, even in .txt form, are impossible to be saved on a different folder, even if you change the $dir in both files (index.html and save.php).
Also, I was wondering if there is a way to name the file.txt directly rather than make it on the server.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Find more information about this PHP script here: http://techslides.com/save-file-with-ajax-and-php/