Doubt about upload code

Asked

Viewed 55 times

-1

I have a question about that upload code.

<?php
//SISTEMA DESENVOLVIDO POR LUCIANO ZANITA | [email protected]

//requerendo o banco de dados a conexão
include "conecta.php";

//PROPRIEDADES DO UPLOAD DO ARQUIVO
$name = $_FILES["logotipo"]["name"];
$type = $_FILES["logotipo"]["type"];
$size = $_FILES["logotipo"]["size"];
$temp = $_FILES["logotipo"]["tmp_name"];
$error = $_FILES["logotipo"]["error"];

if ($error > 0)
{
    die("Ouve alguns problemas. ! Codigo do<b></b> Erro: $error.");
}
else
{

 if($type == "video/avi" || $size > 2000000) //imagens que pode ser upload, e tamanho de arquivo maximo
 {
  die("Arquivo não aceito ou tamanho acima do Limite.");
 }
 else
{
move_uploaded_file($temp,"imagens/".$name);
 echo"Arquivo enviado";
} }
$sql = mysql_query("INSERT INTO BancoDedados (logotipo) VALUES ('imagens/$name')");
?>

Why I saw it accepts all kinds of upload but only does a memory check on video/avi files?

That number that this ai 2000000 is Megabytes or KB ?

  • I already know it’s bytes, only the other doubt now :D

  • 1

    In this case you can add other extensions there, which you do not want to send... or do only extensions allowed.

1 answer

2


You can develop as follows:

<?php
//SISTEMA DESELVOLVIDO POR LUCIANO ZANITA | [email protected]

    //requerendo o banco de dados a conexão
    include "conecta.php";

    //PROPRIEDADES DO UPLOAD DO ARQUIVO
    $name = $_FILES["logotipo"]["name"];
    $type = $_FILES["logotipo"]["type"];
    $size = $_FILES["logotipo"]["size"];
    $temp = $_FILES["logotipo"]["tmp_name"];
    $error = $_FILES["logotipo"]["error"];

if ($error > 0){
    die("Ouve alguns problemas. ! Codigo do<b></b> Erro: $error.");
} else {

    if($type == "video/avi" || $size > 2000000){
        die("Arquivo não aceito ou tamanho acima do Limite.");
    } elseif($type=="image/jpg" or $type=="image/png"){ // Nesta linha você pode adicionar as extensões válidas e permitidas para envio, para descobrir a extensão, consulte a documentação de FILES, dentro do PHP
        move_uploaded_file($temp,"imagens/".$name);
        $sql = mysql_query("INSERT INTO BancoDedados (logotipo) VALUES ('imagens/$name')");
        echo"Arquivo enviado";
    } 
}
?>

Add valid extensions. To know the type of extension, see the $_FILES documentation in PHP. http://php.net/manual/en/reserved.variables.files.php

To know the amount of MB you want to allow: http://pt.calcuworld.com/calculadoras-para-empresas/calculadora-de-bytes/

  • 1

    Ah now I understood the video/avi files will not be allowed...

  • 1

    Exactly, in the second check I entered only the ones that will be allowed, then you will include what is your wish.

  • 1

    I liked the modification you made, it is easier for me to say the ones I will allow than to block, as it has several file extensions all over the world, Thanks

  • 1

    Exactly, there it is intended for which you allow and if you want to add an Else, you can still place a message if you do not find inside the allowed extensions.

  • I will use only so, have any problem sera in the code that I can harm him? if ( $error > 0 ) { die ( "Hear some problems." ) ; } Else { move_uploaded_file ($temp,"upload/". $name) ; echo ( "Uploaded file" ) ; }

  • 1

    Without going through the spells? wouldn’t it.. but they can send malicious files...

  • It’s going to be a very basic thing, I want to clear that code.

Show 2 more comments

Browser other questions tagged

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