-1

So i got this form, to upload some fields and a image to the database:

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" >
    <fieldset>

                 <p><label>Nome:</label><input name="nome" type="text" class="text-long" /></p>
                 <p><label>Género:</label><input name="genero" type="text" class="text-long" /></p>
                 <p><label>Ano de Lançamento:</label><input name="ano" type="text" class="text-long" /></p>
                 <p><label>Banda:</label><input name="banda" type="text" class="text-long" /></p>
                 <p><label>Preço:</label><input name="preco" type="text" class="text-long" /></p>
                 <p><label>Imagem:</label><input name="arquivo" type="file" value="" /></p>

        <input type="submit" name="enviar" value="Inserir Album" >
    </fieldset>

And then after the fieldset i have the php code to upload the image and check the fields:

<?php

include('ligacao.php');

include("resize-class.php");



if(isset($_POST['enviar']))

{ //START IF



$nomealbum = $_POST['nome'];

$genero = $_POST['genero'];

$ano = $_POST['ano'];

$banda = $_POST['banda'];

$preco = $_POST['preco'];

//o nome original do arquivo no computador do utilizador

$arqName = $_FILES['arquivo']['name'];

  //o tipo mime do arquivo

$arqType = $_FILES['arquivo']['type'];

   //o tamanho do arquivo

$arqSize = $_FILES['arquivo']['size'];

//o nome temporario do arquivo com que foi guardado no servidor

$arqTemp = $_FILES['arquivo']['tmp_name'];

 //o codigo de erro associado a este upload de arquivo

$arqError = $_FILES['arquivo']['error'];

 //lista de tipos de arquivos permidos

$tiposPermitidos = array('image/gif', 'image/jpeg', 'image/jpg', 'image/png');

//tamanho maximo

$tamanhoPermitido = 6024 * 3000;



if (empty($nomealbum) {

$noname = '<span  class="error">Insira um nome para album!</span></br>';

echo $noname;

} 



if (empty($genero)) {

$nogenero = '<span  class="error">Insira um genero para o album!</span></br>';

echo $nogenero;

}

if (empty($ano)) {

$noano = '<span  class="error">Insira uma ano para o album!</span></br>';

echo $noano;

} 

if (empty($banda)) {

$nobanda = '<span  class="error">Insira uma banda para o album!</span></br>';

echo $nobanda;

} 

if (empty($preco)) {

$preco = '<span  class="error">Insira uma banda para o album!</span></br>';

echo $nopreco;

} 

if (empty($arqName)) {

$noarqName = '<span  class="error">Insira uma imagem!</span></br>';

echo $noarqName;

}



if ($arqError == 0 && !empty($nomealbum) && !empty($genero) && !empty($ano) && !empty($banda)&& !empty($preco))

{

    //verifica o tipo de arquivo enviado

    if(array_search($arqType, $tiposPermitidos) === false)

    {

    $arqinvalido = '<span class="error">O tipo de arquivo enviado é inválido!</span>';

    echo $arqinvalido;

    }

    //verifica o tamanho do arquivo enviado

    else if ($arqSize > $tamanhoPermitido)

    {

    $arqgrande = '<span class="error">O tamanho do arquivo enviado é maior que o limite!</span>';

    echo $arqgrande;

    }

    else

    {

        $pasta = 'imagens_albuns/';

        //pega a extensao do arquivo enviado

        $extensaoTemp = explode('.',$arqName);

        $extensao = strtolower(end($extensaoTemp));

        //define o nome do arquivo usando um Unix TimesTamp

        $nome = time(). '.' . $extensao;

        $upload = move_uploaded_file($arqTemp, $pasta . $nome);



        if ($upload === true)

        {

        $ficheiro = $pasta.$nome; 

        $resizeObj = new resize($ficheiro); 

        $resizeObj -> resizeImage(800, 600, 'auto');

        $resizeObj -> saveImage("imagens_albuns/$nome", 100);

        $resizethumb = new resize($ficheiro);   

        $resizethumb -> resizeImage(150, 150, 'crop');

        $resizethumb -> saveImage("imagens_albuns/thumbnails/$nome", 100);

        $query = mysql_query("INSERT INTO albuns (nome, genero, ano, banda, preco, arquivo) VALUES ('$nomealbum','$genero','$ano','$banda','$preco','$nome')") or die (mysql_error());

        //se a query existe entao a pagina é redireccionada

        if($query === true)

        {

        $sucesso = '<span  class="yes">Album inserido com sucesso!</span>';

        echo $sucesso;

        }

        }



        else

        {

        $retorno = '<span class="error">Nao foi possivel fazer o upload da imagem!</span>';

        echo $retorno;

        }

    }                         

}



}

?>

I cant get this to work, because everytime i try to upload the file, i get this error:

Notice: Undefined index: arquivo in /home/claire/public_html/inseriralbum.php on line 45 Notice: Undefined index: arquivo in /home/claire/public_html/inseriralbum.php on line 49 Notice: Undefined index: arquivo in /home/claire/public_html/inseriralbum.php on line 53 Notice: Undefined index: arquivo in /home/claire/public_html/inseriralbum.php on line 57 Notice: Undefined index: arquivo in /home/claire/public_html/inseriralbum.php on line 61 Insira uma imagem! O tipo de arquivo enviado é inválido!

I can "arquivo" be undefined, if its the name that is on the file input in the form?

Here is line 45 to 61

$arqName = $_FILES['arquivo']['name'];

  //o tipo mime do arquivo

$arqType = $_FILES['arquivo']['type'];

   //o tamanho do arquivo

$arqSize = $_FILES['arquivo']['size'];

//o nome temporario do arquivo com que foi guardado no servidor

$arqTemp = $_FILES['arquivo']['tmp_name'];

 //o codigo de erro associado a este upload de arquivo

$arqError = $_FILES['arquivo']['error'];
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141

1 Answers1

2

You <form> tag needs to have enctype="multipart/form-data" to work on file uploads.

So that when you submit the form, then you can access those $_FILES data.

And make sure that you have proper permissions in order for the move_uploaded_file() function work.

You can use the chmod command (terminal) on that particular destination folder.

Kevin
  • 41,694
  • 12
  • 53
  • 70
  • OK, but now i got this error: Warning: move_uploaded_file(imagens_albuns/1418692561.jpg): failed to open stream: Permission denied in /home/claire/public_html/inseriralbum.php on line 169 Warning: move_uploaded_file(): Unable to move '/tmp/phpaNaqMB' to 'imagens_albuns/1418692561.jpg' in /home/claire/public_html/inseriralbum.php on line 169 Nao foi possivel fazer o upload da imagem! – Pedro Fidalgo Dec 16 '14 at 01:17
  • @user2535046 basically it already says it. you need to have permissions to move the file. you can use your terminal and set the destination folder and use a `chmod 775` on that. – Kevin Dec 16 '14 at 01:20
  • 1
    OP's also seems to be missing the closing form tag. Plus, might be a potential typo for "imagens_albuns". If in OP's language "album" is "albun", I'd say fine, but it may be that OP's folder is named "imagens_albums" which could be an additional factor and a reason for the error message because it doesn't exist. – Funk Forty Niner Dec 16 '14 at 01:32
  • @Fred-ii- yeah good point. its quite confusing, we don't know what language is this anyway its hard to track variable names. – Kevin Dec 16 '14 at 01:42
  • Ok guys i got this working, now i have problems on removing the fields from the database and the image from the folder. I will edit the op – Pedro Fidalgo Dec 16 '14 at 01:44
  • @user2535046 as a reminder, do not completely change the question with a another question. Just keep it concise and as brief as possible with pertinent details. If this is for another matter, and if you really need to, you need to create another/separate thread/question for it. – Kevin Dec 16 '14 at 01:49
  • Sure like to know what it was that "got it working". – Funk Forty Niner Dec 16 '14 at 01:49
  • It was the permissions of the folder on the mac. also needed to put the multipart/form-data on the form. – Pedro Fidalgo Dec 16 '14 at 01:50
  • @Ghost I rolled it back. – Funk Forty Niner Dec 16 '14 at 02:01
  • @Fred-ii- i thought it was a continuation, but a new question altogether, okay man thanks – Kevin Dec 16 '14 at 02:21
  • Yeah, total stealth edit. You're welcome, *cheers* - P.s.: OP seems to have vanished like a puff of smoke. – Funk Forty Niner Dec 16 '14 at 02:26