0

This is suppose to be pretty straight forward and is driving me mad!

I'm trying to upload a file in PHP and writing the file to MySQL as a blob.

Problem is that the site throws a "Undefined index" all the time when I'm trying to use the

$_FILES['file']['tmp_name'] property.

Here is my code :

<head>
    <title>Upload Worksheet</title>
</head>
<body>
    <form action="index.php" method="POST" enctype="multipart/form-data">
        <input type="hidden" name="MAX_FILE_SIZE" value="2000" />
        File : 
        <input type="file" name"file" id="file"><input type="submit" value="Upload">
    </form>


    <?php

        //connect to db
        mysql_connect("localhost","root","") or die(mysql_error());
        mysql_select_db("autoedi") or die(mysql_error());

        //file properties
        $file = $_FILES['file']['tmp_name'];

        if(!isset($file))
        echo "Please choose a file.";
        else {
            $uploadfile = addslashes(file_get_contents( $_FILES['file']['name']));
            $uploadfilename = addslashes($_FILES['file']['tmp_name']);  
        }


    ?>


</body>

This is what the error message looks like :

Error Message

I haven't even gotten to the database side, as I can't get past this stage.

I'm a PHP noob, so any help would be greatly appreciated!

2 Answers 2

3

You recieve that error message because the form is not sent, yet. When you hit the upload button, the form is sent to your server and PHP populates the $_POST and $_FILES array with data. However, the arrays are empty until that point. It is therefore good practice to check whether or not your data is set, like so:

if (isset ($_POST['upload']))
{
    // upload logic here

    if(!isset($_FILES['file']) || ($_FILES['file']['tmp_name'] == ''))
        echo "Please choose a file.";
    else {
        $uploadfile = addslashes(file_get_contents( $_FILES['file']['name']));
        $uploadfilename = addslashes($_FILES['file']['tmp_name']);  
    }
}

This assumes you have a submit button named "upload".

Sign up to request clarification or add additional context in comments.

4 Comments

Ok, so I've added this in and the error messages are gone, but it still comes back as empty. I've added this to the top of my logic - if (!isset ($_POST['upload'])) echo "Please select a file." - but after I select a file and click on "upload", it still gives me this message, which means that it is still not set, but I don't understand why?
Have you a button named "upload"? <input type="submit" value="Upload" name="upload">
Sorry DavidS, didn't add the "name" property. That part is sorted out. It still says that the file is not set. Don't understand why not?
Thanks for the help DavidS...+1 for patience!
0

The Above answer is perfect because you should check the for post values in order to run any code on those values but you can also try the following

<?php

        //connect to db
        mysql_connect("localhost","root","") or die(mysql_error());
        mysql_select_db("autoedi") or die(mysql_error());

        //file properties
      if(isset($_POST))
    {

      if(array_key_exists('file',$_FILES))
    {   

     $file = $_FILES['file']['tmp_name'];

        if(!isset($file))
        echo "Please choose a file.";
        else {
            $uploadfile = addslashes(file_get_contents( $_FILES['file']['name']));
            $uploadfilename = addslashes($_FILES['file']['tmp_name']);  
        }

}


    ?>

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.