简体   繁体   中英

I am getting an undefined index tmp_name notice

I am trying to insert the name of the file into the "AudioFile" column in the "Audio" Table. Problem is that it keeps giving me an undefined index error: tmp_name line 45. This means when I try to insert the name of a file, instead of doing the below:

Audio

AudioId (auto)  AudioFile
1               AudioFiles/cat.png
2               AudioFiles/cat_2.png
3               AudioFiles/cat_3.png

It is doing this below:

Audio

AudioId (auto)  AudioFile
1               AudioFiles/_2.png
2               AudioFiles/_2.png
3               AudioFiles/_2.png

So my question is why can't it find the tmp name and hence inserts incorrect file name in the table:

$result = 0;



if( file_exists("AudioFiles/".$_FILES['fileAudio']['name'])) {
    $parts = explode(".",$_FILES['fileAudio']['name']);
    $ext = array_pop($parts);
    $base = implode(".",$parts);
    $n = 2;

    while( file_exists("AudioFiles/".$base."_".$n.".".$ext)) $n++;
    $_FILES['fileAudio']['name'] = $base."_".$n.".".$ext;

    move_uploaded_file($_FILES["fileAudio"]["tmp_name"],  //line 45 notice appears
    "AudioFiles/" . $_FILES["fileAudio"]["name"]);
    $result = 1;

}
    else
      {
      move_uploaded_file($_FILES["fileAudio"]["tmp_name"],
      "AudioFiles/" . $_FILES["fileAudio"]["name"]);
      $result = 1;
  }

    $audiosql = "INSERT INTO Audio (AudioFile) 
    VALUES (?)";

        if (!$insert = $mysqli->prepare($audiosql)) {
      // Handle errors with prepare operation here
    }

            //Dont pass data directly to bind_param store it in a variable
$insert->bind_param("s",$aud);

//Assign the variable
$aud = 'AudioFiles/'.$_FILES['fileAudio']['name'];

 $insert->execute();

        if ($insert->errno) {
          // Handle query error here
        }

        $insert->close();

Below is the form:

 var $fileAudio = $("<form action='audioupload.php' method='post' enctype='multipart/form-data' target='upload_target_audio' onsubmit='return audioClickHandler(this);' class='audiouploadform' >" + 

    "<p class='audiof1_upload_process' align='center'>Loading...<br/><img src='Images/loader.gif' /><br/></p>" +

    "<p class='audiof1_upload_form' align='center'><br/><span class='audiomsg'></span><label>" + 

    "Audio File: <input name='fileAudio' type='file' class='fileAudio' /></label><br/><br/><label class='audiolbl'>" + 

    "<input type='submit' name='submitAudioBtn' class='sbtnaudio' value='Upload' /></label>" + 

    "<iframe class='upload_target_audio' name='upload_target_audio' src='#' style='width:300px;height:300px;border:0px;solid;#fff;'></iframe></form>"); 

IT's quite hard to quess which line is 45, but rule of thumb here is that if it says that index in undefined then it really is. There are two common reasons: you made typo in index name trying to read it, or it simply is not there. You can easily check what indexes are present by printing array content out with print_r() or even var_dump() . Then you either fix your typo or try to find why it is not here (if it really should be unconditionally). I'd also check your <FORM> and see if your <input type="file"... /> element is really named and its name is "fileAudio".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM