我编写了以下代码,用于验证用户上传的图像。但是,当代码运行时,它返回一个空白页面。这是我的代码:
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 <form>
元素是正确的,这就是为什么我没有在这里包含它们。问题可能是什么想法?提前谢谢!
注意:目录$picturelocation
实际上并不存在。 move_uploaded_file()
是否可以动态创建它,如果它不存在?在文档中找不到任何相关内容。
答案 0 :(得分:1)
文件信息可从Super global $ _FILES获得。您可以从以下列表中获取名称,类型,大小和文件的信息。
$_FILES['userfile']['name']
$_FILES['userfile']['type']
$_FILES['userfile']['size']
$_FILES['userfile']['tmp_name']