Rename file after upload, PHP?

Asked

Viewed 1,128 times

0

I am uploading the files successfully, but the file cannot be renamed, returns a name 0.txt, or 0.png depending on the extension, where I am missing?

<?
for ($i=1;$i<=1;$i++)
{
    $arquivo = "arq".$i;

    if (!empty($_FILES[$arquivo]['name']))
    {
        $fileName     =  $_FILES[$arquivo]['name'];
        $fileTmp      =  $_FILES[$arquivo]['tmp_name'];
        $fileType     =  $_FILES[$arquivo]['type'];
        $fileSize     =  $_FILES[$arquivo]['size'];
        $fileExt      =  substr($fileName, -4, 5);
        $fileExt      =  str_replace(".", "", $fileExt);
        $extFile      =  $fileExt;
        $fileExt      =  ".".$fileExt;
        $maxSize      =  "800000";
        $hash       =  date("sm");


        $nomeSemExt   = preg_replace('/\.[^.]*$/', '', $fileName); 
        $fileNamex     = strtolower(preg_match("/[^a-zA-Z0-9_.]/", "", strtr($nomeSemExt, "áàãâéêíóôõúüçÁÀÃÂÉÊÍÓÔÕÚÜÇ ", "aaaaeeiooouucAAAAEEIOOOUUC_")));

        $local        =  "../arquivos/";
        $novoNome     =  $fileNamex."".$fileExt;


        $arqEX = "../arquivos/$novoNome";

        if(file_exists($arqEX)) 
        {
            $novoNome     =  $fileNamex."_$hash".$fileExt;
        } 

        move_uploaded_file( $fileTmp, "$local"."$fileName");
        rename("$local"."$fileName", "$local"."$novoNome");
        $local = substr($local, 3, 120);
        $tipo = $fileExt;       

        if($extFile!='exe')
        {

            echo "Arquivo <b><a href=\"arquivos/$novoNome\" target=\"_blank\" style=\"color:#000;font-weght:normal;\" >$novoNome</a></b> anexado!";
            echo "<input type=\"hidden\" name=\"uparquivo$i\" id=\"uparquivo$i\" class='txt03' value=\"$novoNome\">";
            echo "<br><br>Nome do arquivo<br><input type=\"text\" name=\"title$i\" size=\"60\" id=\"title$i\" class=\"inputMd\" value=\"\">";
            echo "<input type=\"hidden\" name=\"ext$i\" id=\"ext$i\"  value=\"$extFile\">";
            echo "<input type=\"hidden\" name=\"size$i\" id=\"size$i\"  value=\"$fileSize\"> ";
            echo "<a href=\"javascript:void(0)\" onClick=\"Load('delete&file=true&arq=$novoNome&i=1', 'upform1')\"><img src=\"images/delete.png\" class=\"img\"></a>";
        }
        else
        {
            echo "<script>alert(\"Arquivos com extesão .exe não são permitidos!\");</script>";
            echo "<input type=\"file\" name=\"arq1\" id=\"arq1\" size=\"20\" onChange=\"enviaDados1('upform1')\">";
        }
    }
}
?>
  • What is the value of $novoNome?

  • $novoNome = $fileNamex."". $fileExt;

1 answer

2


No need to rename, just make use of the move_uploaded_file to save with the desired name directly, for example:

move_uploaded_file( $fileTmp, $local . $novoNome);
$local = substr($local, 3, 120);
$tipo = $fileExt;  
  • I tried this solution but it keeps returning the file with name 0.* (ex: 0.txt), I wanted it to return the file with the original name of the same but without the extension, but very grateful for the help.

  • It worked, with your help and a small change ($newName = $fileNamex."".$nameSemExt;), the code returned the correct file names, thank you very much Guilherme.

Browser other questions tagged

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