Getting error in making new directory for file upload functionality in CRUD project

Viewed 38

I have made a crud project with file upload functionality. For that, each time a user registers in the page a folder with his/her unique id name should be created in the main folder which has been created originally.

If that user, after logging in, uploads the file, it should get stored in that folder which was automatically created when he had registered himself.

For this , I have two files; register.php (code for register functionality) and add.php (for uploading data and file):

Code(Register.php):

    if(isset($_POST['submit'])) {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $user = $_POST['username'];
        $pass = $_POST['password'];
        mkdir("C:/xampp/htdocs/crud/crud_html/'.$name.'/");

        if ($user == "" || $pass == "" || $name == "" || $email == "") {
            echo "All fields should be filled. Either one or many fields are empty.";
            echo "<br/>";
            echo "<a href='register.php'>Go back</a>";
        } else {
            mysqli_query($mysqli, "INSERT INTO login(name, email, username, password) VALUES('$name', '$email', '$user', md5('$pass'))")
            or die("Could not execute the insert query.");
            
            echo "Registration successfully";
            echo "<br/>";
            echo "<a href='login.php'>Login</a>";
            if(!file_exists("C:/xampp/htdocs/crud/crud_html/images/'.$id'")){
                //if the folder doesn't exist, create it
                mkdir("C:/xampp/htdocs/crud/crud_html/images/'.$id'");
            else{
                continue;
            }    
            
        }
    } else {
?>
        <p><font size="+2">Register</font></p>
        <form name="form1" method="post" onsubmit="return validatename()" action="">
            <table width="75%" border="0">
                <tr> 
                    <td width="10%">Full Name</td>
                    <td><input type="text" name="name" maxlength="20" id="t1" required></td>
                </tr>
                <tr> 
                    <td><label for="email">Email</label></td>
                    <td><input type="email" name="email" required ></td>
                </tr>           
                <tr> 
                    <td>Username</td>
                    <td><input type="text" name="username" maxlength="10" id="t2" required></td>
                </tr>
                <tr> 
                    <td><label for="psw">Password</label></td>
                    <td><input type="password" name="password" required id="psw" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}" title="Must contain at least one number and one uppercase and lowercase letter, and at least 8 or more characters"></td>
                </tr>
                <tr> 
                    <td> </td>
                    <td><input type="submit" name="submit" value="Submit"></td>
                </tr>
            </table>
        </form>
    <?php
    }
    ?>
</body>
</html>

But this code is not working. What correction is needed?

1 Answers

I believe the main problem is you reference, in your move_uploaded_file() function a variable called $newd, but in your code above, it looks like you named it $uploads_dir. The below code, simplified from yours, works for me.

Register.php

if ($user == "" || $pass == "" || $name == "" || $email == "") {
    echo "All fields should be filled. Either one or many fields are empty.";
    echo "<br/>";
    echo "<a href='register.php'>Go back</a>";
} else {
    mysqli_query($mysqli, "INSERT INTO login(name, email, username, password) VALUES('$name', '$email', '$user', md5('$pass'))")
    or die("Could not execute the insert query.");
    
    echo "Registration successfully";
    echo "<br/>";
    echo "<a href='login.php'>Login</a>";
    //check if the folder exists
    if(!file_exists("C:/xampp/htdocs/crud/crud_html/images/'.$id'")){
      //if the folder doesn't exist, create it
      mkdir("C:/xampp/htdocs/crud/crud_html/images/'.$id'");
    }
}

Add.php

<?php
//only run this if the form was submitted
if(isset($_POST['Submit'])) {  
    $pname = rand(1000,10000)."-".$_FILES["file"]["name"]; #file name with random number so that similar don't get replaced
    //get the name of the uploaded file
    $tname = $_FILES["file"]["tmp_name"];  
    //set your upload directory
    $uploads_dir= __DIR__;

    //move the uploaded file to the new directory
    move_uploaded_file($tname, $uploads_dir.'/'.$pname);
}
?>
<!--form that will allow the user to upload a file-->
<form action="" method="POST" enctype="multipart/form-data">
    <input type="file" name="file" />
    <input type="submit" name="Submit" />
</form>
Related