2015-11-01 50 views
2

因此,這似乎是一個非常基本的東西,但我不能找到大量的文檔資料在網上什麼回事....在Laravel 5.1處理多個文件(作爲API)

我想使用Laravel 5.1運行一個文件列表,我只能返回/處理/查看第一個文件。我使用的郵差將請求發送到API(所以我知道multiplePOST請求啓用),然後迭代通過幾種不同的方式:

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

連:

public function files() 
{ 
    foreach($_FILES['files'] as $file) 
    { 
     var_dump($file); 
    } 
} 

我總是回(或對象的形式,如果使用$request->files法):

string 'Screen%20Shot%202015-10-23%20at%2010.07.23%20AM.png' (length=51) 
string 'image/png' (length=9) 
string '/tmp/phpZw1ALu' (length=14) 
int 0 
int 13687 

這究竟是爲什麼?我能做些什麼來查看Laravel 5.1控制器中的多個文件?

回答

0

所以我做了一個全新的頁面,下面的代碼調用files.php和它現在返回所有三個文件(或包含很多我上傳):

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

的錯誤是,我是曾在POST請求郵差只接受名爲files而不是files[]的參數。一旦改變了,在我的Laravel控制器代碼:

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

現在返回:

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/php1M5ZJl' (length=14) 
    private 'fileName' (SplFileInfo) => string 'php1M5ZJl' (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/phpE22ubf' (length=14) 
    private 'fileName' (SplFileInfo) => string 'phpE22ubf' (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/phph8v0C8' (length=14) 
    private 'fileName' (SplFileInfo) => string 'phph8v0C8' (length=9) 

希望幫助別人!