我希望用户能够上传以用户名命名的个人资料图片,以便我可以在以后轻松调用它。问题是,没有文件保存在所需的文件夹中。文件夹名称是“profilePictures”,当应该有用户的用户名调用的图片时,它是空的。我试图解决它,但没有运气。我做错了什么?
代码:
<?php
class User {
public function __construct( $username, $password, $passwordRepeat, $email, $firstName, $familyName, $age, $sex, $question, $answer, $car, $profilePicture ) {
$this->username = $username;
$this->password = $password;
$this->passwordRepeat = $passwordRepeat;
$this->email = $email;
$this->firstName = $firstName;
$this->familyName = $familyName;
$this->age = $age;
$this->sex = $sex;
$this->question = $question;
$this->answer = $answer;
$this->car = $car;
$this->profilePicture = $profilePicture;
}
public function savePicture() {
if ( isset( $_POST["register_new"] ) ) {
$profiles = "profiles/$this->username.txt";
if ( $this->password !== $this->passwordRepeat ) {
} else if ( file_exists( $profiles ) ) {
} else if ( $this->age < 18 ) {
} else {
$pictureDir = "profilePictures/";
$pictureFile = $pictureDir . basename(["profilePicture"]["name"][ $this->username ] );
$pictureTemp = $_FILES["profilePicture"]["name"][ $this->username ];
$pictureType = pathinfo( $pictureFile, PATHINFO_EXTENSION );
$uploadFile = 1;
$check = getimagesize( $pictureTemp );
if ( $check !== false ) {
$uploadFile = 1;
} else {
require "register.html";
echo "<p>File is not an image</p>";
$uploadFile = 0;
}
/*
if (file_exists($pictureFile)) {
echo "<p>File already exists</p>";
}
*/
if ( $_FILES["profilePicture"]["size"] > 200000 ) {
echo "File is too large! We accept files that are less than 2 MB";
$uploadFile = 0;
}
if ($pictureType != "jpg" && $pictureType != "png" && $pictureType != "jpeg" && $pictureType != "gif") {
echo "<p>Sorry, only JPG, JPEG, PNG & GIF files are allowed.</p>";
$uploadFile = 1;
}
if ( $uploadFile == 0 ) {
echo "<p>File was not uploaded</p>";
} else {
move_uploaded_file( $pictureTemp, $pictureFile );
}
}
}
}
public function saveUser() {
if ( isset( $_POST["register_new"] ) ) {
$profiles = "profiles/$this->username.txt";
if ( $this->password !== $this->passwordRepeat ) {
require "register.html";
echo "<p>Password does not match! Please try again</p>";
} else if ( file_exists( $profiles ) ) {
require "register.html";
echo "<p>Username already exists!</p>";
} else if ( $this->age < 18 ) {
require "register.html";
echo "<p>You must be over 18 to use this site</p>";
} else {
$fp = fopen( $profiles, "a+" );
$save = $this->username . " , " . $this->password . " , " . $this->email . " , " . $this->firstName . " , " . $this->familyName . " , " . $this->age . " , " . $this->sex . " , " . $this->question . " , " . $this->answer . " , " . $this->car;
fwrite( $fp, $save );
echo "<p>Successfully registered! Click <a href=index.html>HERE</a> to go back to the login window.</p>";
}
}
}
}
$user = new User( ( $_POST["username_register"] ), ( $_POST["password_register"] ), ( $_POST["password_repeat"] ), ( $_POST["email_register"] ), ( $_POST ["first_name_register"] ), ( $_POST ["last_name_register"] ), ( $_POST["age_register"] ), ( $_POST ["sex_register"] ), ( $_POST ["question_register"] ), ( $_POST ["answer_register"] ), ( $_POST["car_register"] ), ($_POST["profilePicture"]) );
$user->saveUser();
$user->savePicture();
答案 0 :(得分:0)
首先确保您拥有profilePictures
地图的写入权限。假设你有权限,我认为它在这两行上失败了:
$pictureFile = $pictureDir . basename(["profilePicture"]["name"][ $this->username ] );
$pictureTemp = $_FILES["profilePicture"]["name"][ $this->username ];
尝试使用此代替上述两行:
//Do not add the username here, because it isn't defined anywhere.
//Also you didn't define $_FILES here, and basedir isn't neccessary since "name" only contains the filename
$pictureFile = $pictureDir . $_FILES["profilePicture"]["name"];
//To get the current picture use tmp_name
$pictureTemp = $_FILES["profilePicture"]["tmp_name"];
该功能将如下所示:
public function savePicture() {
if ( isset( $_POST["register_new"] ) ) {
$profiles = "profiles/$this->username.txt";
if ( $this->password !== $this->passwordRepeat ) {
} else if ( file_exists( $profiles ) ) {
} else if ( $this->age < 18 ) {
} else {
$pictureDir = "profilePictures/";
//Do not add the username here, because it isn't defined anywhere.
//Also you didn't define $_FILES here, and basedir isn't neccessary since "name" only contains the filename
$pictureFile = $pictureDir . $_FILES["profilePicture"]["name"];
//To get the current picture use tmp_name
$pictureTemp = $_FILES["profilePicture"]["tmp_name"];
$pictureType = pathinfo( $pictureFile, PATHINFO_EXTENSION );
$uploadFile = 1;
$check = getimagesize( $pictureTemp );
if ( $check !== false ) {
$uploadFile = 1;
} else {
require "register.html";
echo "<p>File is not an image</p>";
$uploadFile = 0;
}
/*
if (file_exists($pictureFile)) {
echo "<p>File already exists</p>";
}
*/
if ( $_FILES["profilePicture"]["size"] > 200000 ) {
echo "File is too large! We accept files that are less than 2 MB";
$uploadFile = 0;
}
if ($pictureType != "jpg" && $pictureType != "png" && $pictureType != "jpeg" && $pictureType != "gif") {
echo "<p>Sorry, only JPG, JPEG, PNG & GIF files are allowed.</p>";
$uploadFile = 1;
}
if ( $uploadFile == 0 ) {
echo "<p>File was not uploaded</p>";
} else {
move_uploaded_file( $pictureTemp, $pictureFile );
}
}
}
}