1

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.

11
  • When you say 'I have this function', where is it written? In the same file, or a different file? If it's in a different file, are you definitely including it? Commented Jan 16, 2014 at 21:14
  • If you're calling the function "before" the function, then that will throw you that error. It depends on where it's placed. Show us the full code in order to see where it's placed, IF it's inside the same file. Commented Jan 16, 2014 at 21:16
  • İt is in the same file and the same php tag :) Commented Jan 16, 2014 at 21:17
  • No I am calling this function after defining it. Commented Jan 16, 2014 at 21:17
  • Sidenote: This $pathOfFile = "/images/"; may fail. You may need to use a relative path. I.e.: $pathOfFile = "images/"; or $pathOfFile = "../images/"; Commented Jan 16, 2014 at 21:18

2 Answers 2

2

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Again the same error. I will just delete the function and do it within main php with the help of if statement.
1
//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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.