我试图让用户能够为他们的帐户上传自己的个人资料照片,但他们上传的照片似乎永远找不到正确的目的地。我已经关注了TutorialsPoint的教程,一些不同的StackOverflow线程,并进入我的php.ini文件来配置它以允许文件上传。
以下是我用于表单的删节代码
<form method="post" action="db/update-account" enctype="multipart/form-data">
<input type="file" name="profPic" accept="image/jpeg, image/png">
<input type="submit" value="Save Changes"/>
这就是PHP脚本的样子
if(isset($_FILES['profPic'])) {
$errors = array();
$file_name = $_FILES['profPic']['name'];
$file_size = $_FILES['profPic']['size'];
$file_tmp = $_FILES['profPic']['tmp_name'];
$file_type = $_FILES['profPic']['type'];
$file_ext = strtolower(end(explode('.',$_FILES['profPic']['name'])));
$extensions = array("jpeg","jpg","png");
if(in_array($file_ext,$extensions) === false) {
$errors[] = "extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 2097152) {
$errors[] = 'File size must be less than 2 MB';
}
if(empty($errors) == true) {
move_uploaded_file($file_tmp,"imgs/profile-pics/custom".$file_name);
} else {
print_r($errors);
}
}
任何正确方向的提示都将非常感谢!感谢。