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 remain there without link with the bank, other problems can occur like this (depending on system).
You can solve everything based on the item ID in the database table, when you create a table in a reasonable format, usually in most database types you have the ID column, which is usually AUTO_INCREMENT
, ie using this ID will solve the problem.
Then when registering the property, take the ID, for example at the time of registering the property you use insert, then soon 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 the name of a folder based on this ID.
Of course, since you are using the PHP API, you could do everything in it, you can create a function to generate the folder based on the ID:
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 youtube url style format, but only based on ID, then real estate 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
13.07.2018 / 18:44