Orienta
Orienta

Reputation: 5

Why is the following code throws undefined index while uploading file in PHP?

Marking the question as duplicate is futile, cause I've referred all such questions on SO and none of them did provide a solution.

<?php 
if(isset($_POST['submit'])){
$name=$_FILES['filedoc']['name'];  
$temp_name=$_FILES['filedoc']['tmp_name'];  
if(isset($name)){
    if(!empty($name)){      
        $file=file_get_contents($temp_name);
        }
    }
else echo"Please upload file";

echo "<form action=\"";echo htmlentities($_SERVER["PHP_SELF"]);echo "\" method=\"post\">
    <h2>New Paste</h2>
    <label> Upload File?  <input type = \"file\" name = \"filedoc\"/></label><br><span class=\"error\">";echo $fileErr;echo"</span><br>
    <input id=\"button\"class=\"red\" type =\"submit\" class=\"red\" name=\"submit\" value = \"Paste\"/><br><span class=\"error\">";echo $submitErr;echo "</span>
    </form>";
}
?>

Form is being displayed correctly. So there are no errors in the second part. But then, I get this error ( ! ) Notice: Undefined index: filedoc in path of the file

Upvotes: 0

Views: 42

Answers (2)

user7915756
user7915756

Reputation:

The form also needs the following attribute: enctype="multipart/form-data". It specifies which content-type to use when submitting the form Without the requirement above, the file upload will not work.

Upvotes: 0

Dileep Kumar
Dileep Kumar

Reputation: 1107

form attribute enctype="multipart/form-data" is missing

Upvotes: 2

Related Questions