我从github组件下载,简化了文件验证和上传(https://github.com/brandonsavage/Upload)。
我将所有src文件夹放在www文件夹中(我使用WAMP)。
我还在github progaram安装了作为givven的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';
后,我仍然会收到同样的错误 -
(!)致命错误:第7行的C:\ wamp \ www \ Upload \ up.php中找不到“上传\存储\文件系统”类
时间记忆功能位置
1 0.0084 250616 {main}().. \ up.php:0
答案 0 :(得分:1)
如果您不使用composer和composer的自动加载器,它将无效,但此软件包具有自己的类自动加载器,请尝试包含Autoloader并将其注册在脚本的最顶层:
include 'src/Upload/Autoloader.php'
Autoloader::register();
答案 1 :(得分:0)
在使用课程之前,您必须加载作曲家的课程自动加载器。如果没有定义
,Autoloader本质上就是查找该类的函数尝试添加:
require __DIR__ . '/vendor/autoload.php';
在up.php的开头(根据vendor
文件夹所在的位置调整路径相对于up.php
)
答案 2 :(得分:0)
我通过添加新的存储文件夹+在上传之前添加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();
}
谢谢!