Php 为什么下载PDF的超链接不起作用?(无文件)
我开发了一个用于创建文章的系统,除了标题和内容之外,每个帖子都有可能上载PDF。文件名存储在数据库中,并写在下载文件所需的超链接上。现在的问题是,当我想下载文件时,浏览器中显示了一个错误。 有人知道什么是问题吗 数据库 PDF文件的位置 结果 邮务主任Php 为什么下载PDF的超链接不起作用?(无文件),php,laravel,Php,Laravel,我开发了一个用于创建文章的系统,除了标题和内容之外,每个帖子都有可能上载PDF。文件名存储在数据库中,并写在下载文件所需的超链接上。现在的问题是,当我想下载文件时,浏览器中显示了一个错误。 有人知道什么是问题吗 数据库 PDF文件的位置 结果 邮务主任 <?php namespace App\Http\Controllers; use Illuminate\Http\Request; //including post model to controller use App\Pos
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
//including post model to controller
use App\Post;
//if we want to use sql syntax for queries
use DB;
class PostsController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//list articles by desc row
$posts= Post::orderby('created_at', 'desc')->paginate(3);
return view('posts.index')-> with('posts', $posts);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('posts.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this -> validate($request,[
'title' => 'required',
'content' => 'required'
]);
// Handle File Upload
if($request->hasFile('image')){
// Get filename with the extension
$filenameWithExt = $request->file('image')->getClientOriginalName();
// Get just filename
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
// Get just ext
$extension = $request->file('image')->getClientOriginalExtension();
// Filename to store
$fileNameToStore= $filename.'_'.time().'.'.$extension;
// Upload Image
$path = $request->file('image')->storeAs('public/upload', $fileNameToStore);
} else {
$fileNameToStore = 'noimage.jpg';
}
//create new post
$post= new Post;
$post -> title = $request -> input('title');
$post -> content = $request -> input('content');
$post->file_name = $fileNameToStore;
$post -> save();
return redirect('/posts') ->with('success', 'Post Created');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//list post while we enter through link
$post= Post::find($id);
return view('posts.show')-> with('post', $post);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$post= Post::find($id);
return view('posts.edit')-> with('post', $post);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$this -> validate($request,[
'title' => 'required',
'content' => 'required'
]);
//create new post
$post= Post::find($id);
$post -> title = $request -> input('title');
$post -> content = $request -> input('content');
$post -> save();
return redirect('/posts') ->with('success', 'Post Updated');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$post= Post::find($id);
$post -> delete();
return redirect('/posts') ->with('success', 'Post Removed');
}
}
存储/app/public
对用户不可见
您需要移动到public/…
和/或使用以下说明创建符号链接:
php artisan存储:链接
然后你替换
<a href="./storage/app/public/upload/{{$post->file_name}}" download>
与
谢谢你的回答,我做了,但它仍然显示了相同的错误。你可以使用windows资源管理器手动查找/path/to/app/public/…
中的文件吗?我编写了,但没有使用windows资源管理器。转到项目的公用文件夹。你有什么?那么。您可以在那里看到存储
文件夹。将{asset('upload/'.$post->file_name)}
替换为{asset('storage/upload/'.$post->file_name)}
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->mediumText('content');
$table->string('file_name')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
<a href="./storage/app/public/upload/{{$post->file_name}}" download>
<a href="{{asset('storage/upload/'.$post->file_name)}}" download>