Return image upload in index.php

Asked

Viewed 96 times

0

I uploaded an image and by clicking "send" I want you to show me the image on the index.php page. Displaying 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 intended.

  • First, add enctype="multipart/form-data" to the element form. Second, your file will be available at $_FILES["arquivo"], not in $_POST. Third, try reading this material, I believe it will help you.

  • Show on index, okay, but which page is this upload? and which directory is?

  • the image ta on the desktop

1 answer

0


The $_POST do not obtain the file, the correct is $_FILES and the form must have enctype="multipart/form-data", before leaving doing anything I recommend you start studying by the documentation https://secure.php.net/manual/en/features.file-upload.post-method.php.

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 called pasta-upload, it must be in the same folder as index.php

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

  • returns this Warning: move_uploaded_file(folder-upload/586881221590d3f3e4fb356.72248506.jpeg): failed to open stream: Permission denied in /Applications/XAMPP/xamppfiles/htdocs/tm/index.php on line 27 Warning: move_uploaded_file(): Unable to move '/Applications/XAMPP/xamppfiles/temp/phpxee8go' to 'folder-upload/586881221590d3f3e4fb356.72248506.jpeg' in /Applications/XAMPP/xamppfiles/htdocs/tm/index.php on line 27 Error moving file

  • when I open the browser I put the address http://127.0.0.1/tm/ and the form appears and always Warning Warning: chmod(): Operation not permitted in /Applications/XAMPP/xamppfiles/htdocs/tm/index.php on line 16 Error when changing permissions

  • if (!chmod($folder, 0755)) is just change the coding Aki by if (!is_writable($folder))

Browser other questions tagged

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