Image upload

Asked

Viewed 1,490 times

2

how can I upload an image, and then display it on another page, with no database, for example

I have a form on my "initial" page, the person uploads the image, when he makes the image appears on the page "images"

1 answer

1


Wender, you will need to store this image in question. Below follows a simple upload routine:

index.html

<form action="exibir.php" method="POST" enctype="multipart/form-data">
 <input type="file" name="img">
 <input type="submit" name="img_ok" value="Enviar">
</form>

view.php

<?php
 if($_POST["img_ok"]) {
  $tempname = $_FILES["img"]["tmp_name"];
  $name = uniqid();
  $extension = strtolower(pathinfo($_FILES["img"]["name"], PATHINFO_EXTENSION)); // Pega extensão de arquivo e converte em caracteres minúsculos.

  $url_exibir = "/img/".$name.".".$extension; // Caminho para exibição da imagem.      

  $url = "./img"; // Pasta onde será armazenada a imagem.
  if(!file_exists($url)) { // Verifica se a pasta já existe.
   mkdir($url, 0777, TRUE); // Cria a pasta.
   chmod($url, 0777); // Seta a pasta como modo de escrita.
  }

  move_uploaded_file($tempname, $url."/".$name.".".$extension); // Move arquivo para a pasta em questão.
 }
?>

<!-- Exibir imagem de upload -->
<img alt="" title="" src="<?php echo $url_exibir; ?>">

ADDED - For more than one image follows basis for implementation:

index.html

<form action="exibir.php" method="POST" enctype="multipart/form-data">
 <input type="file" name="img[]" multiple><br />
 <input type="submit" name="img_ok" value="Enviar">
</form>

view.php

<?php
    if($_POST["img_ok"]) {

    for($i = 0; $i < count($_FILES["img"]); $i++) {
        $tempname = $_FILES["img"]["tmp_name"][$i];
        $name = uniqid();
        $extension = strtolower(pathinfo($_FILES["img"]["name"][$i], PATHINFO_EXTENSION)); // Pega extensão de arquivo e converte em caracteres minúsculos.

        $url_exibir[$i] = "/img/".$name.".".$extension; // Caminho para exibição da imagem.      

        $url = "./img"; // Pasta onde será armazenada a imagem.

        if(!file_exists($url)) { // Verifica se a pasta já existe.
            mkdir($url, 0777, TRUE); // Cria a pasta.
            chmod($url, 0777); // Seta a pasta como modo de escrita.
        }

        move_uploaded_file($tempname, $url."/".$name.".".$extension); // Move arquivo para a pasta em questão.
    }

 }
?>

<?php for($i = 0; $i < count($url_exibir); $i++) { ?>
    <!-- Exibir imagem de upload -->
    <img alt="" title="" src="<?php echo $url_exibir[$i]; ?>">
<?php } ?>
  • Hello Thyago thank you for helping me, the image is being sent correctly but it is not being displayed, I used the inspect element, and it seems to me that this duplicating the extension. , for example jpg.jpg <img alt="" title="" src="/img/5622846c0486a.png.png"> , is it possible to send more than one image? type in the page display.php show a list <ul> <li>IMAGE-1</li> <li>IMAGE-2</li> <li>IMAGE-3</li> </ul>

  • @Wender, I tested the code I posted in the reply and it worked without duplication of extension, ideal would be you analyze the code again.

  • I analyzed it again and it’s working right Thyago, Thank you very much! , how do I send more than one image? I tried to modify more here without success.

  • I edited the answer and includes the other situation you requested in the case of more than one image, I hope it helps, remembering that the code can be improved.

  • Thank you very much! helped me a lot.

Browser other questions tagged

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