0

I downloaded from github component that simplifies file validation and uploading (https://github.com/brandonsavage/Upload).

I put all the src folder at www folder (I working with WAMP).

I also installed the composer.json that givven at the github progaramץ

I created index.html file with this code:

<!DOCTYPE html>
<html>
<body>

    <form action="up.php" method="POST" enctype="multipart/form-data">
     <input type="file" name="foo" value=""/>
     <input type="submit" value="Upload File"/>
    </form>

  </body>
   </html>

The action direct to up.php that i copied from the Readme (at the github project) like that:

<?php
$storage = new \Upload\Storage\FileSystem(__DIR__."/".$sugar_config['upload_dir']);
$file = new \Upload\File('foo', $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 \Upload\Validation\Mimetype('image/png'),

    //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 \Upload\Validation\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();
}

After clicking the Upload file buttom at the index the browser directing me to the up.php file but with this error:

the error

I tried to fix it with:

  1. Adding namespace Upload; like the other pages have.
  2. Changing the path at this line -$storage = new \Upload\Storage\FileSystem(__DIR__."/".$sugar_config['upload_dir']);

Nothing works.

===== Update - after adding the require dirname(__DIR__) . '\Upload\vendor\autoload.php'; i still get the same error -

( ! ) Fatal error: Class 'Upload\Storage\FileSystem' not found in C:\wamp\www\Upload\up.php on line 7

Time Memory Function Location

1 0.0084 250616 {main}( ) ..\up.php:0

3
  • What is the exact and complete error message? There should be the name of class, the file where the error occurs, a line number... Commented Jul 12, 2016 at 14:30
  • It is in the ScreesShot that i attached to the post ("The error" tag) Commented Jul 12, 2016 at 14:32
  • Run composer and include the generated autoload file. Commented Jul 12, 2016 at 14:50

3 Answers 3

1

Before using the class you have to load the composer's class autoloader. Autoloader is essentially the function that looks for the class if it's not defined

Try adding:

require __DIR__ . '/vendor/autoload.php';

at the beginning of the up.php (adjust the path depending on where the vendor folder is located relatively to up.php)

Sign up to request clarification or add additional context in comments.

Comments

1

It will not work, if you don't use composer and composer's autoloader, but this package has it's own class autoloader, try to include Autoloader and register it at the very top of your script:

include 'src/Upload/Autoloader.php'
Autoloader::register();

3 Comments

You should include these class github.com/brandonsavage/Upload/blob/master/src/Upload/… and call static register function
@fofohoilk Just a wild suggestion: backup up.php and your custom files and try to follow "How to Install" section on the github page: i.e. install the composer for windows first then use composer require to get the Upload's sources. Currently I can't explain the error other than having some strange directory structure where autoloader fails to find the class
@fofohoilk try to echo self::$base . $fileName; from Autoloader's autoload function to see from which directory it tries to load classes and to see works it or not. Read about PSR-4 to understand which directory structure you should follow. Hope I help
0

I solved the problem by adding a new Folder for storage + adding src folder before Upload.

This is my current code for the up.php page:

<?php

require __DIR__ . '/vendor/autoload.php';

$storage = new \Upload\Storage\FileSystem('./stor');
$file = new \Upload\File('foo', $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 \Upload\Validation\Mimetype('image/png'),

    //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 \Upload\Validation\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();
}

Thanks !

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.