图片上传没有提交laravel

时间:2014-11-22 08:41:11

标签: php jquery laravel-4 image-uploading

我想在Laravel中上传没有提交按钮的图片。我从谷歌得到了一些教程。我尝试过但都失败了。

任何人都可以帮助我。

我在create.blade.php中的表格:

<!DOCTYPE html>
<html lang="<?php echo Config::get('application.language') ?>">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>{{ Config::get('administrator::administrator.title') }}</title>



    {{ HTML::style('css/bootstrap-cerulean.min.css') }}

    {{ HTML::style('css/charisma-app.css') }}
    {{ HTML::style('bower_components/fullcalendar/dist/fullcalendar.css') }}
    {{ HTML::style('bower_components/fullcalendar/dist/fullcalendar.print.css') }}
    {{ HTML::style('bower_components/chosen/chosen.min.css') }}
    {{ HTML::style('bower_components/colorbox/example3/colorbox.css') }}
    {{ HTML::style('bower_components/responsive-tables/responsive-tables.css') }}
    {{ HTML::style('bower_components/bootstrap-tour/build/css/bootstrap-tour.min.css') }}
    {{ HTML::style('css/jquery.noty.css') }}
    {{ HTML::style('css/noty_theme_default.css') }}
    {{ HTML::style('css/elfinder.min.css') }}
    {{ HTML::style('css/elfinder.theme.css') }}
    {{ HTML::style('css/jquery.iphone.toggle.css') }}
    {{ HTML::style('css/uploadify.css') }}
    {{ HTML::style('css/animate.min.css') }}
    {{ HTML::style('js/redactor.css') }}
    {{ HTML::style('packages/frozennode/administrator/js/jquery/select2/select2.css') }}

    <!--[if lte IE 9]>
        <link href="{{asset('packages/frozennode/administrator/css/browsers/lte-ie9.css')}}" media="all" type="text/css" rel="stylesheet">
    <![endif]-->

<style>

.preview
{
width:300px;
border:solid 1px #dedede;
padding:6px;
margin-right:10px;
}
.img
{
border:solid 1px #dedede;
padding:6px;
margin-right:10px;
}
#preview
{
font-size:12px;
}


</style>
</head>
<body>
<!-- loading animation -->
    <div id="qLoverlay" style="display: none;"></div>
    <div id="qLbar" style="display: none;"></div>

        @include('administrator::partials.header')

        <h4>Create New Post</h4><hr>

        <form id="imageform" method="post" enctype="multipart/form-data" action='{{ url() }}/ajaximage.php'>
        <h3>Upload your image</h3>
        <div id='imageloadstatus' style='display:none'><img src="loader.gif" alt="Uploading...."/></div>
        <div id='imageloadbutton'>
        <input type="file" name="photoimg" id="photoimg" />
        </div>
        </form>

        @if($errors->has())
        <div id="form-errors">
            <p class="alert-danger">The following errors have occurred:</p>

            <ul>
                @foreach($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div><!-- end form-errors -->
        @endif

        {{ Form::open(array('action' => 'PostsController@store','files'=>true, 'class' => 'form-horizontal')) }}

        <div class="form-group">
            {{ Form::label('title', 'Post title',array('class' => 'col-sm-2 control-label')) }}
            <div class="col-sm-6">
                <input type="text" name="title" class="form-control" id="title" placeholder="Post Title Here">
            </div>
        </div>
        <div class="form-group">
            {{ Form::label('meta_title', 'Meta title',array('class' => 'col-sm-2 control-label')) }}
            <div class="col-sm-6">
                <input type="text" name="meta_title" class="form-control" id="meta_title" placeholder="Meta Title (Optional)">
            </div>
        </div>
        <div class="form-group">
        {{ Form::label('reporters', 'Reporter',array('class' => 'col-sm-2 control-label')) }}
            <div class="col-sm-6">
                <select id="e1" style="width:300px" name="reporter">
                    @foreach ($reporters as $reporter)
                        <option value="{{ $reporter->id }}">{{ $reporter->first_name }} {{ $reporter->last_name }}</option>
                    @endforeach
                </select>
            </div>
        </div>
        <div class="form-group">
        {{ Form::label('categories', 'Categories',array('class' => 'col-sm-2 control-label')) }}
            <div class="col-sm-6">
                <select id="e2" multiple="multiple" class="form-control" name="categories[]">
                    @foreach ($categories as $category)
                        <option value="{{ $category->id }}">{{ $category->name }}</option>
                    @endforeach
                </select>
            </div>
        </div>
        <div class="form-group">
        {{ Form::label('tags', 'Tags',array('class' => 'col-sm-2 control-label')) }}
            <div class="col-sm-6">
                <select id="e3" multiple="multiple" class="form-control" name="tags[]">
                    @foreach ($tags as $tag)
                        <option value="{{ $tag->id }}">{{ $tag->tag_name }}</option>
                    @endforeach
                </select>
            </div>
        </div>

        <div class="form-group">
            {{ Form::label('image', 'Feature Image',array('class' => 'col-sm-2 control-label')) }}
            <div id='preview' class="col-sm-6">

            </div>
        </div>

        <div class="form-group">
            {{ Form::label('body', 'Post Body',array('class' => 'col-sm-2 control-label')) }}
            <div class="col-sm-8">
                <textarea name="body" id="redactor" class="form-control" rows="3"></textarea>
            </div>
        </div>

        <div class="form-group">
            {{ Form::label('top', 'Top',array('class' => 'col-sm-2 control-label')) }}
            <div class="col-sm-6">
                {{ Form::checkbox('top', 1) }}
            </div>
        </div>
        <div class="form-group">
            {{ Form::label('stop', 'Secondary Top',array('class' => 'col-sm-2 control-label')) }}
            <div class="col-sm-6">
                {{ Form::checkbox('stop', 1) }}
            </div>
        </div>
        <div class="form-group">
            {{ Form::label('htop', 'Highlight Top',array('class' => 'col-sm-2 control-label')) }}
            <div class="col-sm-6">
                {{ Form::checkbox('htop', 1) }}
            </div>
        </div>
        <div class="form-group">
            {{ Form::label('mtop', 'Midlight Top',array('class' => 'col-sm-2 control-label')) }}
            <div class="col-sm-6">
                {{ Form::checkbox('mtop', 1) }}
            </div>
        </div>
        <div class="form-group">
            {{ Form::label('ftop', 'Facebook Share',array('class' => 'col-sm-2 control-label')) }}
            <div class="col-sm-6">
                {{ Form::checkbox('ftop', 1) }}
            </div>
        </div>

        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
            {{ Form::button('Create Post', array('type'=>'submit', 'class'=>'btn btn-default')) }}
            </div>
        </div>
        {{ Form::close() }}

        @include('administrator::partials.footer')

        </div><!-- End contentwrapper -->
      </div><!-- End #content -->

    </div><!-- End #wrapper -->



    {{ HTML::script('js/jquery.min.js') }}
    {{ HTML::script('bower_components/bootstrap/dist/js/bootstrap.min.js') }}

<!-- library for cookie management -->
{{ HTML::script('js/jquery.cookie.js') }}
<!-- calender plugin -->
{{ HTML::script('bower_components/moment/min/moment.min.js') }}
{{ HTML::script('bower_components/fullcalendar/dist/fullcalendar.min.js') }}
<!-- data table plugin -->
{{ HTML::script('js/jquery.dataTables.min.js') }}

<!-- select or dropdown enhancer -->
{{ HTML::script('bower_components/chosen/chosen.jquery.min.js') }}
<!-- plugin for gallery image view -->
{{ HTML::script('bower_components/colorbox/jquery.colorbox-min.js') }}
<!-- notification plugin -->
{{ HTML::script('js/jquery.noty.js') }}
<!-- library for making tables responsive -->
{{ HTML::script('bower_components/responsive-tables/responsive-tables.js') }}
<!-- tour plugin -->
{{ HTML::script('bower_components/bootstrap-tour/build/js/bootstrap-tour.min.js') }}
<!-- star rating plugin -->
{{ HTML::script('js/jquery.raty.min.js') }}
<!-- for iOS style toggle switch -->
{{ HTML::script('js/jquery.iphone.toggle.js') }}
<!-- autogrowing textarea plugin -->
{{ HTML::script('js/jquery.autogrow-textarea.js') }}
<!-- multiple file upload plugin -->
{{ HTML::script('js/jquery.uploadify-3.1.min.js') }}
<!-- history.js for cross-browser state change on ajax -->
{{ HTML::script('js/jquery.history.js') }}
<!-- application script for Charisma demo -->
{{ HTML::script('js/charisma.js') }}

    {{ HTML::script('js/bootstrap.js') }}
    {{ HTML::script('js/redactor.js') }}
    {{ HTML::script('js/plugin/filemanager.js') }}
    {{ HTML::script('js/plugin/fontcolor.js') }}
    {{ HTML::script('js/plugin/fontsize.js') }}
    {{ HTML::script('js/plugin/imagemanager.js') }}
    {{ HTML::script('js/plugin/table.js') }}
    {{ HTML::script('js/plugin/video.js') }}
    {{ HTML::script('packages/frozennode/administrator/js/jquery/select2/select2.js') }}
    {{ HTML::script('js/jquery.form.js') }}


<script type="text/javascript" >
 $(document).ready(function() { 

            $('#photoimg').die('click').live('change', function()           { 
                       //$("#preview").html('');

                $("#imageform").ajaxForm({target: '#preview', 
                     beforeSubmit:function(){ 

                    $("#imageloadstatus").show();
                     $("#imageloadbutton").hide();
                     }, 
                    success:function(){ 

                     $("#imageloadstatus").hide();
                     $("#imageloadbutton").show();
                    }, 
                    error:function(){ 

                     $("#imageloadstatus").hide();
                    $("#imageloadbutton").show();
                    } }).submit();


            });
        }); 
</script>
<script>
    $(document).ready(function() { 
     $("#e1").select2();
     $("#e2").select2();
     $("#e3").select2();
    });
</script>
<script>
    $(function()
    {
        $('#redactor').redactor({
            buttonSource: true,
            imageUpload: '/image/upload',
            imageManagerJson: '{{ url() }}/uploads/image.php',
            fileUpload: 'file/upload',
            fileManagerJson: '{{ url() }}/uploads/file.php',
            plugins: ['table', 'video','imagemanager','filemanager','fontcolor','fontsize']
        });
    });
</script>

</body>
</html>

我的商店控制器:

public function store()
{
    $validator = Validator::make(Input::all(), Post::$rules);

    if ($validator->passes()) {
        $post = new Post;
        $post->title = Input::get('title');
        $post->body = Input::get('body');
        $post->meta_title = Input::get('meta_title');
        $post->top = (int)Input::get('top');
        $post->stop = (int)Input::get('stop');
        $post->htop = (int)Input::get('htop');
        $post->mtop = (int)Input::get('mtop');
        $post->ftop = (int)Input::get('ftop');
        $post->reporter_id = Input::get('reporter');
        $post->image = Input::get('image');



        $categories = Input::get('categories');
        $tags = Input::get('tags');

        $post->save();

        $post->categories()->sync($categories);
        $post->tags()->sync($tags);

        return Redirect::route('posts')
            ->with('message', 'Product Created');
    }

    return Redirect::back()
        ->with('message', 'Something went wrong')
        ->withErrors($validator)
        ->withInput();
}

我的剧本:

{{ HTML::script('js/jquery.min.js') }}
{{ HTML::script('js/jquery.form.js') }}


<script type="text/javascript" >
 $(document).ready(function() { 

            $('#photoimg').die('click').live('change', function()           { 
                       //$("#preview").html('');

                $("#imageform").ajaxForm({target: '#preview', 
                     beforeSubmit:function(){ 

                    $("#imageloadstatus").show();
                     $("#imageloadbutton").hide();
                     }, 
                    success:function(){ 

                     $("#imageloadstatus").hide();
                     $("#imageloadbutton").show();
                    }, 
                    error:function(){ 

                     $("#imageloadstatus").hide();
                    $("#imageloadbutton").show();
                    } }).submit();


            });
        }); 
</script>

我的ajaximage.php:

<?php

$path = "uploads/";
$actual_image_name="";
$valid_formats = array("jpg", "png", "gif", "bmp","jpeg","PNG","JPG","JPEG","GIF","BMP");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
    include_once __DIR__.'/includes/getExtension.php';
    $imagename = $_FILES['photoimg']['name'];
    $size = $_FILES['photoimg']['size'];

    if(strlen($imagename))
    {
        $ext = strtolower(getExtension($imagename));
        if(in_array($ext,$valid_formats))
        {
            if($size<(1024*1024))
            {
                $actual_image_name = time().substr(str_replace(" ", "_", time()), 5).".".$ext;
                $uploadedfile = $_FILES['photoimg']['tmp_name'];
                include __DIR__.'/includes/compressImage.php';  

                $widthArray = array(200,100,50);
                foreach($widthArray as $newwidth)
                {
                $filename=compressImage($ext,$uploadedfile,$path,$actual_image_name,$newwidth);
                echo "Images Uplaoded and Resized<br/>";
                }                               
                if(move_uploaded_file($uploadedfile, $path.$actual_image_name))
                {   
                //mysqli_query($db,"INSERT INTO user_uploads(image_name,user_id_fk,created) VALUES('$image_name','$session_id','$time')");

                echo '<input type="text" name="image" class="form-control" id="image" value="'.$actual_image_name.'">';
                }
                else
                echo "Fail upload folder with read access.";
            }
            else
            echo "Image file size max 1 MB";                    
        }
        else
        echo "Invalid file format..";   
    }
    else
    echo "Please select image..!";
    exit;
}
?>

当我尝试上传图片时,图片上传但此警告和fatel错误显示。但为什么呢?

PHP Warning:  Unknown: failed to open stream: No such file or directory in Unknown on line 0
PHP Fatal error:  Unknown: Failed opening required 'server.php' (include_path='.:/usr/local/php5/lib/php') in Unknown on line 0

请帮帮我。

当我删除图片上传表单时,工作正常,没有错误。

0 个答案:

没有答案