Jump to content

Getting images to update


cerberus47810

Recommended Posts

I managed to save my images to the database, but now when I try to edit the image and then save it, it doesn't save. I'm not sure where I'm going wrong. All the content saves except for the image. I'm using laravel 4.2

Here is my edit.blade.php

@extends('templates::admin')
@section('content')

    {{ HTML::script('js/test.js') }}
    <script type="text/javascript">
        var settings = {
                    url: '{{ asset("upload/upload.php") }}',
                    dragDrop:true,
                    multiple : false,
                    showFileCounter:false,
                    showDone: false,
                    fileName: "myfile",
                    allowedTypes:"jpg,png,gif,pdf", 
                    returnType:"json",
                    showDelete:true,
                }
    </script>

    <div class="row">
        <div class="col-lg-12">
            {{ Form::model($galleries, array('method' => 'PATCH', 'route' => array('admin.gallery.update', $galleries->id), 'class' => 'add-form')) }}
        <div class="form">
            <div>
                {{ Form::label('title', 'Title') }}
            </div>
            <div>
                {{ Form::text('title', $galleries->title, array('id' => 'title', 'class' => 'form-control')) }}
            </div>
            <div>
                {{ Form::label('content', 'Content') }}
            </div>
            <div>
                {{ Form::textarea('content', $galleries->content) }}
            </div>

            <div id="fileuploader" class="testing">Upload</div>
            {{ getImages($galleries->image) }}
            <script>
                var uploadObj = $("#fileuploader").uploadFile(settings);
            </script>
            {{ Form::hidden('image', '', array('id' => 'img-add', 'class' => 'img-hidden')) }}

            <div>
                {{ Form::submit('Submit', array('class' => 'btn btn-default', 'role' => 'button')) }}
            </div>
        </div>
    {{ Form::open() }}

    @if($errors->any())
        <ul>
            {{ implode('', $errors->all('<li class="error">:message</li>')) }}
        </ul>
    @endif
        </div>
    </div>
@stop

And here is the test.js

$(document).ready(function(){


    $(".add-form").submit(function(event){
        $(this).find(".testing").each(function(){
            var image = [];
            $(this).parent().find(".ajax-file-upload-statusbar").each(function() {
                image.push($.trim($(this).find(".ajax-file-upload-filename").html()));
            });
            $(this).parent().find(".img-hidden").val(JSON.stringify(image));
        });

        $.ajax({
            type: 'POST',
            url: $(this).attr('action'),
            data: $(this).serialize(),
            dataType: 'json',
            encode : true,
        });
    });
});

and this is the update function in my galleryController

public function update($id)
    {
        $input = Input::all();
        $validation = Validator::make($input, Gallery::$rules);

        if($validation->fails()){
            return Redirect::route('admin.gallery.edit')
                ->withInput()
                ->withErrors($validation)
                ->with('message', 'There were validation errors');
        }

        if($validation->passes()){
            $galleries = Gallery::FindOrFail($id);

            $galleries->update($input);
            return Redirect::route('admin.gallery.index', $id);
        }
    }

Here is also the upload.php

<?php
$output_dir = "../uploads/";
if(isset($_FILES["myfile"]))
{
    $ret = array();

//  This is for custom errors;  
/*  $custom_error= array();
    $custom_error['jquery-upload-file-error']="File already exists";
    echo json_encode($custom_error);
    die();
*/
    $error =$_FILES["myfile"]["error"];
    //You need to handle  both cases
    //If Any browser does not support serializing of multiple files using FormData() 
    if(!is_array($_FILES["myfile"]["name"])) //single file
    {
        $fileName = $_FILES["myfile"]["name"];
        move_uploaded_file($_FILES["myfile"]["tmp_name"],$output_dir.$fileName);
        $ret[]= $fileName;
    }
    else  //Multiple files, file[]
    {
      $fileCount = count($_FILES["myfile"]["name"]);
      for($i=0; $i < $fileCount; $i++)
      {
        $fileName = $_FILES["myfile"]["name"][$i];
        move_uploaded_file($_FILES["myfile"]["tmp_name"][$i],$output_dir.$fileName);
        $ret[]= $fileName;
      }

    }
    echo json_encode($ret);
 }
 ?>
Link to comment
https://forums.phpfreaks.com/topic/294499-getting-images-to-update/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.