move_uploaded_file failing

1

Hello, guys, how are you? I have a problem to save images inside the server. This code I found on the internet, but it does not work. It returns me some errors. Can you 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!';
}

Here are the errors:

  

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

     

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

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

    
asked by anonymous 06.04.2017 / 19:26

1 answer

2

The path of your .php should not be relative to folder images, just seeing the folder structure of the specific project to be sure, through the path back_classes\envioUfc.php I believe that the imagens folder is outside of back_classes so should use this:

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

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

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

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

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

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

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

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

And in files like index.php and others add this above all else:

<?php
require_once 'global.php';

If it is in a different folder than the index.php folder, use this:

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

And the destination $ should look like this:

$destino = ROOT_PATH . 'imagens/' . $novoNome;
    
06.04.2017 / 19:49