2 Replies Latest reply on Feb 12, 2009 8:48 AM by kmdguy

    How to rename a file upon upload?

    kmdguy Level 1
      Hi there,

      I'm successfully using a PHP script w/Flex to create file upload functionality in an app that I'm working on. Everything is working fine. My question is though, I want to be able to upload the file to my server BUT have the file's named changed before or after upload. The reason is that many image files will be uploaded into the same directory and I want every file to have a unique name on the server so no two are the same. When I choose a file name using the fileRef.browse(); method, it returns the name of the file by accessing it via fileRef.name. That works fine and everything.

      Now when I upload the file to the server using my method: fileRef.upload(new URLRequest(remoteURL)), that works fine. I just want to be able to change the name of the file so if a user chooses a file on their computer called "myphoto.jpg" it will be saved as something like "38745_myphoto.jpg".

      Is there a way to tell the fileRef.upload method to push the selected file but save it with a different name when it is on the server?

      Thanks!
        • 1. Re: How to rename a file upon upload?
          drillnaut
          Not that I know of. Generally renaming of files is squarely in the realm of backend technologies. When you upload a file in php it is saved to a tmp file with a weird/random name. After the upload is complete , you choose where to put/name it by using the move_uploaded_file() keyword. Using move_uploaded_file you can change the name of the uploaded file to anything you please. Typing in "php move_uploaded_file" should yield more information.

          Sincerely,
          JDall
          • 2. Re: How to rename a file upon upload?
            kmdguy Level 1
            Thanks JD,

            I'm able to get things to work, that is to upload fine. I guess my new question is: How can I pass a variable into my PHP script to use to assign the file name. My PHP script is this:

            <?php

            $tempFile = $_FILES['Filedata']['tmp_name'];
            $fileName = $_FILES['Filedata']['name'];
            $fileSize = $_FILES['Filedata']['size'];

            move_uploaded_file($tempFile, "./assets/" . $fileName);

            ?>

            This script works fine by saving the file. I know I can also change the variable '$fileName' to anything I want and the file will be saved as that name. I have a variable that is resident in my FLEX app which I want to use as the file name. Can I pass that variable somehow into this PHP script to be used as a substitute for '$fileName' and how would I do that?

            Thanks!