How to create a folder for each user in PHP?

Asked

Viewed 1,340 times

1

I would like an example of how to create a folder to store each user’s data in php, for example www.facebook.com/nomedeusuario

I imagine that in the sequence an index is saved inside this folder and this index is created at the time of registration, with the user information as in Facebook. Could someone give me an example of how to do this?

  • 1

    This is not folder. This can best be called the Friendly URL. Facebook can use the /qualquercoisa for profile.php?id=qualquercoisa. Yes, both are accessible! You can see about it here: http://answall.com/questions/25985/url-amig%C3%a1vel-com-htaccess, but complex, or here: http://answall.com/questions/75963/url-amig%C3%a1vel-utilizing-htaccess/75970#75970.

  • It is not folder is url friendly, look at this example. And read more here

  • 1

    Another way to rewrite, without using mod_rewrire is to pass variables after the index.php file, example: www.seusite.com.br/index.php/pergunta/resposta and capture the URL and separate everything explode() "index.php/".

  • 1

    @Ivanferrer even, if you set up Apache (or equivalent) for this, you don’t even have to be index.php. It may very well be http://example.com/noticias/esporte - is what I normally do. I don’t use htaccess and simplify the whole process.

  • to ligado @Bacco!

1 answer

6


If you want to create folder, you will have to define a path or directory where the folders will be created and save in a constant or variable and then take the name of the person and for the creation of the folder, before any action check if the folder already exists. See an example below:

$pathName = "C:\\php\\www\\pastas_user\\" . $nome_user;

if(!file_exists($pathName )){
   mkdir($pathName); //aqui ele irá criar a pasta
} else {
   $pathName .= time(); //concatena a pasta para gerar com um nome diferente
   mkdir($pathName); //aqui ele irá criar a pasta
}

After this just write the folder directory in the database in the user record.

  • 1

    I think he refers to the folder as being the "URL", as he said, same as facebook.

  • 2

    +1, because apparently the author wants to be something else, answered the question. After all, he may be wanting the URL for a thousand different uses, including saving files from each one separately. (and if it’s not that, someone else who needs it can look for "folder" and find exactly your answer)

  • Yes Bacco, actually I was looking for this "friendly url" created through . htaccess, but learning how to create the folder will also be useful for future projects! Thank you all!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.