Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > ActionScript Forums Group > ActionScript 2.0

Reply
 
Thread Tools Rate Thread Display Modes
Old 06-18-2012, 04:05 PM   #1
Howitzer21
Registered User
 
Join Date: May 2012
Posts: 27
Default How can you perform basic file I/O without using sendVars() and stuff like that?

My understanding is that stuff like sendVars() and loadVars() is more for saving variables and stuff like that. I just want to be able to deal with an ordinary text file on the server, and to be able to write to it, read from it, delete it, copy it, and get the size of it - all while having full control over its contents. I cannot believe this information is so difficult to find. How can it be done? Thanks!
Howitzer21 is offline   Reply With Quote
Old 06-18-2012, 06:42 PM   #2
Prid
Prid - Outing
 
Prid's Avatar
 
Join Date: Oct 2009
Location: Halden, Norway
Posts: 1,157
Send a message via MSN to Prid Send a message via Skype™ to Prid
Default

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
__________________
17 Years old boy, who loves the Computer Technology
Prid is offline   Reply With Quote
Old 06-19-2012, 02:04 PM   #3
Howitzer21
Registered User
 
Join Date: May 2012
Posts: 27
Default

Wow, that is thorough! Thanks much, man!
Howitzer21 is offline   Reply With Quote
Old 08-28-2012, 03:08 PM   #4
landar
Registered User
 
Join Date: Aug 2012
Posts: 3
Default

Prid...thanks for the detailed writeup. I have used Flash to upload content from the Flash player to the server using three methods: send vars, fileReference (browse,Import) and XML send.

I am trying to understand the XML send. In the "write to a file" example cited, the PHP seems to "hardcode" the Filename ($file = "somefile.txt"). Is there some way to embedded the Filename in the HTTP Post MIME header and have the PHP script strip that out and use it...such that the Filename is defined on the Flash side and not strictly determined by the PHP "plugin"?

Last edited by landar; 08-28-2012 at 03:15 PM.
landar is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT. The time now is 09:16 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.