作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在处理 oop class.php 文件。我想实现函数 __contruct()。我不知道为什么它不起作用。
我觉得有错误,但不知道怎么写。 $args['file_upload'] = $_FILES['file_upload'][''] ?? NULL;
谢谢。
fileupload.class.php
public function __construct($string){
$this->filename = $_FILES['$string']['name']['0'];
$this->temp_path = $_FILES['$string']['tmp_name']['0'];
$this->type = $_FILES['$string']['type']['0'];
$this->size = $_FILES['$string']['size']['0'];
}
public function create() {
if(move_uploaded_file....
}
fileupload.php
if(is_post_request()) {
//Create record using post parameters
$args = [];
$args['prod_name'] = $_POST['prod_name'] ?? NULL;
$args['file_upload'] = $_FILES['file_upload'][''] ?? NULL;
$image = new Imageupload($args);
$result = $image->create();
if($result === true) {
$new_id = $image->id;
$_SESSION['message'] = 'The image was uploaded.';
} else {
// show errors
}
} else {
// display the form
$image = [];
}
<p><input name="file_upload[]" type="file" id="file_upload[]" value=""></p>
<p>Product name: <input type="text" name="prod_name" value="" /></p>
UPDATE1函数有效
public function add_files() {
$this->filename = $_FILES['file_upload']['name']['0'];
$this->temp_path = $_FILES['file_upload']['tmp_name']['0'];
$this->type = $_FILES['file_upload']['type']['0'];
$this->size = $_FILES['file_upload']['size']['0'];
}
$image = new Imageupload($args);
$image->add_files();
最佳答案
看起来你又在造轮子了? :)尝试为此目的创建的库之一。 https://github.com/brandonsavage/Upload
在您的操作系统中安装 Composer 并在命令行中运行以下命令
composer require codeguy/upload
HTML
<form method="POST" enctype="multipart/form-data">
<input type="file" name="foo" value=""/>
<input type="submit" value="Upload File"/>
</form>
PHP
<?php
$storage = new \Upload\Storage\FileSystem('/path/to/directory');
$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();
}
关于php oop(__construct) 文件上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59248046/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!