move_uploaded_file does not work

Asked

Viewed 2,924 times

1

Hello, move_uploaded_file is not working and would like to know why

index php.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Upload</title>
</head>

<body>
    <form method="post" action="pegar.php" name="form">
        <input type="file" name="arquivo" value="Escolher">
        <input type="submit" name="enviar" value="Enviar" height="50" width="10">
    </form>
</body>
</html>

get php.

$foto = $_FILES['arquivo']['name'];
$foto = str_replace(" ", "_", $foto);
$foto = str_replace("á", "a", $foto);
$foto = str_replace("à", "a", $foto);
$foto = str_replace("â", "a", $foto);
$foto = str_replace("ã", "a", $foto);
$foto = str_replace("é", "e", $foto);
$foto = str_replace("è", "e", $foto);
$foto = str_replace("ê", "e", $foto);
$foto = str_replace("í", "i", $foto);
$foto = str_replace("ì", "i", $foto);
$foto = str_replace("î", "i", $foto);
$foto = str_replace("ó", "o", $foto);
$foto = str_replace("ò", "o", $foto);
$foto = str_replace("õ", "o", $foto);
$foto = str_replace("ô", "o", $foto);
$foto = str_replace("ç", "c", $foto);
$foto = str_replace("û", "u", $foto);
$foto = str_replace("ù", "u", $foto);
$foto = str_replace("ú", "u", $foto);

$foto = strtolower($foto);

if(file_exists("fotos/$foto"))
{
    $a = 1;
    while(file_exists("fotos/[$a]$foto"))
    {
        $a++;    
    }

    $foto = "[".$a."]$foto";    
}

if(!move_uploaded_file($_FILES['arquivo']['tmp_name'], "fotos/".$foto))
{
    echo "<meta http-equiv='refresh' content='0; url=index.php'>
          <script type='text/javascript'>alert('Erro no upload do arquivo!')</script>";    
}
  • There should be a Warning on the screen with the reason, no?

  • 1

    You need to specify the enctype="multipart/form-data" on your tag <form>.

  • Yeah, it worked right, VLW by the help bro.

  • Please include@Paulorodrigues as the answer, the questioner should mark as correct

1 answer

2


As described in w3schools, whenever it is necessary to carry out file sending, use the attribute enctype if necessary, thus remaining:

<form method="post" action="pegar.php" name="form" enctype="multipart/form-data">

Remembering that the attribute enctype only works correctly if the attribute method be defined as post.

Browser other questions tagged

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