简体   繁体   中英

php - File Upload returns blank page

I wrote the following code that should validate an image uploaded by a user. However, when the code is run, it returns a blank page. Here my code:

HTML ( <form> headline):

<form class="registerbodysections" enctype="multipart/form-data" method="post" action="php/registrationForm.php">

PHP :

<?php

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$confirmemail = $_POST['confirmemail'];
$birthday = $_POST['birthday'];
$birthmonth = $POST['birthmonth'];
$birthyear = $_POST['birthyear'];
$location = $_POST['location'];
$picture = $_FILES['picture'];
$picturelocation = "../img/uploads/'$email'/picture/'$picture[\'type\']";

echo var_dump($picture); //for testing purposes, but does not get executed

function imageFormatCheck ($fileMime) {
    $datatypes = array(
    'image/jpg',
    'image/gif',
    'image/png',
    'image/jpeg'
    );

    foreach($datatypes as $datatype) {
        if ($fileMime !== $datatype) {
            return false;   
        }
    }
    return true;
}

function checkFileType() {
    if(imageFormatCheck($fileMime)) {
        header('Location: http://google.com');
        exit();
    } else {
        header('Location: http://facebook.com');   
        exit();
    }
}

function moveFile() {
    if(!move_uploaded_file($tmpName, $picturelocation)) {
        header('Location: http://google.com');
        exit();
    } else {
        header('Location: http://facebook.com');
        exit();
    }   
}


if(count($picture)) {

    $tmpName = $picture['tmp_name']; //when I var_dump $picture I receive a single array, not a double array as often seen
    $fInfo = $finfo_open(FILEINFO_MIME_TYPE);
    $fileMime = finfo_file($fInfo, $tmpName);
    $finfo_close($fInfo);

    checkFileType();
    moveFile();

}

$userDataArray = array(
    $firstname,
    $lastname,
    $birthday,
    $birthmonth,
    $birthyear,
    $location,
    $picturelocation
);

?>

I added the headers for testing purposes. However, none of these ever get executed. Instead, as mentioned above, a blank page is displayed every time. The HTML <form> elements are correct that's why I didn't include them here. Any ideas where the problem could be? Thanks in advance!

NOTE: the directory $picturelocation doesn't actually exist. Does move_uploaded_file() maybe create this dynamically if it doesn't exist? Couldn't find anything on this in the documentation.

Files information are available from the Super global $_FILES.So you can get the information of name,type,size and file from the following list.

$_FILES['userfile']['name']
$_FILES['userfile']['type']
$_FILES['userfile']['size']
$_FILES['userfile']['tmp_name']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM