brandonsavage / Upload

File uploads with validation and storage strategies
MIT License
1.67k stars 315 forks source link

It was uploading files now has stopped #104

Closed mayurpande closed 7 years ago

mayurpande commented 7 years ago

Package was working fine but now it just keeps throwing an error and not uploading files.

Run a var dump on the error and says;

object(Upload\Exception\UploadException)#131 plus whole load more.

This is my code;

<?php

namespace App\Controllers;

use App\Controllers\Controller;
use Upload\Storage\FileSystem;
use Upload\File;
use Upload\Validation\MimeType;
use Upload\Validation\Size;

//import validator
use Respect\Validation\Validator as v;

class ImageController extends Controller
{

    public function getImageUpload($request,$response){
        return $this->view->render($response,'admin-upload.twig');
    }

    public function postImageUpload($request,$response){
        $dir = USER_ROOT;
        $storage = new \Upload\Storage\FileSystem($_SERVER['DOCUMENT_ROOT'] . "/img");
        $file = new \Upload\File('upload',$storage);

      //  $new_filename = uniqid();
       // $file->setName($new_filename);

        $file->addValidations(array(
            new \Upload\Validation\Mimetype(array('image/png','image/gif','image/jpg','image/jpeg')),

            new \Upload\Validation\Size('20M')
        ));

        // 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 {

            if ($file->upload()) {
                $this->flash->addMessage('success','Image uploaded');
                $this->flash->addMessage('info','Make sure to add the same name i.e. image.jpg as a string in other form submission');
                return $response->withRedirect($this->router->pathFor('admin.update')); 
            }

        } catch (\Exception $e) {
            // Fail!

            $errors = $file->getErrors();
            var_dump($e);
            $this->flash->addMessage('error',$errors);
            return $response->withRedirect($this->router->pathFor('admin.update'));
        }
    }
}
mayurpande commented 7 years ago

The error is leaves is this;

object(Upload\Exception\UploadException)#131 (7) { ["message":protected]=> string(22) "File validation failed" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(0) ["file":protected]=> string(77) "/var/www/html/ryanotoolecollett.com/vendor/codeguy/upload/src/Upload/File.php" ["line":protected]=> int(324) ["trace":"Exception":private]=> array(36) { [0]=> array(6) { ["file"]=> string(71) "/var/www/html/ryanotoolecollett.com/app/Controllers/ImageController.php" ["line"]=> int(50) ["function"]=> string(6) "upload" ["class"]=> string(11) "Upload\File" ["type"]=> string(2) "->" ["args"]=> array(0) { } } [1]=> array(4) { ["function"]=> string(15) "postImageUpload" ["class"]=> string(31) "App\Controllers\ImageController" ["type"]=> string(2) "->" ["args"]=> array(3) { [0]=> object(Slim\Http\Request)#125 (15) { ["method":protected]=> string(4) "POST" ["originalMethod":protected]=> string(4) "POST" ["uri":protected]=> object(Slim\Http\Uri)#79 (9) { ["scheme":protected]=> string(4) "http" ["user":protected]=> string(0) "" ["password":protected]=> string(0) "" ["host":protected]=> string(21) "ryanotoolecollett.com" ["port":protected]=> int(80) ["basePath":protected]=> string(0) "" ["path":protected]=> string(19) "/admin-upload-image" ["query":protected]=> string(0) "" ["fragment":protected]=> string(0) "" } ["requestTarget":protected]=> NULL ["queryParams":protected]=> array(0) { } ["cookies":protected]=> array(1) { ["PHPSESSID"]=> string(26) "dj4fk8kq7uccc0kqq54k4gfea4" } ["serverParams":protected]=> array(38) { ["REDIRECT_STATUS"]=> string(3) "200" ["HTTP_HOST"]=> string(21) "ryanotoolecollett.com" ["HTTP_CONNECTION"]=> string(10) "keep-alive" ["CONTENT_LENGTH"]=> string(7) "4431621" ["HTTP_CACHE_CONTROL"]=> string(9) "max-age=0" ["HTTP_ORIGIN"]=> string(28) "http://ryanotoolecollett.com" ["HTTP_UPGRADE_INSECURE_REQUESTS"]=> string(1) "1" ["HTTP_USER_AGENT"]=> string(104) "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36" ["CONTENT_TYPE"]=> string(68) "multipart/form-data; boundary=----WebKitFormBoundaryGKiA7lTAq49IwR0J" ["HTTP_ACCEPT"]=> string(74) "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" ["HTTP_REFERER"]=> string(47) "http://ryanotoolecollett.com/admin-upload-image" ["HTTP_ACCEPT_ENCODING"]=> string(13) "gzip, deflate" ["HTTP_ACCEPT_LANGUAGE"]=> string(14) "en-US,en;q=0.8" ["HTTP_COOKIE"]=> string(36) "PHPSESSID=dj4fk8kq7uccc0kqq54k4gfea4" ["PATH"]=> string(60) "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ["SERVER_SIGNATURE"]=> string(82) "

This throws this error when I use an image that is above above 1M. I tried to compress on of the images that was 4.4 MB and got it down to 692.6 kb and it uploads fine. However I would rather keep original size.

I also changed my php ini file to accept;

; Maximum allowed size for uploaded files.
upload_max_filesize = 20M

; Must be greater than or equal to upload_max_filesize
post_max_size = 20M
mayurpande commented 7 years ago

Managed to resolve the issue. I have two php ini files for some reason, and was editing the wrong one.

I was editing this one;

/etc/php/7.0/cli/php.ini

When I was meant to be editing this one;

/etc/php/7.0/apache2/php.ini