Why does php not return the extension of all images when renaming it?

Asked

Viewed 25 times

-1

I need to rename images when uploading, however, some images the code can not take the extension to finish the process correctly.

Thus ending: maisguarapari_5c4a3bec18563. (i.e., saving the image without the extension)

I try several times with the same image, but without success.

This is one of the images that I can’t get its extension from the code: https://www.maisguarapari.com.br/certo.jpg

Important to remember that, I take photos by my phone, the dozens of photos I send, some photos the code cannot catch its extension.

PHP

$nome = $_FILES['userfile']['name'];
$extensao = strtolower(end(explode('.', $nome)));
$nomenovoorigem = uniqid('maisguarapari_');
$nomenovo = $nomenovoorigem.".".$extensao;

HTML

<form class="frm_mural" action="/mural-gravar" method="post" enctype="multipart/form-data">
<input class="entrada_frm_mural" required="required" name="userfile" type="file">
<button class="entrada_frm_mural_enviar" type="submit">Enviar</button>
</form>
  • What is the print_r from $_FILES when this happens?

  • @Andersoncarloswoss I used this code print_r($_FILES); and returned that Array ( )

  • Even sending the file through the form? The result seems that no file was sent. By the way, you had not asked this question?

  • @Andersoncarloswoss yes, but in another context. This case is very specific.

  • @Andersoncarloswoss make the direct test by the link with the code that Bulfaitelo sent https://www.maisguarapari.com.br/teste.php

  • The problem with being very specific is that it turns out to be non-reproducible. I recommend that you edit the question by putting all the possible information in the situation that the problem occurs, such as the request that the browser does, values of the superglobal in PHP, if JS influences the upload, server logs, etc.

  • @Andersoncarloswoss Even using only his code, IE, totally dry, does not work to get the extension of the image that provided the link.

Show 3 more comments

1 answer

0

You can use the function pathinfo();:

pathinfo - Returns information about a file path

$path = $_FILES['userfile']['name'];
$extensao = pathinfo($path, PATHINFO_EXTENSION);
$nomenovoorigem = uniqid('maisguarapari_');
$nomenovo = $nomenovoorigem.".".$extensao;

Applied Example:

<!DOCTYPE html>
<html lang="en">
<body>
    <form class="frm_mural" action="index.php" method="post" enctype="multipart/form-data">
        <input class="entrada_frm_mural" required="required" name="userfile" type="file">
        <button class="entrada_frm_mural_enviar" type="submit">Enviar</button>
    </form>
</body>
</html>
<?php
$path = $_FILES['userfile']['name'];
$extensao = pathinfo($path, PATHINFO_EXTENSION);
$nomenovoorigem = uniqid('maisguarapari_');
$nomenovo = $nomenovoorigem.".".$extensao;

var_dump($nomenovo);
  • Still the code could not get the extension of the image above the link.

  • Because this image on the link above, is one of several photos that I have and that the code can not get its extension.

  • In case the image is being sent by a form? , I will mount the environment here and test.

  • Yes, by the form.

  • Rapais set his environment here and it worked perfectly, I will update the example. test there and see if it works

  • Look, it doesn’t work for this image https://www.maisguarapari.com.br/teste.php

  • I put all your code on the link https://www.maisguarapari.com.br/teste.php but it still doesn’t work. Upload the image to Voce see https://www.maisguarapari.com.br/certo.jpg

  • Now I see your problem. This image is 7mb; updates the question, or posts here your php.ini (the memory part) with a smaller image certainly works and service configuration

Show 4 more comments

Browser other questions tagged

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