0

I don't get it.... I'm trying to make an upload script. While it's working fine I was now trying to imply some error handling. But one thing is making me crazy. I just want to check, if a value is NOT in an array.

But I'm always getting a blank page. Since my English isn't the best, (and I hope I can explain my problem good enough), I will show a small part with the first if-statement:

session_start();
$errors = array();

$formData = $_POST;

function getFile( $filename , $formData ) {

    $allowedExts = array("pdf");
    $temp = explode(".", $_FILES["uploaded_file"]["name"]);
    $extension = end($temp); // gives me "pdf"
    $mimes = array('application/pdf');

    if (!in_array($_FILES['uploaded_file']['type'],$mimes ) || 
        !in_array($extension, $allowedExts, true))
    {
        $errors['fileformat'] = "Wrong file format!";
    }
    if(count($errors) === 0){
        sendMailWithAttachment($_FILES["uploaded_file"]["tmp_name"],
                                $_FILES["uploaded_file"]["name"],
                                $formData); //this function is working... 
    }else{
        $_SESSION['error'] = $errors;
        header("Location: no_success.php");
        exit();
    }
}

$name_of_uploaded_file =basename($_FILES['uploaded_file']['name']);
getFile( $name_of_uploaded_file, $formData ); 

So this code (above) gives me a blank page. If I JUST change the "in_array"-line to:

if (in_array($_FILES['uploaded_file']['type'],$mimes ) || 
    in_array($extension, $allowedExts))
{

So without the ! at the beginning. Everything is working (except from the fact, that I need the opposite result)

I also tried:

if ( in_array($_FILES['uploaded_file']['type'],$mimes) === false ||
     in_array($extension, $allowedExts, true) === false){

I also tried:

if ( in_array($_FILES['uploaded_file']['type'],$mimes) == false ||
     in_array($extension, $allowedExts, true) == false)
{

I also tried:

if (in_array($_FILES['uploaded_file']['type'],$mimes ) === 0 || 
    in_array($extension, $allowedExts, true) === 0)
{

I also tried:

if (in_array($_FILES['uploaded_file']['type'],$mimes )  == 0|| 
    in_array($extension, $allowedExts, true) == 0)
{

And all of this variants I also tried just with one if-statement, like:

if (in_array($extension, $allowedExts, true) === 0) {

All of them are making me getting a blank page. I don't understand this, because I just want to get the opposite function of "in_array" and the "in_array" itself just works fine.

Hope someone can help me. Thank you

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
crally
  • 29
  • 3

1 Answers1

1

Thanks to all of u.

@Anant This

error_reporting(E_ALL);ini_set('display_errors',1);

gaves me the hind....

It said, that the var $errors is undefined. So it seems, that the variable isn't visible by the function.

I just put this

$errors = array();

in the function block and everything is working as expected now.

Thanks again. I will remember the error_reporting thing in future.

crally
  • 29
  • 3