【问题标题】:php - File Upload returns blank pagephp - 文件上传返回空白页面
【发布时间】:2015-09-16 04:47:57
【问题描述】:

我编写了以下代码来验证用户上传的图像。但是,当代码运行时,它会返回一个空白页。这是我的代码:

HTML<form> 标题):

<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
);

?>

出于测试目的,我添加了标题。然而,这些都没有被执行。相反,如上所述,每次都显示一个空白页面。 HTML &lt;form&gt; 元素是正确的,这就是我没有在此处包含它们的原因。问题可能出在哪里?提前致谢!

注意:目录$picturelocation 实际上并不存在。 move_uploaded_file() 如果不存在,是否可以动态创建它?在文档中找不到关于此的任何内容。

【问题讨论】:

  • $userDataArray 的输出是什么?我对 $picturelocation 变量的输出特别感兴趣。
  • 尝试在页面上的所有内容上方添加ini_set("display_errors",1); error_reporting(E_ALL);。您可能有错误。
  • @Rasclatt 感谢您的回复。刚刚这样做,但页面仍然保持空白。
  • 很难说;无法查看输入是否具有正确的名称属性。空白页通常意味着语法错误。你从哪里运行这个?本地/托管?
  • @satroy 你是说 $picture 吗?输出为 array(5) { ["name"]=> string(23) "IMG-20150531-WA0008.jpg" ["type"]=> string(10) "image/jpeg" ["tmp_name"]= > string(36) "/Applications/MAMP/tmp/php/phpwFtPsv" ["error"]=> int(0) ["size"]=> int(344860) }

标签: php fileinfo


【解决方案1】:

文件信息可从超级全局 $_FILES 中获取。因此您可以从以下列表中获取名称、类型、大小和文件的信息。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-13
    • 2012-11-27
    • 1970-01-01
    • 2012-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多