PHP or other server-side scripting language. You'll have to communicate with it to write to a file, read its content, delete it, copy it and get its size. This however, requires that you have PHP installed on your server (most ship with PHP pre-installed). For the examples below, Flash Code is to be typed into a Frame's Actions, and the PHP Code is to be typed in a new text file, saved as the name in the bracket/parenthesis, and then you upload the PHP and Flash file to your server and test it with a browser. Also, to be able to edit/create files, you need to give the folder with the PHP and Flash files in, CHMOD 777 permissions (in most FTP clients, you just right-click the folder, click on CHMOD, and change the default, 644, to 777, or if that doesn't work, then it's, 0777).
~ Example Code ~
- Write to a File -
Flash Code:
ActionScript Code:
newContent = "New Content";
loadVariables("write.php", this, "POST");
PHP Code (write.php):
PHP Code:
<?php
$new_content = $_POST['newContent'];
$file = "somefile.txt";
$fileHandler = fopen($file, "w+"); // or a+
fwrite($fileHandler, $new_content);
fclose($fileHandler);
?>
loadVariables sends all variables from the target (in this case, it's this, which refers to the current level), so we're sending newContent variable to the PHP file, and when we do that, it'll execute its codes as well. In the PHP file, we fetch the sent data, using $_POST, which is an array with all the sent variables. Then, we open the target text file in w+ mode, which replaces all of its content and writes the new one in it instead, you can also use a+ to preserve its content and write new content at the end of the text file.
- Read a File -
Flash Code:
ActionScript Code:
loadFile = new LoadVars();
loadFile.onData = function(data){
trace(data); // traces content
}
loadFile("somefile.txt");
Flash can load the text file by itself, using LoadVars. The received content of the text file is stored inside
data, which you can manipulate at will.
- Delete a File -
Flash Code:
ActionScript Code:
fileName = "somefile.txt";
loadVariables("delete.php", this, "POST");
PHP Code (delete.php):
PHP Code:
<?php
unlink($_POST['fileName']);
?>
This will delete the file, assuming that it's in the same directory as the PHP file is in, otherwise you have to include the full path or the directory the file to delete is in.
- Copy a File -
Flash Code:
ActionScript Code:
fileName = "somefile.txt";
loadVariables("delete.php", this, "POST");
PHP Code (copy.php):
PHP Code:
<?php
copy($_POST['fileName'], $_POST['fileName'] . "_copy");
?>
The php function, copy, accepts 2 parameters, one for the file itself, and the other for the new file name (along with its path, if you want it to be copied into a new folder or something). Also, keep in mind that in PHP, you do not use + to join strings or other stuff together, the equivalent is . a dot
- Get a File's Size -
Flash Code:
ActionScript Code:
receiveVars = new LoadVars();
receiveVars.onLoad = function(success){
if(success){
trace(this.file_size);
}
}
sendVars = new LoadVars();
sendVars.fileName = "somefile.txt";
sendVars.sendAndLoad("getsize.php", receiveVars, "POST");
PHP Code (getsize.php):
PHP Code:
<?php
$filesize = filesize($_POST['fileName']);
echo "file_size=" . $filesize;
?>
With LoadVars' sendAndLoad function, you send variables to the PHP file (in this case, the name of the file), and also receive variables from the PHP file, which are stored in another LoadVars (in this case, receiveVars), where you can manipulate them as desired. To send variables, you type the name of the variable, followed by an equal sign, and then the value of the variable (with no spaces on either side of the equal sign). In this case, since PHP only executes its code on the server, before the page is displayed, nothing is actually displayed in the page, it's empty. To write something out with PHP, you use
echo, and in this case, we use echo to write out the variable and its value for Flash to fetch and store in receiveVars object. Then, those variables are stored inside receiveVars (which is why I use this.file_size, and not file_size standalone).
Hope this helps