PHP-File不';救不了
我希望用户能够上传一张以他的用户名命名的个人资料图片,这样我以后就可以轻松地调用它。问题是,没有文件保存在所需的文件夹中。文件夹名为“profilePictures”,当用户的用户名调用其中应有图片时,该文件夹名为空。我试着把它修好了,但运气不好。我做错了什么 代码:PHP-File不';救不了,php,class,Php,Class,我希望用户能够上传一张以他的用户名命名的个人资料图片,这样我以后就可以轻松地调用它。问题是,没有文件保存在所需的文件夹中。文件夹名为“profilePictures”,当用户的用户名调用其中应有图片时,该文件夹名为空。我试着把它修好了,但运气不好。我做错了什么 代码: <?php class User { public function __construct( $username, $password, $passwordRepeat, $email, $firstName,
<?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 );
}
}
}
}
公共函数savePicture(){
如果(isset($_POST[“register_new”])){
$profiles=“profiles/$this->username.txt”;
如果($this->password!==$this->passwordRepeat){
}else if(文件_存在($profiles)){
}否则如果($this->age<18){
}否则{
$pictureDir=“profilePictures/”;
//不要在这里添加用户名,因为它没有在任何地方定义。
//另外,您没有在这里定义$u文件,并且basedir也不是必需的,因为“name”只包含文件名
$pictureFile=$pictureDir.$_文件[“profilePicture”][“name”];
//要获取当前图片,请使用tmp_名称
$pictureTemp=$\u文件[“profilePicture”][“tmp\u名称”];
$pictureType=pathinfo($pictureFile,pathinfo\u扩展名);
$uploadFile=1;
$check=getimagesize($pictureTemp);
如果($check!==false){
$uploadFile=1;
}否则{
需要“register.html”;
echo“文件不是图像””;
$uploadFile=0;
}
/*
如果(文件_存在($pictureFile)){
echo“文件已存在””;
}
*/
如果($_文件[“profilePicture”][“大小”]>200000){
echo“文件太大!我们接受小于2MB的文件”;
$uploadFile=0;
}
如果($pictureType!=“jpg”&&&$pictureType!=“png”&&&$pictureType!=“jpeg”&&&$pictureType!=“gif”){
echo“对不起,只允许使用JPG、JPEG、PNG和GIF文件。”;
$uploadFile=1;
}
如果($uploadFile==0){
echo“文件未上载””;
}否则{
移动上传的文件($pictureTemp,$pictureFile);
}
}
}
}
您是否检查了权限以确保web服务器能够写入目录?在移动上传的文件()
功能中使用的$pictureTemp
应该是上传到服务器上的文件。目前还没有$\u FILES['profilePicture']['tmp\u name']
@Shocm我正在本地主机上执行此操作您应该检查移动上传的\u file
的返回值。不要只是假设它成功了。@Denki-localhost与否,权限就是权限。如果执行脚本的用户帐户没有对输出目录的写入权限,则上载将失败。感谢您的评论。我照你说的做了,但还是一事无成。这真的很奇怪。出于某种原因,它说,$uploadFile=1未使用“代码>”。变量的值会立即被覆盖“未使用”是什么意思?你的意思是$uploadFile=0代码>?