gpt4 book ai didi

php oop(__construct) 文件上传

转载 作者:行者123 更新时间:2023-11-29 09:28:05 24 4
gpt4 key购买 nike

我正在处理 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com