Skip to content

multiple File upload issue #679

Closed
Closed

Description

@Iamscalla

upload of multiple files returns null

below is what i tried
<input accept="image/*" multiple type="file" id="feed-editor-image-input" name="images[]"/>
$imagesFile = $this->request->getFiles('images'); var_dump($imagesFile);

Activity

  1. JakeAi commented on Aug 16, 2017

    @JakeAi
    Contributor

    @chistel In the function getFiles() on line 487 of the IncomingRequest class, if you change

    public function getFiles(): FileCollection
    {
    	if (is_null($this->files))
    	{
    		$this->files = new FileCollection();
    	}
    	return $this->files;
    }
    

    to

    public function getFiles()
    {
    	if (is_null($this->files))
    	{
    		$this->files = new FileCollection();
    	}
    	return $this->files->all();
    }
    
    

    It will work for now. I would let @lonnieezell check over this though because it is a dead end with getFiles().

  2. lonnieezell commented on Aug 16, 2017

    @lonnieezell
    Member

    A couple of thoughts for you @chistel:

    1. Is your form setup with enctype=multipart/form-data? Forgetting that is a common error, for me anyway.
    2. Did you dump the $_FILES variable to ensure there was anything actually uploaded that the server recognized?
    3. You're calling getFiles('images), however that method simply returns all files within a FileCollection class (and, yes, @JakeAi that's the way it should be. The FileCollection class provides some handy helper functions you might want to use.). To retrieve a single file, you should use the getFile() method (singular). Or use getFiles() and then you can do checks like hasFile(), etc.
    4. Finally, when trying to access a single file of an array of files like you have here, you should use dot array syntax.
  3. Iamscalla commented on Aug 16, 2017

    @Iamscalla
    ContributorAuthor

    @JakeAi That was my first solution, but it returned error say Hercules must be an instance of FileCollection except I have to remove the instance from it

  4. Iamscalla commented on Aug 16, 2017

    @Iamscalla
    ContributorAuthor

    @lonnieezell pls can you give a sample of running a multi file upload to a folder using the getFiles() method

  5. lonnieezell commented on Aug 16, 2017

    @lonnieezell
    Member

    @chistel I might be able to pull something together tonight (just getting my morning started here), but you never answered the questions - especially #1 and #2.

  6. Iamscalla commented on Aug 16, 2017

    @Iamscalla
    ContributorAuthor

    @lonnieezell Oh skip me, #1 & #2 did that, and 3 I figured that I was on the wrong path.
    Now it's returns all. but it would be better using specific field name than just returning all uploaded file. Especially if I have two different file field in a single form

  7. JakeAi commented on Aug 16, 2017

    @JakeAi
    Contributor

    @lonnieezell I couldn't even get getFiles to work without modifications. getFiles creates a new FileCollection class, but there is no construct and doesn't call any functions?, and this->files is null. If you call getFile('w/e') and then getFiles, it works.

     <form action="/Shared/Test" method="post" enctype="multipart/form-data">
            <input multiple type="file" id="feed-editor-image-input" name="images[]"/>
            <input type="submit">
        </form>
    // This doesn't work
    	print_r( [
    			$_FILES,
    			$this->request->getFiles(),
    		] );
    Array
    (
        [0] => Array
            (
                [images] => Array
                    (
                        [name] => Array
                            (
                                [0] => 170586-Subaru_Impreza_WRX-Subaru_Impreza-Subaru-JDM.png
                                [1] => 17499755_1403719142999697_1454939783_o.jpg
                            )
    
                        [type] => Array
                            (
                                [0] => 
                                [1] => image/jpeg
                            )
    
                        [tmp_name] => Array
                            (
                                [0] => 
                                [1] => D:\Wamp\tmp\php6315.tmp
                            )
    
                        [error] => Array
                            (
                                [0] => 1
                                [1] => 0
                            )
    
                        [size] => Array
                            (
                                [0] => 0
                                [1] => 118352
                            )
    
                    )
    
            )
    
        [1] => CodeIgniter\HTTP\Files\FileCollection Object
            (
                [files:protected] => 
            )
    
    )
    // This works
    	print_r( [
    			$_FILES,
    			$this->request->getFile( 'images' ),
    			$this->request->getFiles(),
    		] );
    Array
    (
        [0] => Array
            (
                [images] => Array
                    (
                        [name] => Array
                            (
                                [0] => 170586-Subaru_Impreza_WRX-Subaru_Impreza-Subaru-JDM.png
                                [1] => 17499755_1403719142999697_1454939783_o.jpg
                            )
    
                        [type] => Array
                            (
                                [0] => 
                                [1] => image/jpeg
                            )
    
                        [tmp_name] => Array
                            (
                                [0] => 
                                [1] => D:\Wamp\tmp\php6141.tmp
                            )
    
                        [error] => Array
                            (
                                [0] => 1
                                [1] => 0
                            )
    
                        [size] => Array
                            (
                                [0] => 0
                                [1] => 118352
                            )
    
                    )
    
            )
    
        [1] => 
        [2] => CodeIgniter\HTTP\Files\FileCollection Object
            (
                [files:protected] => Array
                    (
                        [images] => Array
                            (
                                [0] => CodeIgniter\HTTP\Files\UploadedFile Object
                                    (
                                        [path:protected] => 
                                        [originalName:protected] => 170586-Subaru_Impreza_WRX-Subaru_Impreza-Subaru-JDM.png
                                        [name:protected] => 170586-Subaru_Impreza_WRX-Subaru_Impreza-Subaru-JDM.png
                                        [originalMimeType:protected] => 
                                        [error:protected] => 1
                                        [hasMoved:protected] => 
                                        [size:protected] => 0
                                        [pathName:SplFileInfo:private] => 
                                        [fileName:SplFileInfo:private] => 
                                    )
    
                                [1] => CodeIgniter\HTTP\Files\UploadedFile Object
                                    (
                                        [path:protected] => D:\Wamp\tmp\php6141.tmp
                                        [originalName:protected] => 17499755_1403719142999697_1454939783_o.jpg
                                        [name:protected] => 17499755_1403719142999697_1454939783_o.jpg
                                        [originalMimeType:protected] => image/jpeg
                                        [error:protected] => 0
                                        [hasMoved:protected] => 
                                        [size:protected] => 118352
                                        [pathName:SplFileInfo:private] => D:\Wamp\tmp\php6141.tmp
                                        [fileName:SplFileInfo:private] => php6141.tmp
                                    )
    
                            )
    
                    )
    
            )
    
    )
  8. Iamscalla commented on Aug 16, 2017

    @Iamscalla
    ContributorAuthor

    @JakeAi I figured it out in to ways, either add the all method of FileCollection into a constructor or go with the previous method you suggested but removing the fileCollection instance from getFiles() method

  9. Iamscalla commented on Aug 16, 2017

    @Iamscalla
    ContributorAuthor

    @lonnieezell Never mind about my last response and request, I figured it out. I would make a doc regarding it.

  10. JakeAi commented on Aug 16, 2017

    @JakeAi
    Contributor

    @chistel All you need to do is return all() and remove the return type after getFiles(). You don't need to necessarily remove the instance.

    public function getFiles(): FileCollection // <-----
    {
    	if (is_null($this->files))
    	{
    		$this->files = new FileCollection();
    	}
    	return $this->files; // <-----
    }

    to

    public function getFiles() // <-----
    {
    	if (is_null($this->files))
    	{
    		$this->files = new FileCollection();
    	}
    	return $this->files->all(); // <-----
    }
  11. Iamscalla commented on Aug 16, 2017

    @Iamscalla
    ContributorAuthor

    @JakeAi exactly what I did

  12. JakeAi commented on Aug 16, 2017

    @JakeAi
    Contributor

    @chistel Oh, right on. I'm still confused though. Is @lonnieezell right? Or is something broken? Like, I understand about the helper functions, but I couldn't get it to work at all. populateFiles doesn't get called at all through getFiles(). Are you supposed to $files=getFiles() then $files->getFile()? I feel like it's an inconvenience. I'd like to get files and then foreach the files and call the helpers on those iterations?

  13. Iamscalla commented on Aug 16, 2017

    @Iamscalla
    ContributorAuthor

    @JakeAi he is right, I can't find anything broken there.. when I make the doc, I would clarify it

  14. JakeAi commented on Aug 16, 2017

    @JakeAi
    Contributor

    @chistel Can you post your sample code?

  15. Iamscalla commented on Aug 16, 2017

    @Iamscalla
    ContributorAuthor

    @JakeAi am on mobile now, but here is what I have,

    If($imagefile = $this->request->getFiles())
    {  
       foreach($imagefile['images'] as $Im)
       {
          if ($im->isValid() && ! $im->hasMoved())
          {
               $newName = $img->getRandomName();
               $img->move(WRITEPATH.'uploads', $newName);
          }
       }
    }
    

    where the images is the form field name
    <input type="file" name="images[]" multiple />

  16. JakeAi commented on Aug 16, 2017

    @JakeAi
    Contributor

    @chistel did you add the all() to the IncomingRequest then? Because your code doesn't work out of the box.

  17. Iamscalla commented on Aug 16, 2017

    @Iamscalla
    ContributorAuthor

    @JakeAi yes I added it

  18. JakeAi commented on Aug 16, 2017

    @JakeAi
    Contributor

    @chistel Ahhh, that's what I meant about being broken. So something does need changed.

  19. Iamscalla commented on Aug 16, 2017

    @Iamscalla
    ContributorAuthor

    @JakeAi 🙄🙄🙄 yes just that

  20. lonnieezell commented on Aug 17, 2017

    @lonnieezell
    Member

    @chistel 's changes have been merged in, so closing this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions