在laravel 4中显示图像的问题

时间:2014-08-11 04:20:14

标签: php laravel-4

我使用Intervention中的laravel4 PHP图片处理和操作库上传了一张图片。通过使用这个库,我正确地上传了图像,但我面临的问题是,当我尝试从数据库显示这些图像时,它显示存在无效的URL。但是,网址是有效的,我认为这是一个有关权限的问题。

我发布了默认情况下提供给上传文件夹的以下权限,并允许用户访问它是www-data。enter image description here

我的代码

@section('main')
@extends('layouts/user')
<h1>All Users</h1>

<p>{{ link_to_route('users.create', 'Add new user') }}</p>

@if ($users->count())
    <table class="table table-striped">
        <thead>
            <tr>
                <th>FirstName</th>
                <th>LastName</th>
                <th>Email</th>
                <th>Address</th>
                <th>Image</th>
                <th>Action</th>
            </tr>
        </thead>

        <tbody>
            @foreach ($users as $user)
                <tr>
                    <td>{{ $user->firstName }}</td>
                    <td>{{ $user->lastName }}</td>
                    <td>{{ $user->emailId }}</td>

                    <td>{{ $user->address }}</td>
                    <td>{{ HTML::image('uploadImg/B05BgJ_school-uniform.jpg') }}</td>

                    </td>

                    <td>{{ link_to_route('users.edit', 'Edit',
                            array($user->id), array('class' => 'btn btn-info')) }}</td>
                                                <td>
                                    {{ Form::open(array('method' 
                            => 'DELETE', 'route' => array('users.destroy', $user->id))) }}                       
                                                        {{ Form::submit('Delete', array('class'
                            => 'btn btn-danger')) }}
                        {{ Form::close() }}
                    </td>

                </tr>
            @endforeach
        </tbody>

    </table>
            <div class="pagination"><?php echo $users->links(); ?></div>
@else
    There are no users
@endif

@stop

4 个答案:

答案 0 :(得分:4)

  1. 检查从浏览器直接显示图像时出现的错误。(我的意思是在浏览器中输入图像的完整URL)。

  2. 确保laravel生成的网址和实际图片网址相同。如果你可以直接从浏览器显示图像。

  3. 所有图片都应该在公共文件夹中,并且通常不应该在基础上有.htacces文件。(我的意思是公共下面的文件夹)

  4. 确保公共文件夹中的.htaccess ile是这样的,

        <IfModule mod_rewrite.c>
            Options -MultiViews
            RewriteEngine On
    
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
    
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteRule ^ index.php [L]
        </IfModule> 
    

答案 1 :(得分:1)

您创建/上传的所有图片都需要在公开/(或任何公共文件夹)之后进行,否则它将无法呈现为有效的网址,因为请求将始终是路径到公共文件夹/图像 - 文件夹/ image.jpg或其他东西,根据图像中的文件夹结构,图像文件夹无法访问。

在您的情况下,您创建了一个与app目录处于同一级别的文件夹uploadimg,而它应该是public / uploadimg

我注意到的另一件事是你要求提供图像

 {{ HTML::image('uploadImg/B05BgJ_school-uniform.jpg') }}

虽然它应该来自

 {{ HTML::image('uploadimg/B05BgJ_school-uniform.jpg') }}

(假设图像名称和路径正确,如果uploadimg文件夹位于公共/文件夹中,这将有效)

答案 2 :(得分:0)

问题是,perhapse,图像的路径。 laravel应用程序的根目录是app /目录。

尝试puth apsolute路径: HTML::image('/var/www/testLara/uploadImg/B05BgJ_school-uniform.jpg')

您应该将图像存储在/public目录的某个子目录中。

答案 3 :(得分:0)

您的&#34; uploadImg&#34;目录不在&#34; public&#34;目录,正如其他人所说,这是根据你的截图。 是否有另一个uploadImg目录&#34; public&#34;? 有三个非常重要的问题仍然没有答案。

  1. 您的数据库在表格中显示的路径是什么?
  2. {{ HTML::image('uploadImg/B05BgJ_school-uniform.jpg') }}输出什么?
  3. 实际图像文件位于何处,一旦上传? (现在去检查)