Php Can';t将图像上载到laravel中的公共目录
我正在尝试将图像上传到Laravel应用程序中,然后需要公开显示。但是,我似乎只能上传到应用程序根目录中的文件夹。任何上载到公用目录的尝试都会引发以下错误:Php Can';t将图像上载到laravel中的公共目录,php,laravel,laravel-4,image-uploading,Php,Laravel,Laravel 4,Image Uploading,我正在尝试将图像上传到Laravel应用程序中,然后需要公开显示。但是,我似乎只能上传到应用程序根目录中的文件夹。任何上载到公用目录的尝试都会引发以下错误: protected function getTargetFile($directory, $name = null) { if (!is_dir($directory)) { if (false === @mkdir($directory, 0777, true)) { throw new F
protected function getTargetFile($directory, $name = null)
{
if (!is_dir($directory)) {
if (false === @mkdir($directory, 0777, true)) {
throw new FileException(sprintf('Unable to create the "%s" directory', $directory));
我假设这意味着Laravel将阻止我上传到任何可公开访问的文件。实际上,我很喜欢这样,但是,我如何链接到我尝试过的公共目录之外的图像../my way out the public folder,但是适当地,这不起作用
或者,任何可以让我直接上传到公共文件夹的东西也会很棒
如果有帮助,下面是我的控制器方法,它将文件放入公用文件夹:
$thumbnail_file = Input::file('thumbnail');
$destinationPath = '/uploads/'. str_random(8);
$thumbnail_filename = $thumbnail_file->getClientOriginalName();
$uploadSuccess = Input::file('thumbnail_url')->move($destinationPath, $thumbnail_filename);
您的目标路径应为:
$destinationPath = public_path(sprintf("\\uploads\\%s\\", str_random(8)));
将上载到
/public/assets/uploads
文件夹,可能会帮助您在创建名为uploads in your public文件夹和另一个名为photos in uploads文件夹后,尝试在控制器中使用此文件夹
$data = Input::all();
$rules = array(
'photo' => 'between:1,5000|upload:image/gif,image/jpg,image/png,image/jpeg',
);
$messages = array(
'upload' => 'The :attribute must be one of the following types .jpg .gif .png .jpeg',
'between' => 'The :attribute file size must be 1kb - 5mb'
);
$validator = Validator::make($data, $rules, $messages);
if($validator->passes())
{
$uploads = public_path() . '/uploads/photos';
$photo = Input::file('photo');
$photodestinationPath = $uploads;
$photoname = null;
if($photo != null)
{
$photoname = $photo->getClientOriginalName();
Input::file('photo')->move($photodestinationPath, $photoname);
}
$thumbnail = new Model_name();
$thumbnail ->column_name_in_table = $photoname;
然后把这个放在你的路线上
Validator::extend('upload', function($attribute,$value, $parameters){
$count = 0;
for($i = 0; $i < count($parameters); $i++)
{
if($value->getMimeType() == $parameters[$i])
{
$count++;
}
}
if($count !=0)
{
return true;
}
return false;});
验证程序::扩展('upload',函数($attribute,$value,$parameters){
$count=0;
对于($i=0;$i在ImageProductController.php(控制器)文件中插入以下代码:
public function store(Request $request, $id)
{
if ($request->hasFile('image')){
$rules = [
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg'
];
$this->validate($request, $rules);
// Save file in directory
$file = $request->file('image');
$path = public_path() . '/images';
$fileName = md5(rand(0,9999)).'.'.$file->getClientOriginalExtension();
$file->move($path, $fileName);
$imageProduct = new ImageProduct();
$imageProduct->image = $fileName;
}
<form enctype="multipart/form-data" method="post" action=" " >
{{ csrf_field() }}
<label for="image" class="btn btn-primary">Inserir Imagem</label>
<input type="file" name="image" id="image" accept="image/*"
class="form-control" value="{{ old('image') }}>
</form>
public function getUrlAttribute()
{
if(substr($this->image, 0, 4) === "http"){
return $this->image;
}
return '/images/'.$this->image;
}
在文件create.blade.php(视图)中插入以下代码:
public function store(Request $request, $id)
{
if ($request->hasFile('image')){
$rules = [
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg'
];
$this->validate($request, $rules);
// Save file in directory
$file = $request->file('image');
$path = public_path() . '/images';
$fileName = md5(rand(0,9999)).'.'.$file->getClientOriginalExtension();
$file->move($path, $fileName);
$imageProduct = new ImageProduct();
$imageProduct->image = $fileName;
}
<form enctype="multipart/form-data" method="post" action=" " >
{{ csrf_field() }}
<label for="image" class="btn btn-primary">Inserir Imagem</label>
<input type="file" name="image" id="image" accept="image/*"
class="form-control" value="{{ old('image') }}>
</form>
public function getUrlAttribute()
{
if(substr($this->image, 0, 4) === "http"){
return $this->image;
}
return '/images/'.$this->image;
}
祝你好运 您需要为要存储图像的laravel文件夹授予权限。 建议如下:
sudo chown www-data:www-data/var/www/html/project-name/path/to/folder
您需要避开所有反斜杠-->$destinationPath=public_path().sprintf(\\\uploads\\%s\\”,str_random(8));我得到一个错误:不可能创建根目录
,只是为了敲响警钟。您可以使用0755作为权限模式,如果要创建随机文件或文件夹名称,可以使用time()函数。