0

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();
6
  • 1
    __contruct -> __construct Commented Dec 9, 2019 at 11:38
  • I made a mistake when rewriting Commented Dec 9, 2019 at 11:43
  • 1
    How did you determine that the constructor didn't work? Also, you should copy/paste your actual code instead of rewriting it. If you rewrite it, we won't be able to spot potential typos (and, as you've noticed, you might introduce new) Commented Dec 9, 2019 at 11:43
  • Fatal error: Cannot redeclare Imageupload::__construct() Commented Dec 9, 2019 at 11:45
  • 1
    Sounds like you've declared the constructor multiple times? Also, $_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. Commented Dec 9, 2019 at 11:48

1 Answer 1

1

Looks like you're creating a wheel again? :) Try one of the libraries has been created for this purpose. https://github.com/brandonsavage/Upload

Install composer in you operating system and run the following command in your command line

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();
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your answer. Good suggestion "Looks like you're creating a wheel again" :)
But I like to do everything myself.
@Syriusz You're welcome :D, but i thought you're stuck. anyways enjoy coding :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.