Reputation: 829
I'm new to PHP and I wrote a simple code in PHP for file upload.but my code gives me an error.can someone help me to find the error
here is my code
error gives as Undefined index
<?php
$name=$_FILES["file"]["name"];
$size=$_FILES['file']['size'];
$type=$_FILES['file']['type'];
?>
<form action="unset.php"method="POST">
<input type="file" name="file"><br><br>
<input type="submit" value="submit">
</form>
Upvotes: 1
Views: 107
Reputation: 2509
try this it wont give you undefined index Error
<?php
if(isset($_FILES) && $_SERVER['REQUEST_METHOD']=='POST')
{
$name=$_FILES["file"]["name"];
$size=$_FILES['file']['size'];
$type=$_FILES['file']['type'];
}
?>
<form action="unset.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file"><br><br>
<input type="submit" value="submit">
</form>
Upvotes: 1
Reputation: 303
You should use enctype="multipart/form-data" in the form tag like
<form action="unset.php" method="POST" enctype="multipart/form-data">
Note:
The enctype attribute of the tag specifies which content-type to use when submitting the form. "multipart/form-data" is used when a form requires binary data, like the contents of a file, to be uploaded
The type="file" attribute of the tag specifies that the input should be processed as a file. For example, when viewed in a browser, there will be a browse-button next to the input field
Upvotes: 0
Reputation: 301
Try this: PHP
<?php
if(isset($_POST['submit'])){
echo $name=$_FILES["file"]["name"];
echo $size=$_FILES['file']['size'];
echo $type=$_FILES['file']['type'];
}
?>
HTML
<form action="unset.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file"><br><br>
<input type="submit" name="submit" value="submit">
</form>
Upvotes: 1
Reputation: 8369
You have to use enctype attribute of <form>
which specifies how the form-data should be encoded when submitting it to the server.
<form action="unset.php" method="POST" enctype="multipart/form-data">
Also try to access FILES
variables only if file is uploaded like
if(isset($_FILES))
{
$name=$_FILES["file"]["name"];
$size=$_FILES['file']['size'];
$type=$_FILES['file']['type'];
}
Upvotes: 2