Return image upload in index.php [duplicate]

0

I uploaded an image and clicked on "submit" I want you to show me the image in the page 'index.php'.Apresentando image in the browser

Code:

<html>
  <body> 
    <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
      Selecione : <input name="arquivo" type="file"/>
      <input type="submit" value="Enviar"/> 
    </form>

    <?php

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $aki = $_POST['arquivo']; 
        if (empty($aki)) {
             echo "introduza de novo";
        } else {
             echo $aki;
        }
    }

    ?>
  </body>
</html>

Result: Shows the file name and does not load the image as desired.

    
asked by anonymous 06.05.2017 / 04:27

1 answer

0

The $_POST does not get the file, the correct is $_FILES and the form must have enctype="multipart/form-data" , before leaving doing anything I recommend that you start studying for the documentation link .

A basic example should look something like this:

<html>
<body>
    <form action="" method="post" enctype="multipart/form-data">
        Selecione : <input name="arquivo" type="file">
        <input type="submit" value="Enviar">
    </form>

    <?php
    //Salva a foto com nome aleatório dentro da pasta
    $pasta = 'pasta-upload';

    $arq = $pasta . '/' . uniqid(rand(), true);

    $image = false;

    if (!chmod($pasta, 0755)) {
        echo 'Erro ao mudar as permissões';
    } else if (isset($_FILES['arquivo']['tmp_name'])) {
        if ($_FILES['arquivo']['error'] !== UPLOAD_ERR_OK) {
            echo 'Erro no upload';
        } else {
            $tmp = $_FILES['arquivo']['tmp_name'];

            $image = getimagesize($tmp);

            $ext = str_replace(array('image/', 'x-'), '', $image['mime']);

            $arq .= '.' . $ext;

            if (!$image) {
                echo 'Formato invalido';
            } else if (!move_uploaded_file($tmp, $arq)) {
                echo 'Erro ao mover o arquivo';
            } else {
                echo '<img src="', $arq, '">';
            }
        }
    }
    ?>
   </body>
</html>
  • In the example, use a folder named pasta-upload , it must be in the same folder as index.php

  • The action="" is empty, this causes the upload to be sent to the same page

06.05.2017 / 05:00