Hey,
I am trying to do a simple insert and upload images to an images folder. The insert works and the filename successfully inserts into the database but the file does not upload so i get an error.
This is my code:
FileUpload.class.php
class Fileupload{
protected $filename, $filetoupload;
public function __construct($filetoupload){
$this->filetoupload = $filetoupload;
}
public function upload(){
if(is_uploaded_file($_FILES[$this->filetoupload]['tmp_name']));{
if($_FILES[$this->filetoupload]['type'] == 'image/jpeg'
|| $_FILES[$this->filetoupload]['type'] == 'image/pjpeg'){
$result = move_uploaded_file($_FILES[$this->filetoupload]['tmp_name'],
getcwd().'/images/'.$_FILES[$this->filetoupload]['name']);
} else {
$result = false;
}
}
return $result;
}
}
index.php
<?php
error_reporting(E_ALL);
require_once('Models/Database.class.php');
require_once('Models/Fileupload.class.php');
$database = new Database('localhost', '*****', '', '*****');
if(isset($_POST['submit']))
{
$view->peopleList = $database->inserttbl($_POST);
$fileupload = new Fileupload('filetoupload');
$result = $fileupload->upload();
if(!$result){
$view->error = 'Error uploading file...';
}
}
require_once('Views/index.phtml');
Database.class.php - Just the insert function which DOES work
public function inserttbl($data) { // Insert into table
$data = $this->sanitise($data);
$sql = 'INSERT INTO hussaini_users (firstname, surname, address, phone, image)
VALUES (\\''.$data['firstname'].'\\', \\''.$data['surname'].'\\',
\\''.$data['address'].'\\', \\''.$data['phone'].'\\', \\''.$data['filetoupload'].'\\')';
$this->mysqli->query($sql);
}
But when i hit submit i get this error:-
Notice: Undefined index: filetoupload in G:\xampp\htdocs\exercise10\Models\Fileupload.class.php on line 9
Notice: Undefined index: filetoupload in G:\xampp\htdocs\exercise10\Models\Fileupload.class.php on line 10
Notice: Undefined index: filetoupload in G:\xampp\htdocs\exercise10\Models\Fileupload.class.php on line 11
How can i fix this?
Thanks