File upload, Why errors??

01/26/2013 22:03 Hikarim#1
Hi, i used the code from w3schools for file upload... but i get errors if i try do have all the code in 1 file...

Code:
<html>

<head>
<title>Files</title>

</head>
<body>

<form action="" 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"] / 512) . " kB<br>";
  echo "Stored in: " . $_FILES["file"]["tmp_name"];
  }

?>


</body>
</html>
01/26/2013 22:29 Mikesch01#2
Which error occurs? Please give is the complete error.
01/26/2013 22:32 Hikarim#3
Notice: Undefined index: file in C:\xampp\htdocs\xampp\www\files.php on line 12

Notice: Undefined index: file in C:\xampp\htdocs\xampp\www\files.php on line 18
Upload:

Notice: Undefined index: file in C:\xampp\htdocs\xampp\www\files.php on line 19
Type:

Notice: Undefined index: file in C:\xampp\htdocs\xampp\www\files.php on line 20
Size: 0 kB

Notice: Undefined index: file in C:\xampp\htdocs\xampp\www\files.php on line 21
Stored in:

these are the errors, and, ye, they are not wanted ...
01/26/2013 22:40 Mikesch01#4
First at all, these aren't errors rather notices. I think this happens when you don't uploaded any file (like when you first want to try to upload a file). This can be simply repaired.

PHP Code:
<html>

<head>
<title>Files</title>

</head>
<body>

<form action="" 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(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"] / 512) . " kB<br>";
  echo 
"Stored in: " $_FILES["file"]["tmp_name"];
  }
}
?>


</body>
</html>
01/26/2013 22:41 PseudoPsycho#5
Try this:
HTML Code:
<?php
$op=$_GET['op'];
if($op==='upload' && !empty($_FILES)){
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"] / 512) . " kB<br>";
  echo "Stored in: " . $_FILES["file"]["tmp_name"];
  }
}
else {
<!Doctype html><html>
<head>
<title>Files</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>?op=upload" 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>
</body>
</html>
<?php }