[英]PHP - File doesn't save
我希望用戶能夠上傳以其用戶名命名的個人資料圖片,以便以后方便地調用它。 問題是,沒有文件保存在所需的文件夾中。 文件夾名稱為“ 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();
首先,請確保您對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 );
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.