-4
<!DOCTYPE html>
<html>

<head>
    <title>
         Move a file into a different
         folder on the server
    </title>
</head>
  
<body>
    <form action="" method="post"
            enctype="multipart/form-data">
          
        <input type="file" name="file1" id="file1">
        <input type="file" name="file2" id="file2"> 
        <br><br>
          
        <input type="submit" name="submit" value="Submit">
    </form>
</body>
</html>
<?php
$target_dir1 = "Uploads/Profile/";
$target_dir2 = "Uploads/sign/";
$target_file1 = $target_dir1 . basename($_FILES["file1"]["name"]);
$target_file2 = $target_dir2 . basename($_FILES["file2"]["name"]);
$uOk = 1;
  
if(isset($_POST["submit"])) {
      
    // Check if file already exists
    if (file_exists($target_file1  && $target_file2 )) {
        echo "file already exists.<br>";
        $uOk = 0;
    }
      
    // Check if $uOk is set to 0 
    if ($uOk == 0) {
        echo "Your file was not uploaded.<br>";
    } 
      
    // if uOk=1 then try to upload file
    else {
          
        // $_FILES["file"]["tmp_name"] implies storage path
        // in tmp directory which is moved to uploads
        // directory using move_uploaded_file() method
        if (move_uploaded_file($_FILES["file1"]["tmp_name"],
                                            $target_file1)) {
            echo "The file ". basename( $_FILES["file1"]["name"])
                        . " has been uploaded.<br>";
              
            }
            else {
                echo "File moving operation failed..<br>";
            }
        }
        if (move_uploaded_file($_FILES["file2"]["tmp_name"],
                                            $target_file2)) {
            echo "The file ". basename( $_FILES["file2"]["name"])
                        . " has been uploaded.<br>";
              
            }
            else {
                echo "File moving operation failed..<br>";
            }
    }

?>

I'm trying to upload two input files in different directories. There is no problem in uploading files, code work properly but the problem is in html page it show:

Notice: Undefined index: file1 in

Notice: Trying to access array offset on value of type null in Notice: Undefined index: file2 in

Notice: Trying to access array offset on value of type null in

DarkBee
  • 16,592
  • 6
  • 46
  • 58
dorami
  • 37
  • 4
  • 1
    Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-warning-undefined-arr) – CherryDT Oct 13 '22 at 08:17
  • 1
    You are accessing `$_FILES["file1"]["name"]` *before* doing any POST/upload resulting in these notices, because before the POST, these simply don't exist – DarkBee Oct 13 '22 at 08:18

1 Answers1

0

Just move these two lines inside if condition like below:

$target_file1 = $target_dir1 . basename($_FILES["file1"]["name"]);
$target_file2 = $target_dir2 . basename($_FILES["file2"]["name"]);

<!DOCTYPE html>
<html>

<head>
  <title>
    Move a file into a different
    folder on the server
  </title>
</head>

<body>
  <form action="" method="post" enctype="multipart/form-data">

    <input type="file" name="file1" id="file1">
    <input type="file" name="file2" id="file2">
    <br><br>

    <input type="submit" name="submit" value="Submit">
  </form>
</body>

</html>
<?php
$target_dir1 = "Uploads/Profile/";
$target_dir2 = "Uploads/sign/";

$uOk = 1;

if (isset($_POST["submit"])) {

  $target_file1 = $target_dir1 . basename($_FILES["file1"]["name"]);
  $target_file2 = $target_dir2 . basename($_FILES["file2"]["name"]);

  // Check if file already exists
  if (file_exists($target_file1  && $target_file2)) {
    echo "file already exists.<br>";
    $uOk = 0;
  }

  // Check if $uOk is set to 0 
  if ($uOk == 0) {
    echo "Your file was not uploaded.<br>";
  }

  // if uOk=1 then try to upload file
  else {

    // $_FILES["file"]["tmp_name"] implies storage path
    // in tmp directory which is moved to uploads
    // directory using move_uploaded_file() method
    if (move_uploaded_file(
      $_FILES["file1"]["tmp_name"],
      $target_file1
    )) {
      echo "The file " . basename($_FILES["file1"]["name"])
        . " has been uploaded.<br>";
    } else {
      echo "File moving operation failed..<br>";
    }
  }
  if (move_uploaded_file(
    $_FILES["file2"]["tmp_name"],
    $target_file2
  )) {
    echo "The file " . basename($_FILES["file2"]["name"])
      . " has been uploaded.<br>";
  } else {
    echo "File moving operation failed..<br>";
  }
}

?>

When PHP script is executed it does not find these file1 and file2 variable values that's why those warnings showing....once the form submitted then it gets value.

DCodeMania
  • 1,027
  • 2
  • 7
  • 19