Welcome, Guest
  • Author Topic: Uploading Jpg, changing name, store in DB, sending new name back to flash?  (Read 1841 times)

    Adam

    • Jr. Programmer
    • **
    • Posts: 96
      • View Profile
      • Email
    I've gone through the upload image tutorial and it's been very helpful, but I am stuck on a modification of my own.

    Once my user uploads an image, I want to modify the jpg and I change it's name. I am also storing info about this image, so I created a record in a mysql table. My PHP upload script renames the jpg file to the auto increment key number and stores additional info in the record.

    In Flash, I've used a listener object when I initiate the upload (just as the tutorial describes). I would like to send the new name of the jpg file created in the PHP upload script back down to flash and then download the jpg under the new file name.

    In the PHP upload script file, I suppose I can use something like:
    echo "filename=$newfilename";

    But how do I tell the listener object to return this variable back to Flash? Or is this done some other way? Please help?

    Thanks, Adam

    gaby

    • Mods
    • Seasoned Programmer
    • *****
    • Posts: 241
    • To code or not to code
      • View Profile
    hi, i had the same problem and what i did is better than chaning name with the php file used for the upload, i defined the new name in the movie and transmited it when sending the file
    hope it will suits your needs
    The courage to begin is the main step to achievement

    Adam

    • Jr. Programmer
    • **
    • Posts: 96
      • View Profile
      • Email
    An interesting idea, but how did you know what to name the file? How did you get the auto-increment record number before you uploaded the jpg?

    Or did you use some other system to create a unique name for each image?

    BurtonRider1983

    • Global Moderator
    • Systems Administrator
    • *****
    • Posts: 864
    • Rider-4-Life
      • AOL Instant Messenger - BurtonRider1983
      • View Profile
      • Flash Developer's Corner (being built)
    The name is passed back to flash so you don't really have to do anything special.
    Just make your query to insert a new image into your database and give it a dummy file name (a basename with a random number appended to it).  Now you can get recover the unique key for the next image by querying the database for the record that has the random file name you generated above.  Next copy the temp_file in php to the desired location with the desired name.  Update your image file name for the newly added record and php will return an object to flash.  You can access the file's new name through the onComplete listener for your FileReference/FileReferenceList.....


    PHP
    Code: [Select]
        $originalFile = $_FILES['Filedata']['name'];
        $fileArray = explode('.', $originalFile);
        $fileExtension = $fileArray[sizeof($fileArray) - 1];
        $tmp_name = $_FILES['Filedata']['tmp_name'];
        $dummyName = "IM$uid.$fileExtension";

        // Insert record into database with dummname in place of real file name.
        $query = "INSERT INTO tbl_images VALUES('', '" . $dummyName . "')";
        $result = mysql_query($query);

        // Query to get the uniqueID of the record you just added
        $query = "SELECT unqueID FROM tbl_images WHERE imageName='" . $dummyName . "' LIMIT 1";
        $result = mysql_query($query);

        $row = mysql_fetch_assoc($result);
        $UID = $row['uniqueID'];
        $newName = "image" . $UID;

        move_uploaded_file($tmp_name, $MAINDIR . "images/$newName");

    AS2.0
    Code: [Select]
    import flash.net.FileReference;
    import mx.utils.Delegate;

    var file:FileReference = new FileReference();
    var myListener:Object = {};
    myListener.onSelect = Delegate.create(this, onSelect);
    myListener.onComplete = Delegate.create(this, onComplete);

    file.addListener(myListener);

        private function onSelect(file:FileReference): Void {
            file.upload(UPLOAD_SCRIPT);
        }

        private function onComplete(file:FileReference):Void {
            trace(file.name);
        }
    « Last Edit: 06/22/07, 17:40 by BurtonRider1983 »

    Adam

    • Jr. Programmer
    • **
    • Posts: 96
      • View Profile
      • Email
    BurtonRider,

    Neat.

    It's that "mx.util.delegate" part that's new to me. I haven't seen that before. I clearly need to learn more about Listeners.


    Gaby, Burton, thanks for the help!
    Adam

    Adam

    • Jr. Programmer
    • **
    • Posts: 96
      • View Profile
      • Email
    Burton,

    Thanks for the code but there is something I still don't understand here. You say...

    "You can access the file's new name through the onComplete listener for your FileReference/FileReferenceList..... "

    There are several things that confuse me about this. First, the OnComplete will fire immediately after the upload is finished. If there is any lag on the server, the new file name may not be created in time. The download request in flash may trigger before the new file exists on the server.
    Secondly, even if there was no lag on the server at all, I still don't see how the OnComplete listener will capture the new file name created up on the server?

    Can you explain how OnComplete listener in Flash will get the new file name created in the PHP script?

    Thanks,
    Adam


    Andresss

    • Moderator
    • Systems Administrator
    • *****
    • Posts: 738
    • check me out at www.asb-labs.com/blog
      • View Profile
      • asb-labs
      • Email
    hey hi Adam, all those listeners are header based so for example when you enter a page and is not there it generates a 404 error, flash works the same way, if the server say redhat couldnpt handle the connection properly then a header of an error is sent back to flash....

    cheers!!!
    halemos de flash en espaņol!....wondering about crazy flash experiments?

    Adam

    • Jr. Programmer
    • **
    • Posts: 96
      • View Profile
      • Email
    Still stuck...

    I can't get the onComplete event of the filereference object to pick up the NEW file name created in the PHP script. I keep getting the original file name back.

    The PHP code works fine.

    The file is uploaded and assigned a new name, but I can't find a way to tell flash about the change.

    Does anyone have any advise?

    gaby

    • Mods
    • Seasoned Programmer
    • *****
    • Posts: 241
    • To code or not to code
      • View Profile
    so on the upload page, first get the record from the db about next auto increment value with loadvars for example, then send this value while uploading so your php page can update your db
    The courage to begin is the main step to achievement