Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
    
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? –  Blowski Jan 16 '14 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. –  Fred -ii- Jan 16 '14 at 21:16
    
İt is in the same file and the same php tag :) –  ekrem777 Jan 16 '14 at 21:17
    
No I am calling this function after defining it. –  ekrem777 Jan 16 '14 at 21:17
    
Sidenote: This $pathOfFile = "/images/"; may fail. You may need to use a relative path. I.e.: $pathOfFile = "images/"; or $pathOfFile = "../images/"; –  Fred -ii- Jan 16 '14 at 21:18

2 Answers 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;
}
share|improve this answer
    
Again the same error. I will just delete the function and do it within main php with the help of if statement. –  ekrem777 Jan 16 '14 at 21:31
//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)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.