File upload issue (Undefined Index)

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

CAn you also show HTML code ?

well the html is just a <form> and a <table>, see below:


    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <table>
        <tr>
            <td>First Name: </td>
            <td><input name="firstname" type="text"/></td>
        </tr>
        <tr>
            <td>Surname: </td>
            <td><input name="surname" type="text"/></td>
        </tr>
        <tr>
            <td>Address: </td>
            <td><input name="address" type="text"/></td>
        </tr>
        <tr>
            <td>Phone Number: </td>
            <td><input name="phone" type="text"/></td>
        </tr>
        <tr>
            <td>Image: </td>
            <td><input name="filetoupload" type="file"/></td>
        </tr>
    </table>
</form>

Thanks

Add the enctype attribute in your <form> tag and try:


<form action="" method="post" enctype="multipart/form-data">

Hey now i don’t get all the errors however i still get this one:

Notice: Undefined index: filetoupload in G:\xampp\htdocs\exercise10\Models\Database.class.php on line 42

This is referring to this line in the database class:


\\''.$data['address'].'\\', \\''.$data['phone'].'\\', \\''.$data['filetoupload'].'\\')';

And also now the image is not inserting into the database either OR uploading to the images folder…

Any ideas?

I think the variable $data interprets only $_POST array and filetoupload is the index of $_FILES. So manage an update statement or insert file information to the table differently or set the filename anyhow so that $data will hold the value of files.

It’s very basic but because i am a php coder i thought that i can skip reading about the php and relying on the visual studio.net to write the ccna codes for me.