Reputation: 307
I've written code for upload file to server like below.
Home.tpl
<form enctype="multipart/form-data" method="post" action="upload_file.php" >
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
upload_file.php
if(($_FILES["file"]["size"] > 0))
{
$fileName = $_FILES["file"]["name"];//the files name takes from the HTML form
$fileTmpLoc = $_FILES["file"]["tmp_name"];//file in the PHP tmp folder
$fileType = $_FILES["file"]["type"];//the type of file
$fileSize = $_FILES["file"]["size"];//file size in bytes
$fileErrorMsg = $_FILES["file"]["error"];//0 for false and 1 for true
$target_path = "uploads/" . basename( $_FILES["file"]["name"]);
$moveResult = move_uploaded_file($fileTmpLoc, $target_path);
}
But i getting 'Undefined index: file' error. please help me to get rid from there.
Upvotes: 0
Views: 4161
Reputation: 11
i think its $_FILES["file"]["tmp_name"][0] for the first file. check it with var_dump($_FILES); or print_r($_FILES) then iterate throu it with an foreach. something like
foreach ($arr as &$value) {
//...
$moveResult = move_uploaded_file($fileTmpLoc, "uploads/" . basename( $_FILES["file"]["name"][$arr]);
}
Upvotes: 0
Reputation: 9527
If you are getting error every time, its because you dont have set variable $_FILES. That variable is set only after you submit your form.
In that case, to get rid of error mesage, add control of variable:
if((isset($_FILES["file"])) && ($_FILES["file"]["size"] > 0))
Upvotes: 1
Reputation: 6202
change
if(($_FILES["file"]["size"] > 0))
to
if((isset($_FILES["file"]["size"]) && $_FILES["file"]["size"] > 0))
Upvotes: 0