New in php, i'm trying to experiment file upload mechanism at VERY BASIC level (not trying to test file size etc.) I wrote a file upload function, testing it with if-else. Function works (uploads file successfully)' but still echoes the error string in else clause. Sure i'm missing something but cannot find out what. Code is like this:
<?php
function fileupload() {
$path = "img/";`enter code here`
$tmp = $_FILES['upload']['tmp_name'];
$name = $_FILES['upload']['name'];
move_uploaded_file($tmp,$path.$name);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>UNTITLED</title>
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="upload">
<input type="submit" name="submit" value="submit">
</form>
<?php
if(isset($_POST['submit'])) {
if(fileupload()) {echo "upload ok";} else {echo "error";}
} else { echo "no submit"; }
?>
</body>
</html>
fileupload()is a null return function, so of course yourifstatement always returns false. I think you want something likereturn move_uploaded_file($tmp,$path.$name);to check if it suceeded