How to upload file with brandonsavage/Upload


#1

Hi, I need to upload file with brandonsavage/Upload, but I have many problème in the code, the upload don’t work. my Model Image.
<?php

namespace Cart\Models;

use Illuminate\Database\Eloquent\Model;
use Cart\Models\Customer;
use Upload\Storage\FileSystem;
use Upload\File;
use Upload\Validation\Mimetype;
use Upload\Validation\Size;

/**
 * Description of Image
 *
 * @author mbele
 */
class Image extends Model{
protected $table = 'image';

protected $fillable = [
    'alt',
    'url',
];

public function customers(){
    return $this->hasOne(Customer::class);
}    

public function upload() {
    $storage = new FileSystem('../public/img');
    $file = new File('avatar', $storage);

    // Optionally you can rename the file on upload
    $new_filename = uniqid();
    $file->setName($new_filename);

    // Validate file upload
    // MimeType List => http://www.iana.org/assignments/media-types/media-types.xhtml
    $file->addValidations(array(
        // Ensure file is of type "image/png"
        new Mimetype('image/png', 'image/gif', 'image/jpg', 'image/pdf'),

        //You can also add multi mimetype validation
        //new \Upload\Validation\Mimetype(array('image/png', 'image/gif'))

        // Ensure file is no larger than 5M (use "B", "K", M", or "G")
        new Size('5M')
    ));

    // Access data about the file that has been uploaded
    $data = array(
        'name'       => $file->getNameWithExtension(),
        'extension'  => $file->getExtension(),
        'mime'       => $file->getMimetype(),
        'size'       => $file->getSize(),
        'md5'        => $file->getMd5(),
        'dimensions' => $file->getDimensions()
    );

    // Try to upload file
    try {
        // Success!
        $file->upload();
    } catch (\Exception $e) {
        // Fail!
        $errors = $file->getErrors();
    }        
}
} 

my form

{% extends 'templates/app.twig' %}

{% block content %}
    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <div class="panel panel-default">
                <div class="panel-heading">Add customer</div>
                <div class="panel-body">
                    <form action="{{ path_for('add.customer') }}" method="post" autocomplete="off" enctype="multipart/form-data">
                        <div class="form-group{{ errors.email ? ' has-error' : '' }}">
                            <label for="email">Email</label>
                            <input type="email" name="email" id="email" placeholder="you@domain.com" class="form-control">
                            {% if errors.email %}
                                <span class="help-block">{{ errors.email | first }}</span>
                            {% endif %}
                        </div>
                        <div class="form-group{{ errors.name ? ' has-error' : '' }}">
                            <label for="name">Name</label>
                            <input type="text" name="name" id="name" class="form-control">
                            {% if errors.name %}
                                <span class="help-block">{{ errors.name | first }}</span>
                            {% endif %}
                        </div>
                        <div class="form-group{{ errors.file ? ' has-error' : '' }}">
                            <label for="avatar">Avatar</label>
                            <input type="hidden" name="avatar" role="uploadcare-uploader"/>
{#                            <input type="hidden" name="image" class="form-control" value=""/>#}
                            {% if errors.file %}
                                <span class="help-block">{{ errors.file | first }}</span>
                            {% endif %}
                        </div>                        
                        <div class="form-group{{ errors.password ? ' has-error' : '' }}">
                            <label for="password">Password</label>
                            <input type="password" name="password" id="password" class="form-control">
                            {% if errors.password %}
                                <span class="help-block">{{ errors.password | first }}</span>
                            {% endif %}
                        </div>
                        <input type="hidden" name="role" id="role" value="user" class="form-control">
                        <button type="submit" class="btn btn-default">Sign up</button>
                        {{ csrf.field | raw }}
                    </form>
                </div>
            </div>
        </div>
    </div>
{% endblock %}

public function postUser(Request $request, Response $response, User $customer, Image $image, Router $router, Validator $validator) {
    $validation = $validator->validate($request, [
        'email' => v::noWhitespace()->notEmpty()->email(),
        'name' => v::notEmpty()->alpha(),
        'url'  => v::noWhitespace(),
        'password' => v::noWhitespace()->notEmpty(),
        'role' => v::notEmpty()->alpha(),
    ]);
     
    if ($validation->fails()) {
        return $response->withRedirect($router->pathFor('add.customer'));
    }       
     
    $customer = $customer->create([
        'email' => $request->getParam('email'),
        'name' =>  $request->getParam('name'),
        'password' =>  password_hash($request->getParam('password'), PASSWORD_DEFAULT),
        'role' =>  $request->getParam('role'),
        'image_id' => $image->id,
    ]);       
    $var = 'avatar';
    $image = $image->firstOrCreate([
//            'alt' => $request->getParam('alt'),
         
        'url' =>  $image->upload($var),
    ]);
     
    $image->customers()->save($customer);
     
    return $response->withRedirect($router->pathFor('home'));       
}

my error

Details

Type: InvalidArgumentException Message: Cannot find uploaded file identified by key: avatar File: C:\laragon\www\cart\vendor\codeguy\upload\src\Upload\File.php Line: 139 Trace


#2

Looks like you’re missing and input type “file”, that may be why its not finding “avatar”


#3

Hi, I put the type=“file”, but I have this error message now

Slim Application Error
The application could not run because of the following error:

Details

Type: Illuminate\Database\QueryException
Code: 23000
Message: SQLSTATE[23000]: Integrity constraint violation: 1048 Column ‘url’ cannot be null (SQL: insert into image (url, updated_at, created_at) values (, 2017-04-28 01:30:43, 2017-04-28 01:30:43))
File: C:\laragon\www\cart\vendor\illuminate\database\Connection.php
Line: 647

How to do?


#4

Hi, I resolve the problem of upload, but this class upload don’t send other image extension in a server, but all the informations are save in databse