我在使用Vagrant(hashicorp / precise32)的archlinux上我从w3c-school学习php,我在页面上传文件,这里我的脚本无法上传任何东西,唯一让我回复的东西是无效文件(.jpg .png ecc ..)这里是代码:
class User
{
//init Db Variable
private $db;
/*
* Constuctor
*/
public function __construct()
{
$this->db = new Database;
}
/*
* Upload User Avatar
*/
public function uploadAvatar()
{
$allowedExts = array("gif", "GIF", "jpeg", "JPEG", "jpg", "JPG", "png", "PNG");
$extension = end(explode(".", $_FILES["avatar"]["name"]));
if ((($_FILES["avatar"]["type"] === "image/gif")
|| ($_FILES["avatar"]["type"] === "image/jpeg")
|| ($_FILES["avatar"]["type"] === "image/jpg")
|| ($_FILES["avatar"]["type"] === "image/pjpeg")
|| ($_FILES["avatar"]["type"] === "image/x-png")
|| ($_FILES["avatar"]["type"] === "image/png")) && ($_FILES["avatar"]["size"] < 20000000 ) && in_array($extension, $allowedExts)) {
if ($_FILES["avatar"]["error"] > 0) {
redirect('register.php', $_FILES["avatar"]["error"], 'error');
} else {
if (file_exists('img/avatars/' . $_FILES["avatar"]["name"])) {
redirect('register.php', 'File already exists', 'error');
} else {
move_uploaded_file($_FILES["avatar"]["tmp_name"],
"img/avatars/" . $_FILES["avatar"]["name"]);
return true;
}
}
} else {
redirect('register.php', 'Invalid File Type!', 'error');
}
}
}
html fom Bootstap寄存器
<form ectype="multipart/form-data" method="POST" action="register.php">
<div class="form-group">
<input type="file" name="avatar" class="btn btn-sm btn-primary" />
</div>
</form>
register.php
<?php
// Create User Object
$user = new User;
if (isset($_POST['register'])) {
//Create Data away
$data = array();
//Upload Avatar Image
if ($user->uploadAvatar()) {
return $data['avatar'] = $_FILES["avatar"]["name"];
} else {
$data['avatar'] = 'gavatarm.png';
}
}
答案 0 :(得分:0)
简单!修正您的拼写错误,使用 enctype 而不是 ectype 。
<form enctype="multipart/form-data" method="POST" action="register.php">