-1

I am trying to upload a song to a database using PHP, just like soundcloud web. But it gives me error

Notice: Undefined index: file in C:\Xampp\htdocs\aksongs\upload.php on line 25

Notice: Undefined index: file in C:\Xampp\htdocs\aksongs\upload.php on line 26

Here is the code of file

<?php
include_once 'connection.php';
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>AK music</title>
  <link rel="stylesheet" href="main.css" href="main.js" >

</head>
<body>
 <form enctype="multipart/form-data" action="upload.php" method="POST">
 Please choose a file: <input name="file" type="file" /><br />
 <input type="submit" value="submit" />
 </form>
 <?php
 $name = $_FILES['file']['name'];
$tmp_name = $_FILES['file']['tmp_name'];
 
 if(isset($name)){
   if(!empty($name)){
  $location = 'aksongs/upload';
  if(move_uploaded_file($tmp_name,$location.$name)){
   echo 'uploaded';
   }
  
  }else{
   echo 'Please choose a file';
   
   }
  
  }
 ?> 
</body>
</html>

1 Answers1

0

Your code should be like this

<?php  if(isset($_FILES['file']['name']) && $_FILES['file']['name']!=''){
$name = $_FILES['file']['name'];
$tmp_name = $_FILES['file']['tmp_name'];


  if(!empty($name)){
      $location = 'aksongs/upload';
      if(move_uploaded_file($tmp_name,$location.$name)){
        echo 'uploaded';
     }

 }else{
     echo 'Please choose a file';
}}else{
echo 'Please choose a file';

} ?>

M Umair
  • 34
  • 1
  • 3
  • Could you explain to him why it should look like this? – moorscode Nov 15 '15 at 14:28
  • When page load it find the $_FILES['file']['name'] which is not defined yet. And your php notice are enable that's why he shows undefined variable file – M Umair Nov 15 '15 at 14:43