ekrem777
ekrem777

Reputation: 45

Php function calling, Undefined function error

Earlier I have a form that ask user to upload a picture and I have this function:

function fileUploaded() {
        $fileName = $_FILES ['picture'] ['name'];
        $pathOfFile = "/images/";
        $fileTmpLoc = $_FILES ['picture'] ["tmp_name"];
        $fileResult = move_uploaded_file ( $fileTmpLoc, $pathOfFile );
        if (isset ( $fileName )) {
            return true;
        }

}

Basically it moves the uploaded picture to images file. Then I am calling this function in an if statement:

        if (fileUploaded () == true) {
        if ($fileResult) {
          /*checking the size of file*/
            }
        }
       else {
        $fileName = "default.jpg";
       }

After when I try to upload and submit it gives the error in the below:

Fatal error: Call to undefined function fileUploaded()

What should be the problem? Thanks.

Upvotes: 1

Views: 236

Answers (2)

Gasim
Gasim

Reputation: 8001

//functions.php
function fileUpload($path) {
    if(!isset($_FILES['picture'])) return false;
    $fileName = $_FILES['picture']['name'];
    $fileTmpLoc = $_FILES['picture']['tmp_name'];
    if(move_uploaded_file ($fileTmpLoc, $path)) return $fileName;
    return false;
}

//main.php
include('functions.php');

$fileName = fileUpload('/images/');
if($fileName === false) {
   $fileName = 'default.jpg'; 
}

//do the rest here

Something similar to the above code. Since your function is in a different file, you need to include it (or require it)

Upvotes: 1

R3tep
R3tep

Reputation: 12874

You don't return a default value in your function. Maybe it's the problem :

function fileUploaded() {
    $fileName = $_FILES ['picture'] ['name'];
    $pathOfFile = "/images/";
    $fileTmpLoc = $_FILES ['picture'] ["tmp_name"];
    $fileResult = move_uploaded_file ( $fileTmpLoc, $pathOfFile );
    if (isset ( $fileName )) {
        return true;
    }
    return false;
}

Upvotes: 2

Related Questions