1

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>
1
  • fileupload() is a null return function, so of course your if statement always returns false. I think you want something like return move_uploaded_file($tmp,$path.$name); to check if it suceeded Commented Sep 6, 2015 at 17:17

3 Answers 3

1

fileupload() function doesn't return any Boolean you have to check if move_uploaded_file($tmp,$path.$name); succeeded not the function it self...

function fileupload() {
    $path = "img/";`enter code here`
    $tmp = $_FILES['upload']['tmp_name'];
    $name = $_FILES['upload']['name'];
    if (move_uploaded_file($tmp,$path.$name)) {
     return true;
    } else {
     return false;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

This is because the fileupload() function doesn't return anything, making the if(fileupload()) condition fail, and thereby going to the else block. The move_uploaded_file() function returns true on success, and false otherwise. You can use this on your function to return the success status.

<?php 
function fileupload() {
    $path = "img/";`enter code here`
    $tmp = $_FILES['upload']['tmp_name'];
    $name = $_FILES['upload']['name'];

    // Return the success status
    return move_uploaded_file($tmp,$path.$name);
}
?>

Comments

1

To fix the exact error you're getting, you should check my comment on why it's not working fileupload() returns null. To get a return notifying if the move_uploaded_file worked, you should change your function to this:

function fileupload() {
    $path = "img/";`enter code here`
    $tmp = $_FILES['upload']['tmp_name'];
    $name = $_FILES['upload']['name'];
    return move_uploaded_file($tmp,$path.$name); //This is a bool type return
}

However, that might not be the only place the fileupload() fails, or the file upload in general fails. You should debug the whole file upload process first before trying to move a .tmp file on the server which may not even be there. You can debug this built-in PHP errors for file uploading by accessing the $_POST['filename']['error'], which stores all errors from the file upload.

http://php.net/manual/en/features.file-upload.errors.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.