Invalid argument for chmod()?

Asked

Viewed 44 times

0

I’m trying to receive the dimensions of each photo sent, but when I run the file happens this:

Error:

  Warning: chmod(): Invalid argument in C:\Program Files (x86)\EasyPHP-
  Devserver-17\eds-www\PPI\adm\cadastrar_jogo.php on line 15

  Warning: getimagesize(): Filename cannot be empty in C:\Program Files   
  (x86)\EasyPHP-Devserver-17\eds-www\PPI\adm\cadastrar_jogo.php on line 16

Here is the part of the code that is giving the error:

    for ($i = 0; $i < count($_FILES['img']); $i++){
    if (isset($_FILES['img']['name'][$i])){
    if (!empty($_FILES['img']['name'][$i])){
     chmod($_FILES['img']['tmp_name'][$i], 0777);
      list ($larg, $alt) = getimagesize($_FILES['img']['tmp_name'][$i]);
          $verifica_quant_img++;

How can I solve the problem, and what exactly is the problem?

1 answer

0

But there’s not much point in using chmod in a file that is in the folder ./tmp operating system, you must first use the move_upload and then apply chmod in the new destination.

It would also be interesting to check if there was an error with the $_FILES['img']['error'], see an example of use:

if (empty($_FILES['imagem']['name'])) {
    echo 'Você não selecionou nenhum arquivo';//Aqui você pode trocar por um alert ou customizar como desejar, é um aviso que o usuário provavelmente não selecionou nada
} else {
    $arquivos = $_FILES['img'];
    $total = count($arquivos['name']);

    for ($i = 0; $i < $total; $i++) {
        $nome = $arquivos['name'][$i];

        if ($arquivos['error'][$i] !== UPLOAD_ERR_OK) {
            echo 'Erro ao fazer upload de ', htmlspecialchars($nome), '<br>';
            continue;
        }

        $destino = 'pasta/foo/bar/' . $nome;

        if (move_uploaded_file($arquivos['tmp_name'][$i], $destino)) {
            echo 'O arquivo ', htmlspecialchars($nome),' foi carregado<br>';
            chmod($destino, 0777);
        } else {
            echo 'O arquivo ', htmlspecialchars($nome),' não foi carregado<br>';
        }
    }
}

However, I think that using 0777 is exaggeration for images, maybe 644 is more than enough.

  • Hello William, the problem is that I am sending files in input, but when I send a PNG type image the error of the function getimagesize(). So I used chmod() because I thought it was something related to permissions, since PNG files are usually bigger...

  • @shelldude may be upload failure, make the check first (above all) as in the example if ($arquivos['error'][$i] !== UPLOAD_ERR_OK)

Browser other questions tagged

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