A Flash Developer Resource Site

Results 1 to 16 of 16

Thread: pasing date/time stamp to PHP for filename

Hybrid View

  1. #1
    Senior Member whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,755

    pasing date/time stamp to PHP for filename

    ok.. I have my code working.. basically the user drags some clips around the stage (building a chopper)..and hits the "save" button..this will export all the needed properties (_x, _y, _xscale, _yscale..etc..etc) to a textfile as a string (example: &tankData=val1, val2, val3..etc) which i will import and split into an array...so far so good (although i havent tested the loading part yet..should be fine)...as of now I have my PHP script dumping these values to a hardcoded textFile name. (build1.txt)..what i want to do is...instead of using the SAME text file for each save.. I want each save to have its OWN text file. So to be unique I am using a date/tie stamp fucntion I wrote to make a unique fileName that wont be overwritten.

    what I need help on..is ow to pass this function the PHP script?

    here all my code...

    on my SVAE BUTTON: (I call the saveBuild function I wrote)

    button:
    Code:
    on (release) {
    	// [0]=x [1]=y [2]=xscale [3]=yscale [4]=_currentframe [5]=_rotation [6]=partName
    	var frameData = [_root.mainContainer.frameContainer.frame_mc._x, _root.mainContainer.frameContainer.frame_mc._y, _root.mainContainer.frameContainer.frame_mc._xscale, _root.mainContainer.frameContainer.frame_mc._yscale, _root.mainContainer.frameContainer.frame_mc._currentframe, _root.mainContainer.frameContainer.frame_mc._rotation, _root.frameChoice];
    	var tankData = [_root.mainContainer.tankContainer.tank_mc._x, _root.mainContainer.tankContainer.tank_mc._y, _root.mainContainer.tankContainer.tank_mc._xscale, _root.mainContainer.tankContainer.tank_mc._yscale, _root.mainContainer.tankContainer.tank_mc._currentframe, _root.mainContainer.tankContainer.tank_mc._rotation, _root.tankChoice];
    	saveBuild();
    	
    }
    the function it calls is THIS:

    saveBuild function:
    Code:
    function saveBuild() {
    	
    	// call the PHP script & return if save was sucessful.
    	//myTargetPath = "http://www.dmstudios.net/DBB/";
    	
    	// Format variables for PHP
    	sendBuild = new LoadVars();
    	sendBuild.frameData = "&frameData="+frameData;
    	sendBuild.tankData = "&tankData="+tankData;
    	reply = new LoadVars();
    	reply.onLoad = function(success) {
    		if (success) {
    			//error is a variable echo'd in the PHP file.
    			response_txt.text = reply.error; 
    			//check text field to see if the values wrote correctly. (only for testing wont go live)
    			_root.getURL("http://www.dmstudios.net/DBB/build1.txt?" add random(9999), "_blank");
    		}
    	};
    	sendBuild.sendAndLoad("writeTextFile2.php", reply, POST);	
    }
    and here is the PHP script I am using:

    PHP code:
    PHP Code:
    <?
    $myFileName="build1.txt";  // note that either the "files" folder needs chmod 777 or test.dat needs chmod 666 to save anything on unix server
    // note further that accepting an unchecked filename from the web is an invitation to hackers      
    $frameData=$_POST["frameData"];
    $tankData=$_POST["tankData"];
    $fp = fopen ("$myFileName", "w");
    if($fp)
    {  fwrite($fp,stripslashes("$frameData\n$tankData"));  // it does not make sense to write if the fopen failed, so this goes within the if
       fclose($fp);
       echo ("&error=NONE&msg=Save was successful!");
    } else {
    echo ("&error=OK&msg=Save unsuccessful!...an error occurred!");
    }

    ?>

    I have everything woring as it should and how I want it to be.. EXCEPT if you look I am using the SAME textFile hardcoded into the PHP script.

    what i want to do is us the OUTPUT form this function I wrote:

    timeStamp function:
    Code:
    function timeStamp() {
    	//Get DATE:
    myDate = new Date();
    currentDate = myDate.getDate();
    currentMonth =["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"];
    //currentYear = myDate.getYear(); // last digits of YEAR: 06
    currentYear = myDate.getFullYear();  //fulle YEAR: 2006
    currentHour = myDate.getHours();
    currentMinute = myDate.getMinutes();
    currentSecond = myDate.getSeconds();
    timeStamp = currentMonth[myDate.getMonth()]+currentDate+currentYear+currentHour+currentMinute+currentSecond;
    trace(timeStamp);
    }
    to be the NAME of the text file the PHP script writes to.

    How would I go abotu doing this? (I know 0% php really)

    so wold I just call this function form the button (at the same time I call the saveBuild unction) and then try to pass the result in the varObject I am passing to the PHP script as well??

    or is there a better method? or better practice?

    thanks


    update: I have tried:
    Code:
    var newFileName = timeStamp();
    trace(newFileName);
    but it returns "undefined". I know I am not using it properly to return the results from the function.. HELP???

  2. #2
    Product Designer keyone.it's Avatar
    Join Date
    Aug 2001
    Location
    Rome, Italy.
    Posts
    1,625
    Well, PHP is pretty straightforward.
    I don't understand why you made such a "timestamp" function, the UNIX timestamp is the simplest and versatile solution here (it's just the number of seconds from 1/1/1970). With such a date format you can easily reconstruct a date object (in flash you just pass it to the Date(); function, but it must be defined as milliseconds).

    Anyway, if you go give a look at http://www.php.net/ you will find a bunch of info on your issue:

    Look for
    FileSystem functions
    Date functions
    Strings functions

    That should be it.
    Remember to check if your hosting service runs your website in SAFE_MODE, because if it does you wont be able to do much stuff unless you run PHP scripts as CGI (through .htaccess override). That's because the owner of the executed script must be the same as the owner of the folder (CHMOD properties don't count in this case), and it's very unlikely that this is the case.


    Hope this helps,
    Ascanio.

    p.s.
    It's very, very easy to make files in PHP, but I have to suggest you use a database. Using databases is far easier than having to deal with textfiles...
    Altruism does not exist. Sustainability must be made profitable.

  3. #3
    Product Designer keyone.it's Avatar
    Join Date
    Aug 2001
    Location
    Rome, Italy.
    Posts
    1,625
    I would also use XML format for the PHP output. It takes a little time to set up a generic parser you can use for your movie, but if you design it well, you just have to build it once and use it for all
    Altruism does not exist. Sustainability must be made profitable.

  4. #4
    Product Designer keyone.it's Avatar
    Join Date
    Aug 2001
    Location
    Rome, Italy.
    Posts
    1,625
    fwrite($fp,stripslashes("$frameData\n$tankData"));

    fwrite() --> prints content to the file object;

    stripslashes() --> takes out formatting slashes based on a few rules (you can check it at php.net)
    "" (double quotation marks) are used to define strings with variables that have to be embedded: "hello $name" is equal to 'hello ' . $name;

    Code:
    \n
    stands for newline;
    Altruism does not exist. Sustainability must be made profitable.

  5. #5
    Senior Member whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,755
    Quote Originally Posted by keyone.it
    fwrite($fp,stripslashes("$frameData\n$tankData"));

    fwrite() --> prints content to the file object;

    stripslashes() --> takes out formatting slashes based on a few rules (you can check it at php.net)
    "" (double quotation marks) are used to define strings with variables that have to be embedded: "hello $name" is equal to 'hello ' . $name;

    Code:
    \n
    stands for newline;
    ?? sorry Im confused about this post? Did I miss something?

    my PHP file is fine..and is working as it should. (I believe) as all data is outputted to the text file..and in the correct format needed by my movie.

    I just cant get my timeStamp function OUTPUT to be held in a var.

    I though doing

    newVar = timeStamp(); would work..I guess not.

    THANKS

  6. #6
    Senior Member whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,755
    right..I understand the advantage to using a DB...and XML for that matter..however I am NOT there yet. (one thing at a time)...

    so to recap...your solution is to NOT use the timeStamp function I wrote?..but to transfer it over to PHP and use the date(); function there?

    is there NOT a way to just get the result of MY function above into a varName? so that I can pass it will my varObject to PHP like I am doing my other varData?

    //////////////////////////////////////////////

    or lets DUMB it up.. simple..involves no other PHP or code..but just how can I get the outcome of my timeStamp function into a varname? (like I tried above) I think I may have updated after you posted though.

    Thanks... once I do that..I'll just pass that value form the timeStamp function to my PHP script like so: sendBuild.newFileName = newFileName; // result from the timeStamp function.

  7. #7
    Product Designer keyone.it's Avatar
    Join Date
    Aug 2001
    Location
    Rome, Italy.
    Posts
    1,625
    Well yes, I would not use Flash to generate the timestamp. Also because you want unique timestamps, and only at server level you can do it (clients can set their clocks badly).
    At this point I would also add the microseconds to the UNIX timestamp, using random numbers (microseconds don't affect the date, and can make it safer from people saving at the same time).

    I see your problem is just getting the darn vars out of the PHP script.
    Well, I must admit I always used XML output.. so I don't have experience with regular vars being returned, but what physically PHP is doing is printing a textfile back.
    Maybe adding some header job would make things work better.
    You should define the file format and coding that Flash is compatible with (you find that in Flash Help pane).

    If that still doesn't work, I would really go for the XML, it will easily solve the problem and give you a better tool.

    It's really easy, you just build it as you would any text/HTML output from PHP, just add the xml declaration at the beginning, and use good tagging.
    In Flash you can parse XML pretty much like in Javascript, it's really quick.

    Altruism does not exist. Sustainability must be made profitable.

  8. #8
    Product Designer keyone.it's Avatar
    Join Date
    Aug 2001
    Location
    Rome, Italy.
    Posts
    1,625
    Oh, no, wait....now I see....
    You aren't getting the timestamp value from the timestamp function. You are not talking about the return value (error) from the PHP script.

    .... I'm sorry...

    You just need to add a "return varname;" line to the timestamp function.

    Functions don't give variables back unless you tell them to.
    You can pass any kind of variable or object within Flash (and it appears that you can also between flash and javascript, when the variable type is supported by both, but I tried and it doesn't work).

    :P
    Altruism does not exist. Sustainability must be made profitable.

  9. #9
    Senior Member whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,755
    I have read some XML books...and I understood what i read..unti lit came to schema's and crap liek that.

    OK... lets just forget abotu the PHP part ALL TOGETHER!!!

    All I want to know is...

    if you had a function in FLASH called timeStamp, and was this:
    Code:
    function timeStamp() {
    	//Get DATE:
    myDate = new Date();
    currentDate = myDate.getDate();
    currentMonth =["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"];
    //currentYear = myDate.getYear(); // last digits of YEAR: 06
    currentYear = myDate.getFullYear();  //fulle YEAR: 2006
    currentHour = myDate.getHours();
    currentMinute = myDate.getMinutes();
    currentSecond = myDate.getSeconds();
    timeStamp = currentMonth[myDate.getMonth()]+currentDate+currentYear+currentHour+currentMinute  +currentSecond;
    trace(timeStamp);
    }
    I can TRACE "timeStamp" fine if I do this:
    Code:
    timeStamp();
    trace(timeStamp);

    but how could I say assign the result of timeStamp(); function to a variable. Like so I could display it in a text field or something. forget all abotu XML, DB's and PHP for now...

    I can get MY values BACK form flash..no problem (using the send & load mthod with a varObject to handle the response)..

    I do however like your idea about server side timeStamp thouh (good idea) but that will be version 2 LOL

  10. #10
    Product Designer keyone.it's Avatar
    Join Date
    Aug 2001
    Location
    Rome, Italy.
    Posts
    1,625
    Ok, you just do something like this:
    Code:
    function varReturn(inputVar) {
        var outputVar:String = "you input: " + inputVar;
        return outputVar;
    }
    var myVar:String = varReturn("hello");
    trace(myVar); // should trace "you input hello"
    Altruism does not exist. Sustainability must be made profitable.

  11. #11
    Product Designer keyone.it's Avatar
    Join Date
    Aug 2001
    Location
    Rome, Italy.
    Posts
    1,625
    Anyway don't get afraid from big words like "Schema". That's all crap.
    You can use xml in the simplest ways:
    Code:
    <?xml version="1.0" ?>
    <result>
        <person id="48529">
            <name>Ascanio</name>
            <age>25</age>
        </person>
        <person id="83950">
            <name>Elena</name>
            <age>22</age>
        </person>
    </result>
    I think this is simple enough right? This could be the output of a search in a database of members.

    Altruism does not exist. Sustainability must be made profitable.

  12. #12
    Senior Member whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,755
    yeah I understand the setup of an XML document...very simple..however outputting to that format...and what t odo with it AFTER is somethign I hae never done before.. I have never imported any XML document...and went through the XML index of childNode or whatever they are...

  13. #13
    Senior Member whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,755
    ??? I guess Im still a little lost..LOL

    so your saying create ANOTHER function to return the result of my timeStamp function? That seems liek an un-needed step no?

    I guess directly assigning a varName to a function doesnt work huh?

    nvName = timeStamp();

    (cause when I trace varName) its undefined...


    oops..never hit submit on this one. (shodl have been before you last post)

  14. #14
    Product Designer keyone.it's Avatar
    Join Date
    Aug 2001
    Location
    Rome, Italy.
    Posts
    1,625
    No, you should do it in your function:

    Code:
    function timeStamp() {
        myDate = new Date();
        currentDate = myDate.getDate();
        currentMonth =["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"];
        currentYear = myDate.getFullYear();  //fulle YEAR: 2006
        currentHour = myDate.getHours();
        currentMinute = myDate.getMinutes();
        currentSecond = myDate.getSeconds();
        var tsout:String = currentMonth[myDate.getMonth()]+currentDate+currentYear+currentHour+currentMinute  +currentSecond;
        return tsout;
    }
    var myVar = timeStamp();
    trace(myVar); // this should trace the output of the function timeStamp();
    By the way, you must give unique names to variables, objects and functions.
    If you create a timeStamp function, you better not use a variable named timeStamp...it may give unpredictable results...

    Altruism does not exist. Sustainability must be made profitable.

  15. #15
    Senior Member whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,755
    yeah...I caugth that (naming stuff) when I was re-reading my posts..I changed it to savedTime for the VAR name as to NOT interfere.

    ok..this is what im talking about:
    Code:
    return tsout;
    this will take a function and "return" the value? (awesome) never used the return action before.

    I'll test it out. Thanks

  16. #16
    Senior Member
    Join Date
    Nov 2001
    Posts
    1,145
    if i get your question....

    the filename in the php code can be a variable instead of "build1.txt"

    (im a cold fusion guy so somebody else has to fix your code)

    send the filename from flash to php like other data

    sendBuild.fileName = myVar;

    myVar will have to be a valid filename in a string like "saveData_12131123123.txt". then php will have the variable fileName to use to name the text file or whatever

    $fileName=$_POST["fileName"];

    (again, that's just a guess at the exact syntax, i don't know php but i do know the variable will be passed out of flash to whatever file)

    was that it?
    Last edited by moot; 02-07-2006 at 11:37 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center