&引用;标准;php html表单到laravel结构
我有一个页面的示例代码,它运行良好,我想将其集成到我的laravel项目中。但我不工作。在浏览表单后,它没有显示任何内容。这是页面中的代码: HTML 控制器:&引用;标准;php html表单到laravel结构,php,html,laravel,Php,Html,Laravel,我有一个页面的示例代码,它运行良好,我想将其集成到我的laravel项目中。但我不工作。在浏览表单后,它没有显示任何内容。这是页面中的代码: HTML 控制器: class OcrController extends Controller { public function ocr(Request $request) { $image = $request->input('image'); $pic = null; $te
class OcrController extends Controller
{
public function ocr(Request $request)
{
$image = $request->input('image');
$pic = null;
$text = null;
if(isset($_FILES[$image])){
$file_name = $_FILES[$image]['name'];
$file_tmp =$_FILES[$image]['tmp_name'];
move_uploaded_file($file_tmp,"images/".$file_name);
$pic = '<img src="images/'.$file_name.'" style="width:100%">';
shell_exec('"C:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe" "C:\\CV-Test\\blog\\public\\images\\'.$file_name.'" out');
$myfile = fopen("out.txt", "r") or die("Unable to open file!");
$text= fread($myfile,filesize("out.txt"));
fclose($myfile);
}
return view('CV_test.OCR', compact('pic', 'text'));
}
}
类OcrController扩展控制器
{
公共功能ocr(请求$请求)
{
$image=$request->input('image');
$pic=null;
$text=null;
如果(isset($\u文件[$image])){
$file\u name=$\u文件[$image]['name'];
$file\u tmp=$\u FILES[$image]['tmp\u name'];
移动上传的文件($file\u tmp,“images/”)$file\u name);
$pic='';
shell_exec(“'C:\\Program Files(x86)\\Tesseract OCR\\Tesseract.exe”“C:\\CV Test\\blog\\public\\images\\'。$file_name.'out');
$myfile=fopen(“out.txt”,“r”)或die(“无法打开文件!”);
$text=fread($myfile,filesize(“out.txt”));
fclose($myfile);
}
返回视图('CV_test.OCR',compact('pic','text');
}
}
当我执行它时,表单被发布,没有错误,但它只是没有显示任何内容。
它应该先显示图片,然后是ocr算法识别的下面的文本。
我怀疑varibale$pic和$text的初始化有问题,但若我不初始化它,我会得到一个错误。我不知道如何处理那个问题
请帮助我:(
赞赏。他使用不同的方式获取/移动图像,可能会有所帮助
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function imageUploadPost(Request $request)
{
$request->validate([
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
$imageName = time().'.'.$request->image->extension();
$request->image->move(public_path('images'), $imageName);
return back()
->with('success','You have successfully upload image.')
->with('image',$imageName);
}
刀片文件:
<form action="{{ route('image.upload.post') }}" method="POST" enctype="multipart/form-data">
@csrf
<div class="row">
<div class="col-md-6">
<input type="file" name="image" class="form-control">
</div>
<div class="col-md-6">
<button type="submit" class="btn btn-success">Upload</button>
</div>
</div>
</form>
@csrf
上载
(对于其他用户,我不能发表评论,因为我没有50%的声誉)我建议您阅读如何在Laravel中上传文件,而不是使用
$\u FILES
数组并手动处理。Laravel使它变得非常简单,而且会更容易。然后您应该尝试一件事。从文件上传开始,并开始工作。然后您可以继续,逐个添加其他功能。您好,谢谢这句话的意思是说,$\u FILES
是它不起作用的原因吗?这本身不是问题,但你在几个地方使用它的方式是错误的。这就是为什么我建议你使用“laravel”当时只有一件事。你现在似乎混淆了很多概念。是的,我可以确认我是混淆的。我实际上是拉雷维尔的新手,还有很多事情我不清楚。但是我已经在检查你说的。谢谢老兄。
Route::get('/OCR', [OcrController::class, 'ocr'])->name('ocr');
Route::post('/OCR', [OcrController::class, 'ocr'])->name('ocr');
class OcrController extends Controller
{
public function ocr(Request $request)
{
$image = $request->input('image');
$pic = null;
$text = null;
if(isset($_FILES[$image])){
$file_name = $_FILES[$image]['name'];
$file_tmp =$_FILES[$image]['tmp_name'];
move_uploaded_file($file_tmp,"images/".$file_name);
$pic = '<img src="images/'.$file_name.'" style="width:100%">';
shell_exec('"C:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe" "C:\\CV-Test\\blog\\public\\images\\'.$file_name.'" out');
$myfile = fopen("out.txt", "r") or die("Unable to open file!");
$text= fread($myfile,filesize("out.txt"));
fclose($myfile);
}
return view('CV_test.OCR', compact('pic', 'text'));
}
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function imageUploadPost(Request $request)
{
$request->validate([
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
$imageName = time().'.'.$request->image->extension();
$request->image->move(public_path('images'), $imageName);
return back()
->with('success','You have successfully upload image.')
->with('image',$imageName);
}
<form action="{{ route('image.upload.post') }}" method="POST" enctype="multipart/form-data">
@csrf
<div class="row">
<div class="col-md-6">
<input type="file" name="image" class="form-control">
</div>
<div class="col-md-6">
<button type="submit" class="btn btn-success">Upload</button>
</div>
</div>
</form>