我需要在 laravel 4 中上传多个文件 我在控制器中编写了以下代码
public function post_files()
{
$allowedExts = array("gif", "jpeg", "jpg", "png","txt","pdf","doc","rtf");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
$filename= $temp[0];
$destinationPath = 'upload/'.$filename.'.'.$extension;
if(in_array($extension, $allowedExts)&&($_FILES["file"]["size"] < 20000000))
{
if($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
$uploadSuccess=move_uploaded_file($_FILES["file"]["tmp_name"],$destinationPath);
if( $uploadSuccess )
{
$document_details=Response::json(Author::insert_document_details_Call($filename,$destinationPath));
return $document_details; // or do a redirect with some message that file was uploaded
}
else
{
return Response::json('error', 400);
}
}
}
}
else
{
return "Invalid file";
}
}
}
我可以通过此代码上传单个文件,但无法上传多个文件。请在这个问题上帮助我..提前谢谢你..
public function post_abc()
{
// $file = Input::file('file'); // your file upload input field in the form should be named 'file'
$allowedExts = array("gif", "jpeg", "jpg", "png","txt","pdf","doc","rtf","docx","xls","xlsx");
foreach($_FILES['file'] as $key => $abc)
{
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
$filename= $temp[0];
$destinationPath = 'abc/'.$filename.'.'.$extension;
if(in_array($extension, $allowedExts)&&($_FILES["file"]["size"] < 20000000))
{
if($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
if (file_exists($destinationPath))
{
echo $filename." already exists. ";
}
else
{
$uploadSuccess=move_uploaded_file($_FILES["file"]["tmp_name"],$destinationPath);
if( $uploadSuccess )
{
return "hello";
}
else
{
return Response::json('error', 400);
}
}
}
}
}
在谷歌浏览器上使用邮递员休息客户端我已将密钥作为“文件”传递,值作为“3 个选定文件”传递,输入类型为文件