[英]PHP - “Class not found” error when the program try to find class at the project
我從github組件下載了文件,簡化了文件驗證和上傳( https://github.com/brandonsavage/Upload )。
我將所有src文件夾都放在了www文件夾中(我正在使用WAMP)。
我還安裝了在github progaram上提供的composer.json
我使用以下代碼創建了index.html文件:
<!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>
我直接從自述文件(在github項目)復制到up.php的操作是這樣的:
<?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();
}
在索引處單擊“上傳文件”按鈕后,瀏覽器將我定向到up.php文件,但出現此錯誤:
我試圖用以下方法修復它:
namespace Upload;
像其他頁面一樣。 $storage = new \\Upload\\Storage\\FileSystem(__DIR__."/".$sugar_config['upload_dir']);
什么都沒有。
=====更新-添加require dirname(__DIR__) . '\\Upload\\vendor\\autoload.php';
require dirname(__DIR__) . '\\Upload\\vendor\\autoload.php';
我仍然遇到相同的錯誤-
(!)致命錯誤:在第7行的C:\\ wamp \\ www \\ Upload \\ up.php中找不到類“ Upload \\ Storage \\ FileSystem”
時間記憶功能位置
1 0.0084 250616 {main}().. \\ up.php:0
如果您不使用composer和composer的自動加載器,則它將不起作用,但是此程序包具有其自己的類自動加載器,請嘗試包括自動加載器並在腳本的頂部注冊它:
include 'src/Upload/Autoloader.php'
Autoloader::register();
在使用該類之前,您必須加載作曲家的類自動加載器。 如果沒有定義, Autoloader本質上就是尋找該類的函數
嘗試添加:
require __DIR__ . '/vendor/autoload.php';
在up.php的開頭(根據vendor
文件夾相對於up.php
位置調整路徑)
我通過添加新的存儲文件夾+在上傳之前添加src文件夾解決了該問題。
這是我當前的up.php頁面代碼:
<?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();
}
謝謝 !
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.