Hi,

I'm trying to upload an image but ir gives me error on 'move_upload_file'.

Warning: move_uploaded_file(http://localhost/administrador/images/projects/54031d9e94d63a24dba816ddacbf315e.jpg): failed to open stream: HTTP wrapper does not support writeable connections in C:\wamp\www\administrador\includes\uploadImage.php on line 38

Warning: move_uploaded_file(): Unable to move 'C:\wamp\tmp\php5E59.tmp' to 'http://localhost/administrador/images/projects/54031d9e94d63a24dba816ddacbf315e.jpg' in C:\wamp\www\administrador\includes\uploadImage.php on line 38

This is my code:

form.php

<form role="form" method="POST" enctype="multipart/form-data" action="/administrador/includes/projectValidations.php/'.$action.'">
    <div class="form-group">
        <label for="projectLogo">Logotipo</label>

        <input type="file" id="projectLogo" name="projectLogo" placeholder=""
        data-val="true" data-val-required="Deve escolher a imagem do projecto." />

        <p class="field-validation-valid" data-valmsg-for="projectLogo" data-valmsg-replace="true"></p>
    </div>

projectValidations.php

 if(isset($_POST['formValid']))
{
  $error = array();
  //Nome do Projecto
  if($_POST['projectNome']=='')
  {
   $error['nome']='Nome é obrigatório.';
  }
  else
   $nome=$_POST['projectNome'];

  //Website do Projecto
  if($_POST['projectUrl']=='')
  {
   $error['url']='URL é obrigatório.';
  }
  else
   $website=$_POST['projectUrl'];

  //Logo do Projecto
  include('uploadImage.php');

  if(count($error) != 0)
  {
   echo '<pre style="float:left; width:100%; margin-top:100px;">'.print_r($error, true).'</pre>';
  }
  else
  {
   echo '<pre style="float:left; width:100%; margin-top:100px;">YEAHH</pre>';
  }
  die;

uploadImage.php

<?php
define ('SITE_ROOT', realpath(dirname(__FILE__)));

echo '<pre style="float:left; width:100%; margin-top:100px;">';
    if(isset($_FILES['projectLogo']['name']))
    {
        /*
            Nome: $_FILES['nomeInput']['name'] (imagem1.jpg)
            Tipo: $_FILES['nomeInput']['type'] (image/jpeg)
            Nome Temporário: $_FILES['nomeInput']['tmp_name'] (/var/tmp/php6jqBHz)
            Tamanho: $_FILES['nomeInput']['size'] (1048576 Bytes - 1Mb)
        */

        $arquivo_tmp = $_FILES['projectLogo']['tmp_name'];
        $nome = $_FILES['projectLogo']['name'];
        $tamanho = $_FILES['projectLogo']['size'];

        $maxSize = '2097152'; //2Mb

        // Verificar extensão da imagem
        $extensao = strrchr($nome, '.');

        // Converte a extensão para minúsculo
        $extensao = strtolower($extensao);

        // Somente imagens, .jpg .jpeg .png
        // Separar extensões, pretendidas, por ';'
        if(strstr('.jpg;.jpeg;.gif;.png', $extensao))
        {
            // Criar um nome único para a imagem
            // Evitar imagens duplicadas no servidor.
            $novoNome = md5(microtime()) . $extensao;

            // Pasta para onde enviamos a imagem
            $destino = '/administrador/images/projects/' . $novoNome;
            $destino = SITE_ROOT.$destino;

            if($tamanho <= $maxSize )
            {
                if (move_uploaded_file($arquivo_tmp, $destino) == true) 
                {
                    echo "File has been moved from " . $arquivo_tmp . " to" . $destino;
                    die;
                }
                else 
                {
                    echo $error['logo']='Erro ao salvar o arquivo.';
                }


                die;
            }
            else
                $error['logo']='Tamanho máximo é de 2Mb.';

        }
    }
    else
    {
        // $error - Array de validação do ficheiro '/administrador/includes/projectValidations.php'
        $error['logo']='A imagem é obrigatória.';
    }
echo '</pre>';
?>

You can only move a file to an absolute or relative path. So you'll have to replace SITE_ROOT with an actual path to your folder.

It doesn't work, i have this:

// Pasta para onde enviamos a imagem
$destino = '/administrador/images/projects/' . $novoNome;

if($tamanho <= $maxSize )
{
    if (move_uploaded_file($arquivo_tmp, $destino) == true) 
    {
        echo "File has been moved from " . $arquivo_tmp . " to" . $destino;
        die;
    }
    else 
    {
        echo $error['logo']='Erro ao salvar o arquivo.';
    }
    die;
}

It doesn't work

What doesn't work? Same error? Different one? Doesn't work is too vague.

Sorry, is the same error. I read that i have to replace '/' to '\', but it gives me another error:

( ! ) Parse error: syntax error, unexpected 'logo' (T_STRING) in C:\wamp\www\administrador\includes\uploadImage.php on line 4

(...)

if(strstr('.jpg;.jpeg;.gif;.png', $extensao))
        {
            // Criar um nome único para a imagem
            // Evitar imagens duplicadas no servidor.
            $novoNome = md5(microtime()) . $extensao;

            // Pasta para onde enviamos a imagem
            $destino = '/administrador/images/projects/' . $novoNome;
            $destino = str_replace('/', '\', $destino);

            if($tamanho <= $maxSize )
            {
                if (move_uploaded_file($arquivo_tmp, $destino) == true) 
                {
                    echo "File has been moved from " . $arquivo_tmp . " to" . $destino;
                    die;
                }
                else 
                {
                    echo $error['logo']='Erro ao salvar o arquivo.';
                }


                die;
            }
            else
                $error['logo']='Tamanho máximo é de 2Mb.';
        }

Sorry, is the same error

Show it anyway please.

Warning: move_uploaded_file(/administrador/images/projects/c7bd1d52522236f785bb79ca3f6e634a.jpg): failed to open stream: No such file or directory in C:\wamp\www\administrador\includes\uploadImage.php on line 39

Warning: move_uploaded_file(): Unable to move 'C:\wamp\tmp\php606E.tmp' to '/administrador/images/projects/c7bd1d52522236f785bb79ca3f6e634a.jpg' in C:\wamp\www\administrador\includes\uploadImage.php on line 39

I think you need to prepend $destino with /wamp/www/

It worked. So, if i do this:

$baseUrl='/wamp/www'; //when i'm working with localhost
$baseUrl='http://www.mywebsite.com' //when i'm on the server

$destino=$baseUrl.'/administrador/images/projects/'.$novoNome;

It should work out fine, right?

By the way thanks for your help. ;-)

It should work out fine, right?

No. On the webserver it should also be an absolute path, but that depends on your host. On mine it's something like:

/home3/prit/public_html
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.