Skip to content

Instantly share code, notes, and snippets.

@techslides
Created October 20, 2014 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save techslides/5df05f62f2a034ee537c to your computer and use it in GitHub Desktop.
Save techslides/5df05f62f2a034ee537c to your computer and use it in GitHub Desktop.
Save Ajax POST data as a file with PHP
<?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;
}
?>
@techslides
Copy link
Author

Find more information about this PHP script here: http://techslides.com/save-file-with-ajax-and-php/

@apaiktos
Copy link

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