I would not do this in any way, there are no guarantees of "integrity", names can be repeated mixing files, folders that will be empty can be created and stay there without link with the bank, other problems can occur like this (depending on the system).
You can solve everything based on the ID of the item in the database table, when you create a table in a reasonable format, usually in most types of database you have the ID column, which is usually AUTO_INCREMENT
, ie using this id will already solve the problem.
So when registering the property, take the ID, for example at the time of registering the property you use Insert, then right after use LAST_INSERT_ID
, something like:
INSERT INTO imoveis (categoria, descricao) VALUES ('casa', 'foo bar baz');
SELECT LAST_INSERT_ID();
Then you can create a folder name based on this ID.
Of course, since you are using the PHP API so you could do everything yourself, you can create a function just to generate the ID-based folder:
function GerarPasta($categoria, $id, $target) {
$prefixo = strtoupper($categoria{0}); // Gera C (casa), T (terreno), A (apartamento)
$path = $prefixo . str_pad($id, 10, '0', STR_PAD_LEFT);
if (!is_dir($path)) mkdir($path, 0755);
}
The use would be:
GerarPasta(<categoria>, <id do banco do imovel>, <pasta aonde vai criar as pastas>);
In this function I did not generate strings with url format youtube, but rather based only on ID, so properties with ids like 1, 2, 3, 4 and 100 will generate:
GerarPasta('casa', 1, '/foo/bar/'); // Gera pasta: /foo/bar/C0001
GerarPasta('terreno', 2, '/foo/bar/'); // Gera pasta: /foo/bar/T0002
GerarPasta('apartamento', 3, '/foo/bar/'); // Gera pasta: /foo/bar/A0003
GerarPasta('casa', 4, '/foo/bar/'); // Gera pasta: /foo/bar/C0004
GerarPasta('casa', 100, '/foo/bar/'); // Gera pasta: /foo/bar/C0100
Which I believe already suits your case well, but if you really want a string scheme I think you should use the function like this:
function GerarPasta($categoria, $id, $target) {
$prefixo = $categoria{0}; // Gera C (casa), T (terreno), A (apartamento)
//Usando L gera em 32bit combinado com o "repetidor *"
$packb64 = base64_encode(pack('L*', $id));
//Remove qualquer caractere diferente de A-Z e 0-9
$foldername = preg_replace('#[^\da-z]#i', '', $packb64);
$path = $prefixo . $foldername;
if (!is_dir($path)) mkdir($path, 0755);
}
Note: I haven’t tested the function, I don’t know if she can conflict
Examples generate something like:
GerarPasta('casa', 1, '/foo/bar/'); // Gera pasta: /foo/bar/CAQAAAA
GerarPasta('casa', 2, '/foo/bar/'); // Gera pasta: /foo/bar/CAGAAAA
GerarPasta('casa', 3, '/foo/bar/'); // Gera pasta: /foo/bar/CAWAAAA
GerarPasta('casa', 1000, '/foo/bar/'); // Gera pasta: /foo/bar/C6AMAAA
GerarPasta('casa', 1000000, '/foo/bar/'); // Gera pasta: /foo/bar/CQEIPAA
GerarPasta('casa', 10000000, '/foo/bar/'); // Gera pasta: /foo/bar/CGJAYAA
Example of use:
I don’t know how you did the code, but if you used preparedstatment it must be something similar to this:
if (isset($_POST['categoria'], $_POST['descricao'])) {
$categoria = $_POST['categoria'];
$descricao = $_POST['descricao'];
if (!$connection->prepare('INSERT INTO imoveis (categoria, descricao) VALUES (?, ?)')) {
if ($prepared->bind_param('ss', $categoria, $descricao)) {
if ($prepared->execute()) {
//Aqui pega a ID
$id_inserido = mysqli_insert_id($link);
GerarPasta($categoria, $id_inserido, '/home/user/public_html/'); //Cria a pasta
} else {
echo 'Falha ao executar a query';
}
} else {
echo 'Falha nos parametros passados';
}
$prepared->close();
} else {
echo 'Falha no prepare';
}
}
I would like to understand the negative point.
– Agência La Barba
It was not I who denied, but it was not very clear what instructions you want. I answered some things based on what I understood.
– Wallace Maxters
People should explain, rather than just negatively. Lack of sense this.
– Agência La Barba
I understand your point, it’s revolting to take a negative without an explanation. But sometimes some users do so for fear of reprisals (which unfortunately has happened a lot [including already happened and happens to me])
– Wallace Maxters
@Once I formulated a reply https://answall.com/a/314187/3635, the ideal way to avoid failures or conflicts is to use the ID of the property registered (assuming you use auto_increment in the table), so I left an example more or less to generate the folders.
– Guilherme Nascimento
@Wallacemaxters, I get upset, but ball forward... I work well on the front, but I suffer a little in the backend... anyway...
– Agência La Barba