0

I am trying to run the following code but get lots of errors, such as the one below;

Notice: Undefined index: file in C:\xampp\htdocs\SimpleCMS\_class\simpleCMS.php on line 14,21,22.23.

And my code is

<html>
<body>

<form action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>




<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
?>
Matt
  • 74,352
  • 26
  • 153
  • 180
  • Is that all in a single file? If so, your PHP code will run as soon as you load the form, since there's no check to see if the form has been submitted. You need to wrap the PHP in an if statement - a check to see if `$_POST['submit']` is set would work. – andrewsi Apr 30 '13 at 13:48
  • i ma following this tutorial http://www.w3schools.com/php/php_file_upload.asp – First Nameere Er Apr 30 '13 at 13:49

5 Answers5

0

When you first load your page, the $_FILES variable is empty. So before using it in any way, check, that your property is set, e.g., using isset() like this:

<?php
if ( isset($_FILES['file']) ) {
  if ($_FILES["file"]["error"] > 0) {
    echo "Error: " . $_FILES["file"]["error"] . "<br>";
  } else {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Stored in: " . $_FILES["file"]["tmp_name"];
  }
}
?>
Sirko
  • 72,589
  • 19
  • 149
  • 183
0

You forgot to check if your form has been submitted.

In your case, wrap your PHP code into a if like this one :

if (isset($_POST['submit']))
{
    // your code
}
Mathieu Amiot
  • 1,204
  • 8
  • 16
0

Use isset function before getting element from array by index.

Danil Speransky
  • 29,891
  • 5
  • 68
  • 79
0

It gives you that error, because there are no files, in the $_FILE variable. You should check if it is set, before using it like this:

if(isset($_FILES["file"])) {
    if ($_FILES["file"]["error"] > 0)
    {
        echo "Error: " . $_FILES["file"]["error"] . "<br>";
    }
    else
    {
        echo "Upload: " . $_FILES["file"]["name"] . "<br>";
        echo "Type: " . $_FILES["file"]["type"] . "<br>";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
        echo "Stored in: " . $_FILES["file"]["tmp_name"];
    }
}
Daniel Mensing
  • 956
  • 5
  • 13
0

You already know that at first $_FILES is empty but PHP wants to inform you in case of using not necessary indexes.

In this specific case you can do like that:

if ($_FILES["file"]["error"] > 0)
{
    echo "Error: " . @$_FILES["file"]["error"] . "<br>";
}
else
{
    echo "Upload: " . @$_FILES["file"]["name"] . "<br>";
    echo "Type: " . @$_FILES["file"]["type"] . "<br>";
    echo "Size: " . (@$_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Stored in: " . @$_FILES["file"]["tmp_name"];
}

"@" will cause you will dont get any warnings.

webrama.pl
  • 1,870
  • 1
  • 23
  • 36