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
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
}

?>

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.