Store a variable in a txt file or xml file

I do not have access to a database. Is there a way to store a php or javascript in a permanent txt file or xml file?

Any help would be appreciated


Direct answer to the question

To answer your question directly (storing one variable) you would do:

$file_path = '/path/to/your/text.file';
$my_var = 'my value';
file_put_contents($file_path, serialize($my_var));

To retrieve the value you would do something like this:

$my_var = '';
$file_contents = file_get_contents($file_path);
if(!empty($file_contents)) {
    $my_var = unserialize($file_contents);

By using serialize() you allow your code to store an array or stdClass object as well as numbers and strings in $my_var.

SQLite would be better

If the server supports SQLite then I would recommend using it if you need to store more than one variable.

To interact with SQLite I would recommend using PHPs PDO or perhaps Idiorm.

You can't store files with JavaScript. However you can easily write files (on the server) with PHP. If you would like to store a simple plain text to a file simply do this:

File_Put_Contents ( '/path/to/file.txt', 'some content' );

If you would like to manipulate XML, take a look at the SimpleXML extension.

You could also use SQLite for your database, as it is "just" a file and you don't have to install anything (PHP>=5).

If you have at least FTP access you need to create writable folder(i.e. set permissions to 777). From your PHP code:

$data = 'some data'; //xml or txt or whatever
file_put_contents('path_to_your_writable_folder/your_file_name.txt', $data);

Write it to a file, take a look at Flintstone a key/value database store using flat files.

Need Your Help

Template for canvas html5/jquery/javascript

javascript jquery html5-canvas

I am a beginner at HTML5, Jquery/JavaScript.

Selecting all the descendants of a tree node

mysql stored-procedures adjacency-list

I'm using the adjacency list model to store a (very dynamic) tree structure in a MySQL database. I need a way to select all of the descendants of a given node, preferably via a single call to a sto...