2
<form enctype="multipart/form-data" method="post" action="upload2.php">
    Send this file: <input name="userfile" type="file" /><br />
    <input type="submit" value="Send File" />
</form>
<?php
  if (move_uploaded_file($_FILES['userfile']['tmp_name'], "C:\wamp64\www\project")) {
        print "Received {$_FILES['userfile']['name']} - its size is {$_FILES['userfile']['size']}";
    } else {
        print "Upload failed!";
    }
?>

I am having trouble with uploading a file in PHP.I have been trying to make a form for uploading files.Each time I get the below error.

error: "Undefined index: userfile in C:\wamp64\www\project\firstfile.php " .Any solutions ?

TIGER
  • 2,864
  • 5
  • 35
  • 45
Danelo
  • 103
  • 2
  • 11

4 Answers4

4

undefined index userfile, that means when the page loads there is no $_FILES['imageupload'] you need to submit the page to have that variable

<?php
if(isset($_POST['save'])){
    $path="upload/";
    $name = $_FILES['imageupload']['name'];//Name of the File
    $temp = $_FILES['imageupload']['tmp_name'];
    if(move_uploaded_file($temp, $path . $name)){
        echo "success";
    }else{
        echo "failed";
    }
}
?>
<form method="post" action="" enctype="multipart/form-data">
    <input type="file" name="imageupload">
    <input type="submit" name="save" value="submit">
</form>
Sanooj T
  • 1,317
  • 1
  • 14
  • 25
  • Thank you.But there is one more problem.The images will always be saved to the main wamp folder and never to the specified path. How I solve that ? – Danelo Nov 25 '16 at 09:24
  • You need to change the path lol Look up move_uploaded_file() on php.net – Bernhard Nov 25 '16 at 09:44
2
 Please try this  
<?php
        if(isset($_POST['save'])){
            $path = __DIR__ . "/uploads/";
            $img_name = $_FILES['imageupload']['name'];
            $temp_name = $_FILES['imageupload']['tmp_name'];
            if(move_uploaded_file($temp_name, $path . $img_name)){
                echo "uploaded";
            }else{
                echo "failed";
            }
        }
        ?>
        <form method="post" action="" enctype="multipart/form-data">
            <input type="file" name="imageupload">
            <input type="submit" name="save" value="submit">
        </form>
Shanu k k
  • 1,235
  • 2
  • 18
  • 43
2

try this,

<form enctype="multipart/form-data" method="post" action="">
    Send this file: <input name="userfile" type="file" /><br />
    <input type="submit" name="btn_submit" value="Send File" />
</form>

<?php
   if(isset($_POST['btn_submit'])){

      $img_name = $_FILES['userfile']['name'];//name of image
      $tmp_name = $_FILES['userfile']['tmp_name'];

  if (move_uploaded_file($tmp_name, "upload/".$img_name)) {
       //your message
    } else {
       //message
    }
?>
Dhaval Naphade
  • 555
  • 2
  • 21
0
<?php
if(isset($_POST['upload'])){
    $path="uploaddir/";
    $name = $_FILES['userfile']['name'];
    $temp = $_FILES['userfile']['tmp_name'];
    if(move_uploaded_file($temp, $path . $name)){
        echo "success";
    }else{
        echo "failed";
    }
}
?>
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
    <input type="file" name="userfile" />
    <input type="submit" name="upload" value="submit" />
</form>
Deep Kakkar
  • 5,831
  • 4
  • 39
  • 75