We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

getUploadedFiles use with multiples inputs

I have multiples inputs os images one theys receiving multiples images and other receive only one, and both are save in diferents directorys.

How can I tell them?

Example of what I using the global variable $ _FILES

$img1 = $_FILES['img1']['tmp_name']

$img2 = $_FILES['img2']['tmp_name']

How i make this using getUploadedFiles???



33.8k

getUploadedFiles(..) returns an array of Phalcon\Http\Request\File objects, so you can use his functions to know everything about them https://docs.phalcon.io/es/latest/api/Phalcon_Http_Request_File.html



33.8k

getUploadedFiles(..) returns an array of Phalcon\Http\Request\File objects, so you can use his functions to know everything about them https://docs.phalcon.io/es/latest/api/Phalcon_Http_Request_File.html

edited Feb '15

getUploadedFiles(..) returns an array of Phalcon\Http\Request\File objects, so you can use his functions to know everything about them https://docs.phalcon.io/es/latest/api/Phalcon_Http_Request_File.html

I know, but i need get the files in differents vars, something like this

//HTML
<input type="file" name="profile" id="profile" />
<input type="file" name="albuns[]" multiple/>

//Controller
$profile_img  = getUploadedFiles('profile');
$albuns_img = getUploadedFiles('albuns');

getUploadedFiles receiving all uploaded files in one array like $_FILES but how differentiate the received inputs? This is my doubt.



33.8k
Accepted
answer

Ahhhhhh know I get it. This has been discussed in other post (https://forum.phalcon.io/discussion/4418/how-to-get-an-uploaded-file-directly).

edited Feb '15

Ahhhhhh know I get it. This has been discussed in other post (https://forum.phalcon.io/discussion/4418/how-to-get-an-uploaded-file-directly).

Unfortunately does not allow Phalcon What do I want to do as you showed, I could do otherwise, but the possibility of receiving the data according to your name or id should be implemented in future versions of the framework for it makes us lose a lot of time with something that should be simple.



2.1k
edited Feb '15

i believe u can extend http request class for that. something along the line of.

of course this is zep, so yeah 2.0 =d

/**
     * Gets attached files as Phalcon\Http\Request\File instances
     *
     * @param boolean notErrored
     * @return Phalcon\Http\Request\File[]
     */
    public function getUploadFiles(string index, boolean notErrored = false) -> <File[]> | null
    {
        var superFiles, prefix, input, smoothInput, files, file, dataFile;
        if fetch superFiles, _FILES[index] {

        let files = [];

        if (count(superFiles) > 0) {

            for prefix, input in superFiles {
                if (typeof input["name"] == "array") {
                    let smoothInput = this->smoothFiles(input["name"], input["type"], input["tmp_name"], input["size"], input["error"], prefix);

                    for file in smoothInput {
                        if (notErrored == false || file["error"] == UPLOAD_ERR_OK) {
                            let dataFile = [
                                "name": file["name"],
                                "type": file["type"],
                                "tmp_name": file["tmp_name"],
                                "size": file["size"],
                                "error": file["error"]
                            ];

                            let files[] = new File(dataFile, file["key"]);
                        }
                    }
                } else {
                    if (notErrored == false || input["error"] == UPLOAD_ERR_OK) {
                        let files[] = new File(input, prefix);
                    }
                }
            }
        }

        return files;
        }
        return null;
    }

i believe u can extend http request class for that. something along the line of.

of course this is zep, so yeah 2.0 =d

/**
   * Gets attached files as Phalcon\Http\Request\File instances
   *
   * @param boolean notErrored
   * @return Phalcon\Http\Request\File[]
   */
  public function getUploadFiles(string index, boolean notErrored = false) -> <File[]> | null
  {
      var superFiles, prefix, input, smoothInput, files, file, dataFile;
      if fetch superFiles, _FILES[index] {

      let files = [];

      if (count(superFiles) > 0) {

          for prefix, input in superFiles {
              if (typeof input["name"] == "array") {
                  let smoothInput = this->smoothFiles(input["name"], input["type"], input["tmp_name"], input["size"], input["error"], prefix);

                  for file in smoothInput {
                      if (notErrored == false || file["error"] == UPLOAD_ERR_OK) {
                          let dataFile = [
                              "name": file["name"],
                              "type": file["type"],
                              "tmp_name": file["tmp_name"],
                              "size": file["size"],
                              "error": file["error"]
                          ];

                          let files[] = new File(dataFile, file["key"]);
                      }
                  }
              } else {
                  if (notErrored == false || input["error"] == UPLOAD_ERR_OK) {
                      let files[] = new File(input, prefix);
                  }
              }
          }
      }

      return files;
      }
      return null;
  }

where be located this class?



2.1k

anywhere, convert the zep to php and u can use it in a library folder


if($this->request->hasFiles(true))
{
    foreach($this->request->getUploadedFiles(true) as $upload)
    {
        //check form input name attribute getKey() 
        //<input type="file" name="img1" ....>
        if($upload->getKey() === 'img1')
        {
            //...
            if($upload->moveTo('img1/'.$upload->getName()))
            {
                //...
            }
        }

        //check form input name attribute getKey() 
        //<input type="file" name="img2" ....>
        if($upload->getKey() === 'img2')
        {
            //...
            if($upload->moveTo('img2/'.$upload->getName()))
            {
                //...
            }
        }
    }
}