move_uploaded_file() fails to upload files

I am working on a page that allows users to upload files. All the if statements work but when in comes to moving the uploaded file,the page shows the upload failure I have using an if statement. Please help me solve it. I’m using Apache on Ubuntu 20.04LTS The code is as follows:

<?php class Document {     public $docname;     public $destination;     public $extension;     public $size;     public $tmp;     public $target_file;     public function __construct()     {         $this->docname = $_FILES["fileToUpload"]["name"];         $this->destination = 'uploads/';         $this->target_file = $this->destination . basename($this->docname);         $this->extension = pathinfo($this->docname, PATHINFO_EXTENSION);         $this->size = $_FILES["fileToUpload"]["size"];         $this->tmp = $_FILES["fileToUpload"]["tmp_name"];     }     public function upload()     {         if (!in_array($this->extension, ["do", "pdf", "jpg", "jpeg", "doc", "docx"])) {             echo "<div class='alert alert-warning'>You file must be either a .doc, .pdf or .jpg</div>";         }         if ($this->size > 10000000) {             echo "<div class='alert alert-warning'>File should not exceed 10MB</div>";         }         if (file_exists($this->target_file)) {             echo "<div class='alert alert-warning'>File Already Exists</div>";         }         if (move_uploaded_file($this->tmp, $this->target_file)) {             require_once('../includes/connect.php');             $sql = "INSERT INTO documents (name,size,doc_type) VALUES ('$this->docname','$this->size','$this->extension')";             if (mysqli_query($conn, $sql)) {                 echo "<div class='alert alert-success'>File $this->docname Uploaded succesfully</div>";             }         } else {             echo "<div class='alert alert-warning'>File upload failure</div>";         }     } } 
Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.