The current issue is, you are calling the handleUpload() function in another place than the example usage I gave. It is this second call to the function that's causing the current error, because your code is based on the function only returning the message, which it is no longer doing. You must alter your code to just call the function once, then use the result object that is returned from that function. This will require that you reorganize your code.
Your form processing code needs to do these things -
1) check that a post method form was submitted
-- 2) check the uploaded file for errors
---- 3) validate the uploaded file information - extension, mime-type, file size
------ 4) move the uploaded file
-- 5) validate the $_POST form field values
-- 6) if there are no errors in any of the above, insert the data into the database
I looked at your code more thoroughly, to see what it would take to show you the suggestions that I made, and the code apparently started out as all main code, but got some function definitions thrown around sections of it, and now you are trying to make it 'work'. This is not how to design code. When you design a function, you need to define what the inputs to the function are, what processing the function will do, and what result the function will produce and return to the calling code.
Specifically, for your handleUpload function -
1) The input(s) to this function should be the $_FILES['file_upload'] array. This will make the function general purpose and allow it to operate on any uploaded file, regardless of the name that was given to the file form field.
2) The function should only handle moving the uploaded file. Checking the file size and checking the file extension/mime-type are validation steps and should be complete before ever calling the handleUpload() function.
3) Since moving the file can either be successful or it can fail, and you want to return two different failure messages, you need a method of returning more than one value. This is where the result object class comes in.
Your function definition should look more like this -
function handleUpload($f) {
$flag = false;
$uploadedTempFile = $f['tmp_name'];
$filename = basename($f['name']);
$destFile = UPLOAD_DIRECTORY . $filename;
if(!is_uploaded_file($uploadedTempFile)){
// this actually means someone got a file into the uploaded temp folder without uploading it using the form
$response = 'Error: the file you tried to upload is not a valid uploaded file.';
} else {
$success = move_uploaded_file($uploadedTempFile, $destFile);
if ($success) {
$flag = true;
// $response = 'The file was uploaded successfully!'; // a true status value means the file was moved, any success message should be handled by the calling code
} else {
$response = 'An unexpected error occurred; the file could not be uploaded.'; // this is usually due to a path problem or a permission problem
trigger_error('call to move uploaded file failed'); // this is an internal error and you should log all available information about it. i have shown only a simple message in this example.
}
}
return new function_result($flag,$response);
}
You would call it and use the returned information like this (this example code uses the $errors array as described in reply #7 in this thread) -
// if all the upload file validation tests passed, you can do something with the uploaded file
if(empty($errors)){
$upload = handleUpload($_FILES['file_upload']); // pass the array of uploaded file info to the function
if(!$upload->status)
{
// handling the file failed
$errors['upload'][] = $upload->message; // add the message that the function produced to the $errors array.
}
}
If this step fails, an error is added to the $errors array. This would be used later when deciding to insert the data into the database -
// done with all validation, if no errors use the form data
if(empty($errors))
{
// the code for the INSERT query would go here...
}