I work on a oop class.php file. I want to implement function __contruct(). I don't know why it doesn't work.
I think there is error, but I don't know how to write it. $args['file_upload'] = $_FILES['file_upload'][''] ?? NULL;
Thanks.
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 function works
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();
__contruct->__construct$_FILES['$string']will literally look for a key named$string(since it's inside single quotes). It should most likely be$_FILES[$string]. However, you're not passing in a string with the key name, you're passing in an array.