move_uploaded_file crashing

Asked

Viewed 830 times

1

Hello, guys, all right? I’m having a problem saving images inside the server. This code I found on the internet, but it doesn’t work. Give me some errors. You can help me?

This is the code:

if ( isset( $_FILES[ 'lutadorUm' ][ 'name' ] ) && $_FILES[ 'lutadorUm' ][ 'error' ] == 0 ) {
    echo 'Você enviou o arquivo: <strong>' . $_FILES[ 'lutadorUm' ][ 'name' ] . '</strong><br />';
    echo 'Este arquivo é do tipo: <strong > ' . $_FILES[ 'lutadorUm' ][ 'type' ] . ' </strong ><br />';
    echo 'Temporáriamente foi salvo em: <strong>' . $_FILES[ 'lutadorUm' ][ 'tmp_name' ] . '</strong><br />';
    echo 'Seu tamanho é: <strong>' . $_FILES[ 'lutadorUm' ][ 'size' ] . '</strong> Bytes<br /><br />';

    $arquivo_tmp = $_FILES[ 'lutadorUm' ][ 'tmp_name' ];
    $nome = $_FILES[ 'lutadorUm' ][ 'name' ];

    $extensao = pathinfo ( $nome, PATHINFO_EXTENSION );

    $extensao = strtolower ( $extensao );

    if ( strstr ( '.jpg;.jpeg;.gif;.png', $extensao ) ) {

        $novoNome = uniqid ( time () ) . ".".$extensao;

        $destino = 'imagens/' . $novoNome;

        if (move_uploaded_file ( $arquivo_tmp, $destino ) ) {
            echo 'Arquivo salvo com sucesso em : <strong>' . $destino . '</strong><br />';
            echo ' < img src = "' . $destino . '" />';
        }
        else {
            echo 'Erro ao salvar o arquivo. Aparentemente você não tem permissão de escrita.<br />';
            echo $destino;
        }
    }
    else {
        echo 'Você poderá enviar apenas arquivos "*.jpg;*.jpeg;*.gif;*.png"<br />';
        echo $destino;
    }
}
else {
    echo 'Você não enviou nenhum arquivo!';
}

These are the mistakes:

Warning: move_uploaded_file(images/149149930358e679279a144.jpg): failed to open stream: No such file or directory in C: xampp htdocs back_classes envioUfc.php on line 34

Warning: move_uploaded_file(): Unable to move 'C: xampp tmp php6E3B.tmp' to 'images/149930358e679279a144.jpg' in C: xampp htdocs back_classes envioUfc.php on line 34

Note: I have the folder inside the project folder. And even when I pointed directly to the server, it didn’t work. For example: "localhost/back/images/".

  • What do you mean? "C://users/user/back/images", that’s it?

  • Thank you! That’s right!

1 answer

2


The path of your . php should not be relative to the image folder, just seeing the specific project folder structure to be sure, by the way back_classes\envioUfc.php I believe the folder imagens is out of back_classes then you should wear it like this:

$destino = '../imagens/' . $novoNome;

If it does not work it is because the path is wrong, to adjust you can also try the absolute path (assuming that pas images are inside htdocs):

 $destino = 'C:/xampp/htdocs/imagens/' . $novoNome;

If you’re inside back_classes and failure occurs because you are including the envioUfc.php in another file .php (with include or require) that is on a different level, so you can try this:

 $destino = 'C:/xampp/htdocs/back_classes/imagens/' . $novoNome;

When sending to a production server you have to adjust this absolute path.

An example to facilitate and avoid maintenance or tweaking would be to create a global file that should be included in all files with require_once, in this global.php file should contain this:

<?php
//Define a constante
define('ROOT_PATH', dirname(__FILE__) . '/');

And in the archives as index.php and others add this above all:

<?php
require_once 'global.php';

If you are in a different folder from the folder where index.php is, use it like this:

<?php
require_once '../global.php';

And $destiny must stay like this:

$destino = ROOT_PATH . 'imagens/' . $novoNome;
  • Okay, I’ll do that, thank you!!

  • I’m sorry, but apparently he’s not saving you on the server. On the localhost I find the file inside the images folder, but on the server it is not there. I said it worked because I saw that I had saved the link in the bank

  • Okay, but I used global.php as you suggested.

  • The same error occurs

Browser other questions tagged

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