0
I am uploading php to the server. At first it worked perfectly. The problem is that now when it uploads to the folder on the server it changes the name.
<?php
try {
if (
!isset($_FILES['arquivo']['error']) ||
is_array($_FILES['arquivo']['error'])
) {
throw new RuntimeException('Parametros invalidos. Contate o administrador.');
}
switch ($_FILES['arquivo']['error']) {
case UPLOAD_ERR_OK:
break;
case UPLOAD_ERR_NO_FILE:
throw new RuntimeException('O arquivo não foi selecionado.');
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
throw new RuntimeException('O arquivo excedeu o tamanho máximo permitido (5mb).');
default:
throw new RuntimeException('Erro desconhecido. Contate o administrador.');
}
if ($_FILES['arquivo']['size'] > 5242880) {
throw new RuntimeException('O arquivo excedeu o tamanho máximo permitido (5mb).');
}
$finfo = new finfo(FILEINFO_MIME_TYPE);
if (false === $ext = array_search(
$finfo->file($_FILES['arquivo']['tmp_name']),
array(
'pdf' => 'application/pdf',
),
true
)) {
throw new RuntimeException('<br />Arquivo com formato invalido (Extenção permitida: PDF).<br />');
}
if (!move_uploaded_file(
$_FILES['arquivo']['tmp_name'],
sprintf('Documentos/%s.%s',
sha1_file($_FILES['arquivo']['tmp_name']),
$ext
)
)) {
throw new RuntimeException('<br />Falha ao fazer upload do arquivo. Contate o administrador.<br />');
}
echo '<br />Arquivo enviado com sucesso <br />';
} catch (RuntimeException $e) {
echo $e->getMessage();
}
?>
How do I upload it with the original file name and not with tmp?
I tried with the name in sprintf but did not put the ext in front. I will try when I get home. Thanks.
– Willian Coqueiro
Buddy I had to make some changes to the code you gave me
if (!move_uploaded_file( $_FILES['arquivo']['tmp_name'],
 sprintf('Documentos/%s.', $_FILES['arquivo']['name'], $ext)))
and now it worked perfectly, the way it passed me it wrote the extension in the name and more the . pdf, example.pdf.pdf. Thanks more if it wasn’t for your help I’d beat myself up for a long time.– Willian Coqueiro
Glad you solved your problem @Williancoqueiro :) Well, it went unnoticed. I’ve already edited the answer.
– DolbyCpp