Hi.
I am having problems with an image upload, i manage to insert the filename into the database, but the image does not upload into the “items” folder. Code is shown below:
Fileupload.class.php
<?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().'/items/'.$_FILES[$this->filetoupload]['name']);
} else {
$result = false;
}
}
return $result;
}
}
Items.class.php
<?php
class Items {
protected $mysqli;
public function sanitise($data) { // Escapes parameters before sending SQL query
foreach($data as $key => $value){
$data[$key] = $this->mysqli->real_escape_string($value);
}
return $data;
}
public function __construct($host, $username, $password, $dbname) {
$this->mysqli = new mysqli($host, $username, $password, $dbname);
if ($this->mysqli->errno){
echo 'Unable to connect'.$this->mysqli->error;
exit();
}
}
public function addNewItem($data) { // Add New Item
$data = $this->sanitise($data);
$sql = 'INSERT INTO hussaini_items(CID, name, description, price, image, image_1, image_2, date_added, stock)
VALUES ('.$data['CID'].', \\''.$data['name'].'\\',
\\''.$data['description'].'\\', \\''.$data['price'].'\\',
\\''.$data['filetoupload'].'\\', \\'test\\', \\'test\\', '.date().', '.$data['stock'].')';
$this->mysqli->query($sql) or mysql_error();
}
public function __destruct() {
$this->mysqli->close();
}
}
add-new-item.php
<?php
error_reporting(E_ALL);
$view->pageTitle = 'Add New Item';
require_once('Models/Items.class.php');
require_once('Models/Fileupload.class.php');
$additem = new Items('localhost', '***', '', '***');
if(isset($_POST['submit']))
{
$view->addItem = $additem->addNewItem($_POST); //This WORKS
$fileupload = new Fileupload('filetoupload');
$result = $fileupload->upload(); //This does not work
}
require_once('Views/add-new-item.phtml');
In my add-new-item.phtml file i have a file upload like this:
<input name="filetoupload" type="file" value=""/>
Can anyone see why the upload does not work?
I get the following errors:
Notice: Undefined index: filetoupload in G:\xampp\htdocs\Manstore\Models\Fileupload.class.php on line 9
Notice: Undefined index: filetoupload in G:\xampp\htdocs\Manstore\Models\Fileupload.class.php on line 10
Notice: Undefined index: filetoupload in G:\xampp\htdocs\Manstore\Models\Fileupload.class.php on line 11
Can anybody please help?
Thanks again