Jump to content

[SOLVED] Notice: Undefined index: file in /


jej1216

Recommended Posts

I am getting an error

"Notice: Undefined index: file in /usr/local/php_classes/forms/ehi_forms_upload.php on line 24"

on the folowing PHP code:

<?php

  $location = "/usr/local/php_classes/forms/";

  $tmp_name = "none";

  echo $tmp_name;

  $file = "   nada";

  echo $file;

  if (!$_FILES['file']['tmp_name']) {                  <------------ this is line 24

     $form = "Select a file to upload.<br>";

     $form.="<form  method='post' enctype='multipart/form-data'>";

     $form.="   <input type='file' name='file' size='50'><br>";

     $form.="   <input type='submit' value='Upload File'>";

     $form.="</form>";

  } elseif($_FILES['file']['tmp_name'] != "none") {

     copy ($_FILES['file']['tmp_name'], $location.$_FILES['file']['name']) or die("could not upload file.");

     $form ="File upload succeeded...<br>";

     $form.="<ul><li>sent: ".$_FILES['file']['name'];

     $form.="<li>size: ".$_FILES['file']['size'];

     $form.="<li>type: ".$_FILES['file']['type'];

  } else {

     $form = "upload failed";

  }

  echo $form;

?>

 

I thought initially that it was because ['file']['tmp_name'] were not defined, so I defined them above line 24.  However, I get the same error.

 

The file successfully uploads, but I can't find what I have to add to get rid of the error message.

 

I am fairly new at PHP, so forgive me if this is an elementary issue.

 

TIA,

 

jej1216

 

Link to comment
https://forums.phpfreaks.com/topic/77336-solved-notice-undefined-index-file-in/
Share on other sites

It is because $_FILES['file']['tmp_name'] is not set. This is only set when the user submits the form and before that happens PHP will throw you "undefined index".

You should do something like this:

 

<?php

if(isset($_FILES['file']) {
  Do what you wanna do after submitting
}
else {
  Show the form
}

?>

If I understand you correctly, you suggest that I replace

 

if (!$_FILES['file']['tmp_name']) {

 

with

 

if (isset($_FILES['file']) {

 

But that brings a Parse error: parse error, unexpected '{' in /usr/local/php_classes/forms/ehi_forms_upload.php on line 21

 

If I remove the { that brings a Parse error: parse error, unexpected T_VARIABLE in /usr/local/php_classes/forms/ehi_forms_upload.php on line 22

 

I don't have a form page submitting this - I'm working straight off of the PHP code.  Do I need to put a Form page in front of this so $_FILES['file']['tmp_name'] gets set?

 

TIA,

 

jej1216

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.