Changing the identification data of an upload file

Asked

Viewed 61 times

0

I am first trying to rename a file according to what the user provides us, however error occurs, but it is given as $_FILES["arquivo_foto"]["error"] -> 0

Sending script

<?php


$diretorioimagens = "arquivos/image/";
$uploadarquivo = $diretorioimagens . basename($_FILES["arquivo_foto"]["name"]);
$novonome = $_POST["novo_nome_foto"];
echo $novonome;
$_FILES["arquivo_foto"]["tmp_name"] = $novonome;
if($_FILES["arquivo_foto"]["size"] < 62914560){
    if(move_uploaded_file($_FILES["arquivo_foto"]["tmp_name"], $diretorioimagens)){
        echo "Enviado com sucesso com o nome " . $_FILES["arquivo_foto"]["tmp_name"];
    }else{
        echo "Não foi possível enviar o arquivo, mais detalhes do erro abaixo <br>" . $_FILES["arquivo_foto"]["error"];
    }
}else{
    echo "Arquivo ultrapassa 60Mb";
};

?>

Form for sending the photo

<form method="post" enctype="multipart/form-data" action="envia_foto.php">
            <div>
                <label>Pré-visualização</label>
                <img src="" alt="Pré-visualização da imagem enviada">
            </div>
            <label>Nome do arquivo:
                <input type="text" name="novo_nome_foto" placeholder="Nome do arquivo">
            </label>
            <label>
            <input type="hidden" value="62914560" name="">
                <input type="file" name="arquivo_foto" placeholder="Arquivo">
            </label>
            <button type="submit">Enviar</button>
        </form>

What’s wrong, and how can I fix it ?

  • Whether you are using hosting or virtual (xampp)? check your php.ini configuration to enable file_uploads for ON, EDIT: See the simple example https://www.w3schools.com/php/php_file_upload.asp

1 answer

2


The error is in that line:

$_FILES["arquivo_foto"]["tmp_name"] = $novonome;

To change the file name, do not modify the tmp_name, change the name when copying it to the definitive location, in the move_uploaded_file function:

move_uploaded_file($_FILES["arquivo_foto"]["tmp_name"], $diretorioimagens . $novonome)

And it is very important you filter the new name to avoid confusion in your file system, or try to save in different folder with something like:

// remove tudo que não for palavra, espaço, numero ou -_~,;[]()
$novonome = mb_ereg_replace("([^\w\s\d\-_~,;\[\]\(\).])", '', $novonome);
// remove pontos corridos 
$novonome = mb_ereg_replace("([\.]{2,})", '', $novonome);
  • $directory images already have a bar at the end of the path.

  • Got it, it calls PATH, recommend by punctuation an example $diretorioimagens = "./arquivos/image/" .. Anderson said it should / this right, if independent server the Unix directory

Browser other questions tagged

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