How to create a folder with the user's name when uploading a file (IMAGE)? PHP


Good on this occasion I want to make the user of my website login with the option of creating a photo album, and I can get the photo that the user uploads through the form with these codes:


  <form action="album.php" enctype="multipart/form-data" method="post">
    		<input type="file" name="image">
    		<input type="submit" value="Subír" name="upload">




    $usuario = $_SESSION['user'];

    if (isset($_POST['upload'])) {

        $conexion = mysqli_connect('localhost','root','','image');
        if (!$conexion) {
            die("Error al conectarse a la base de datos.!". mysql_error());


            move_uploaded_file($archivo, $directorio."/".$nombrearchivo); 

            mysqli_query($conexion, "INSERT INTO imagenes(imagen) 
            VALUES ('{$directorio}')"); 


In the directory variable "$ directory", if it is set after the folder tmpAlbums I insert the variable of the user, it is say, as if it were a folder with the name of that user, but it is obvious that it did not work for me.

Well in question is that, I want to help me that when the user clicks to upload the photo that photo can be moved to the main destination folder plus the user's folder that I want to create to be stored there all your photos.

I wonder if you understand me anyway, in case of doubts I will be aware let me know if there is any doubt about my purpose and thanks in advance.!

asked by Abdiel 26.11.2018 в 21:02

1 answer


Based on your code you create a directory that would be inside of which folder you have to create the user's folder:


With this code you check if the directory exists and if you do not believe it:

if (!file_exists($directorio)) {
    mkdir($directorio, 0777, true);

and then move the images to that directory:

move_uploaded_file($archivo, $directorio."/".$nombrearchivo);

A quick example of the creation:

answered by 26.11.2018 / 21:13