Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cannot figure out Undefined Index error

Status
Not open for further replies.

proggybilly

Programmer
Apr 30, 2008
110
US
I am trying to do a page which will upload a file to the server, I have already check the max_file_size and post_max_size.. Both are set to 200MB.

The errors I get are:
Notice: Undefined index: tempcmdlog.log in /var/ on line 12
/var/lib/asterisk/sounds/custom/
Notice: Undefined index: tempcmdlog.log in /var/ on line 15
There was an error uploading the file, please try again!

here is my code (maybe someone here can figure what I am doing wrong)
Code:
<?php


$filename = $_POST['datafile'];

function uploadFile($filename){
  $target_path = '/var/lib/asterisk/sounds/custom/';

  $target_path = $target_path . basename( $_FILES[$filename]['name']);

echo $target_path;
   if(move_uploaded_file($_FILES[$filename]['tmp_name'], $target_path)) {
     echo "The file ".  basename( $_FILES[$filename]['name']).
     " has been uploaded";
  $query = "insert into files(path) values('$target_path')";
  $result = mysql_query($query);
   } else {
      echo "There was an error uploading the file, please try again!";
   }
}


if($_POST['submit']){
  uploadFile($filename);
}
?>


<html>
 <head>
  <title>IVR UPLOAD NEW SOUND FILE</title>
 </head>

<body>
<?php require('header.php') ?>
<form method="post" action="upload.php">
<table align="center">
 <tr>
  <td>Please choose the file you wish to upload:<br>
      <input type="file" name="datafile">
  </td>
 </tr>
 <tr><td><input type="submit" name="submit" value="Upload"></td></tr>
</table>
</form>
</body>
</html>
 
$filename (as used in $_FILES) needs to be the NAME of the file control in your html form. that looks like the problem.
 
Wait I see where your coming from instead of doing $_FILES[$filename] I should be doing $_FILES['datafile']. IF that is what you mean, I have already tried that, my errors then will say datafile rather than the name of the file like I am doing in my code.
 
Yes, you do need to be using $_FILES['datafile']. However, that's not all. You also need to add an enctype="multipart/form-data" attribute to your form tag. Otherwise, PHP isn't going to realize it's supposed to be a file upload.
 
bear in mind as well that not all browsers post the file name in the POST vars. in fact i'm not sure that many do.
 
Thanks AdaHacker, I figured that out after looking more closely at the example i used to create my upload form. Also I disabled notices in php.ini, and made sure my permissions on the folder i am uploading to are correct. It is working now, finally.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top