dongyiyu3953 2016-07-12 14:26 采纳率: 0%
浏览 220

PHP - 程序试图在项目中查找类时出现“Class not found”错误

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条回答 默认 最新

  • dongyan0629 2016-07-12 14:33
    关注

    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)

    评论

报告相同问题?