lundi 2 novembre 2015

Accessing file attributes with Symfony in Laravel 5.1

So I've figured out how to iterate through files uploaded to my API that is built with Laravel 5.1 - Processing multiple files in Laravel 5.1 (as an API)

Now that I have that completed, I'm still not able to access the attributes of the files with Symfony as I iterate through them.

HTML page (for testing):

<!DOCTYPE>
<html>
<body>
    <form method="post" enctype="multipart/form-data" action="http://ift.tt/1k5V2QT">
        <input type="file" name="files[]" multiple>
        <input type="submit" value="Upload">
    </form>
</body>
</html>

Controller:

public function files(Request $request)
{
    foreach($request->files as $file)
    {
        return var_dump($file->getClientOriginalName());
    }
}

Error: Call to a member function getClientOriginalName() on array

If I just just var_dump the files with this controller method:

public function files(Request $request)
{
    foreach($request->files as $file)
    {
        return var_dump($file);
    }
}

... that returns:

array (size=3)
0 => 
object(Symfony\Component\HttpFoundation\File\UploadedFile)[84]
  private 'test' => boolean false
  private 'originalName' => string 'Screen Shot 2015-10-23 at 10.07.23 AM.png' (length=41)
  private 'mimeType' => string 'image/png' (length=9)
  private 'size' => int 270504
  private 'error' => int 0
  private 'pathName' (SplFileInfo) => string '/tmp/phpzekUeL' (length=14)
  private 'fileName' (SplFileInfo) => string 'phpzekUeL' (length=9)
1 => 
object(Symfony\Component\HttpFoundation\File\UploadedFile)[85]
  private 'test' => boolean false
  private 'originalName' => string 'Screen Shot 2015-10-26 at 7.28.59 PM.png' (length=40)
  private 'mimeType' => string 'image/png' (length=9)
  private 'size' => int 13687
  private 'error' => int 0
  private 'pathName' (SplFileInfo) => string '/tmp/phprYW7MZ' (length=14)
  private 'fileName' (SplFileInfo) => string 'phprYW7MZ' (length=9)
2 => 
object(Symfony\Component\HttpFoundation\File\UploadedFile)[86]
  private 'test' => boolean false
  private 'originalName' => string 'Screen Shot 2015-10-27 at 2.50.58 PM.png' (length=40)
  private 'mimeType' => string 'image/png' (length=9)
  private 'size' => int 786350
  private 'error' => int 0
  private 'pathName' (SplFileInfo) => string '/tmp/phpLq3lle' (length=14)
  private 'fileName' (SplFileInfo) => string 'phpLq3lle' (length=9)

Why am I returning this Symfony error? How can I access the file attributes in the foreach loop with getClientOriginalName() and other functions outlined in http://ift.tt/1k5V2QW?

Thanks in advance for your help!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire