简体   繁体   中英

PHP - File doesn't save

I want the user to be able to upload a profile picture that is named after his username, so I can easily call it afterwards. The problem is, no file is saved at the desired folder. The folder name is "profilePictures" and it's empty when there should be a picture inside called by the username of the user. I've tried to fix it but no luck. What am I doing wrong ?

Code:

<?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();

First make sure that you have write permission on the profilePictures map. Assuming you have permission i think it fails on this two lines:

$pictureFile = $pictureDir . basename(["profilePicture"]["name"][ $this->username ] );
$pictureTemp = $_FILES["profilePicture"]["name"][ $this->username ];

Try this instead of the 2 lines above:

//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"];

The function will become like this:

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 );
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM