如何使用php laravel上传图片到服务器?
How to upload image to server using php laravel?
我一直在尝试写一个api来上传图片到服务器,但都是徒劳的。
我正在使用 laravel 框架。并尝试 this example 但它不起作用。
此外,在测试来自 POSTMAN 的 api 时,我在 Headers.And 中传递了 mutlipart/form-data,在 Body 选项卡中选择了 form-data,添加了键 =图片,将其文本更改为文件并在我测试 api 时添加了一个 image.But ,不知道为什么,但图片请求是空的。
也许我在 POSTMAN 中传递了错误或者我的代码中可能有问题,请帮助我。
这是我的 api 代码
public function upload(Request $request){
if ($request->hasFile('image')) {
$image = $request->file('image');
$name = md5(time().uniqid()).".png";
$destinationPath = base_path() . '/public/uploads/images/' . $name;
move_uploaded_file($name, $destinationPath);
return response()->json(['title'=>"image is uploaded"]);
}
}
还有我的控制器代码:
Route::post('uploadImage','TestController@upload');
邮递员的屏幕截图 request.Please 告诉我如果我在 header 或 body 中传递错误。
此外,控制台显示此错误 Missing boundary in multipart/form-data POST data in Unknown on line 0
您可以使用核心 PHP 代码上传文件。
在我的 laravel 项目中,我使用以下代码上传文件。
if(isset($_FILES["image"]["type"]))
{
$FILES = $_FILES["image"];
$upload_dir = storage_path('app/public/document/');
// create folder if not exists
if (!file_exists($upload_dir)) {
mkdir($upload_dir, 0777, true);
}
//Send error
if ($FILES['error'])
{
return response()->json(['error'=>'Invalid file']);
}
//Change file name
$target_file = md5(time().uniqid());
$imageFileType = pathinfo($FILES["name"],PATHINFO_EXTENSION);
$target_file = $upload_dir.$target_file.'.'.$imageFileType;
//Upload file
if (move_uploaded_file($FILES["tmp_name"], $target_file))
{
return response()->json(['success' => 'File uploading successful']);
}
else
{
return response()->json(['error'=>'Invalid file']);
}
}else{
return response()->json(['error'=>'Invalid file']);
}
下面是我在函数开头打印$_FILES时的截图
我一直在尝试写一个api来上传图片到服务器,但都是徒劳的。 我正在使用 laravel 框架。并尝试 this example 但它不起作用。
此外,在测试来自 POSTMAN 的 api 时,我在 Headers.And 中传递了 mutlipart/form-data,在 Body 选项卡中选择了 form-data,添加了键 =图片,将其文本更改为文件并在我测试 api 时添加了一个 image.But ,不知道为什么,但图片请求是空的。
也许我在 POSTMAN 中传递了错误或者我的代码中可能有问题,请帮助我。
这是我的 api 代码
public function upload(Request $request){
if ($request->hasFile('image')) {
$image = $request->file('image');
$name = md5(time().uniqid()).".png";
$destinationPath = base_path() . '/public/uploads/images/' . $name;
move_uploaded_file($name, $destinationPath);
return response()->json(['title'=>"image is uploaded"]);
}
}
还有我的控制器代码:
Route::post('uploadImage','TestController@upload');
邮递员的屏幕截图 request.Please 告诉我如果我在 header 或 body 中传递错误。
此外,控制台显示此错误 Missing boundary in multipart/form-data POST data in Unknown on line 0
您可以使用核心 PHP 代码上传文件。 在我的 laravel 项目中,我使用以下代码上传文件。
if(isset($_FILES["image"]["type"]))
{
$FILES = $_FILES["image"];
$upload_dir = storage_path('app/public/document/');
// create folder if not exists
if (!file_exists($upload_dir)) {
mkdir($upload_dir, 0777, true);
}
//Send error
if ($FILES['error'])
{
return response()->json(['error'=>'Invalid file']);
}
//Change file name
$target_file = md5(time().uniqid());
$imageFileType = pathinfo($FILES["name"],PATHINFO_EXTENSION);
$target_file = $upload_dir.$target_file.'.'.$imageFileType;
//Upload file
if (move_uploaded_file($FILES["tmp_name"], $target_file))
{
return response()->json(['success' => 'File uploading successful']);
}
else
{
return response()->json(['error'=>'Invalid file']);
}
}else{
return response()->json(['error'=>'Invalid file']);
}
下面是我在函数开头打印$_FILES时的截图