0
I wrote a feature on php
to upload files by running localhost
it works normal creating the correct directory, but on the server it creates wrong, if I step the way: home\banners
, it creates a folder with the name bootstrap.. and does not save the file, it is as if the function is not making use of the third parameter of the mkdir(path, mode, recursive = true)
, follows the function:
// arquivo config.php
define('DP', DIRECTORY_SEPARATOR);
define('UPLOAD_DIRECTORY', __DIR__ . '\..\assets' . DP);
// arquivo controllers.php
public function saveUploadFile(string $uploaddirectory = '', UploadedFile $file)
{
// PEGA EXTENÇÃO DO ARQUIVO
$_['ext'] = pathinfo($file->getClientFilename(), PATHINFO_EXTENSION);
// RENOMEIA PARA NUMERO EXADECIMAL ALEATORIO
$_['rename'] = bin2hex(random_bytes(16));
// JUNTA NOME DO ARQUIVO + EXTENSÇÃO
$image = sprintf('%s.%0.8s', $_['rename'], $_['ext']);
// CRIA PATH QUE SERA SALVA A IMAGEM
$_['path'] = UPLOAD_DIRECTORY . $uploaddirectory;
// SE NÃO EXISTE DIRETORIO CRIAR
if( !is_dir( $_[ 'path' ] ) ) {
if( !mkdir( $_['path'], 0777, true ) ){
exit('falha ao criar arquivo no diretorio '. $_['path']);
}
}
// MOVE ARQUIVO PARA O PATH
$file->moveTo($_['path'] . DP . $image);
// DEVOLVE CAMINHO ONDE IMAGEM FOI SALVA
return $_['path'] . DP . $image;
}
Folder structure:
root
├── app
│ └── Controllers
│ └── Controllers.php
├── bootstrap
│ └── Config.php
└── assets
└── // imagens devem ficar aqui
was that same, the backslashed fixed other parts of the code too and now this creating correctly, thank you!
– Hebert Lima
@Hebertlima I typed wrong a line, change
$_ext = pathinfo($file->getClientFilename(), PATHINFO_EXTENSION);
for$ext = pathinfo($file->getClientFilename(), PATHINFO_EXTENSION);
, already corrected in the reply– Guilherme Nascimento
had noticed, and fixed here xD vlw
– Hebert Lima
About DIRECTORY_SEPARATOR I recommend reading (since there is what we call multiplatform) https://answall.com/questions/2304/difference-entrepath-separator-e-directory-separator
– Marcos Xavier
@Marcosxavier makes no difference to operating systems within native PHP functions. The only use is if you will pass to commands like
exec()
, although nowadays WINDOWS also no longer suffers from this problem, rarely will use this.– Guilherme Nascimento
about DIRECTORY_SEPARATOR is my custom to use, ends up being automatico rs
– Hebert Lima
@Hebertlima then do not use things in automatic, use when necessary :) ... in case writing / is write much less than writing DIRECTORY_SEPARATOR or DS
– Guilherme Nascimento
I will also review the part of the unnecessary arrays and the initialization, I have this bad habit of not initializing the arrays =X @Guilhermenascimento
– Hebert Lima
@Guilherme Nascimento Question of good practices. It is for reasons like this that bad codes are appearing. Little things like this will leave the dev relaxed. My point of view, not that use / or Ds or DIRECTORY_SEPARATOR.
– Marcos Xavier
Dear @Marcosxavier good practice is synonymous with myths in programming, there are many things that are misleading, people understand the language but do not understand the architecture of the operating system, then make up fabulas about why you should use X form or Y form. The question is to understand the need for a Feature and the goal by which it was created, even in large frameworks I have already found "redundancies" that you realize it’s because of DEV confusion or simply because people follow "cake recipes" but don’t really learn the essence.
– Guilherme Nascimento
I disagree with your opinion in some quarters, but I respect it. I think (speaking of IT), cake recipes serve exactly to avoid reworks or redundant codes. If in this great framework you cited found redundancy imagine if you did not have the cake recipes. Patterns help prevent (in a multi-dev project) that everyone does it their own way, if you do it yourself, I see no problem following your own pattern. As for not understanding system architecture nor will I enter the merit.
– Marcos Xavier
Dear @Marcosxavier the problem is people following cake recipe without learning the basics and learning the essence, for me you are not disagreeing with me, in fact you are not understanding the root of the problem I want to talk about
– Guilherme Nascimento
I always say (there are many references on the site itself, if you search) that the expression "Good practices" is usually accompanied by nonsense. Time is solidifying this experience (there are already 30 years in the area, there are things that are shot and fall). "Good practices" simply do not exist. If you have a reason to do something, it is by reason (and you say what it is). If you do not say the real reason, it is because you do not know for sure. If you have something that you really think is "good practice," you don’t need a new name for it. I say this to open the minds of colleagues who unfortunately will hear a lot of the phrase around.
– Bacco
In the first answer, I agree on the essence and on disagreement and understanding, I understand and disagree. I close my part in this discussion. As Bacco has much more time than me in the area and has formed opinion also and in view of that, what to pass from here will not add to the topic. It’s nothing personal, for me it’s a healthy discussion.
– Marcos Xavier