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.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
ekrem777
  • 35
  • 2
  • 8
  • 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? – Dan Blows 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. – Funk Forty Niner 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/";` – Funk Forty Niner Jan 16 '14 at 21:18
  • I'm a bit rusty on PhP, but are you allowed to have a function that may or may not have a return value? If not, that could be a problem (if not the specific one you're facing) – StephenTG Jan 16 '14 at 21:19
  • Well, I ran your code in the correct order and the function was called. You must be doing something mad. – Drumbeg Jan 16 '14 at 21:20
  • I tried "images/" version also but problem occurs before going in the function itself. – ekrem777 Jan 16 '14 at 21:20
  • ^-- That's because the original problem hasn't been dealt with yet. – Funk Forty Niner Jan 16 '14 at 21:22
  • another side note: `$fileResult` seems to be finding from inside the `fileUploaded` but you are trying to use it outside of the function scope... its better to just `return move_uploaded_file($tmpLoc, $filePath);`. EDIT: To the actual problem, how are loading the `fileUpload` function? if your function is in "functions.php" (or whatever name), just `include('functions.php');` – Gasim Jan 16 '14 at 21:22
  • Yes php does only say that problem is "Fatal Error:Call to undefined function fileUploaded() in ..." – ekrem777 Jan 16 '14 at 21:23

2 Answers2

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;
}
R3tep
  • 12,512
  • 10
  • 48
  • 75
  • 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
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)

Gasim
  • 7,615
  • 14
  • 64
  • 131