Upload error [Function.move-uploaded-file]: failed to open stream: Permission denied

Asked

Viewed 1,243 times

1

I’m trying to make a record where I have uploads of images and need to save all this content in different tables. The text part is saving perfectly but the photo part is not saving yet.

Follow the PHP code that saves the data in the database:

<?php
// incluindo o arquivo que faz a conexao com o banco
include ("../includes/conexao.php");
include ("../includes/suc_validacao.php");
include ("../includes/suc.php");

$cdClie = $_POST['COD_IDENT_CLIEN'];
$titulo = $_POST['TXT_TITUL_PUBLI'];
$status = $_POST['FLG_STATU_PUBLI'];
$resumo = $_POST['TXT_RESMO_PUBLI'];
$msg = $_POST['TXT_DETLH_PUBLI'];
$link = $_POST['TXT_LINKX_URLXX'];
$usurLoga = $_SESSION['UsuarioID'];

$imagem = $_FILES["imagem"];


$query = "INSERT INTO tbl_PUBLICACOES (COD_IDENT_CLIEN, TXT_TITUL_PUBLI, FLG_STATU_PUBLI, FLG_TIPOX_PAGIN, TXT_RESMO_PUBLI, TXT_DETLH_PUBLI, TXT_LINKX_URLXX,  COD_IDULT_ATUAL, DAT_ULTIM_ATUAL) VALUES";
$query .=  "('$cdClie','$titulo','$status','012','$resumo','$msg','$link','$usurLoga', now())";

$inserir = mysql_query($query)
or die(error());

$response = array("success" => true);

// Recebeu a imagem
if($imagem != NULL):
    $nomeFinal = time().'.jpg'; 

    // Tenta gravar o arquivo no servidor
    if (move_uploaded_file($imagem['tmp_name'], $nomeFinal)): 

        // Pega a imagem
        $tamanhoImg = filesize($nomeFinal); 
        $mysqlImg = addslashes(fread(fopen($nomeFinal, "r"), $tamanhoImg));
        $linkIMG = $_POST['TXT_LINKX_URLXX'];
        $msgImg = $_POST['MEM_LEGEN_IMAGM'];

        // Conecta-se ao BD e tenta gravar
        try{
            $query = mysqli_query("INSERT INTO tbl_IMAGENS (BLO_CONTE_IMAGM, MEM_LEGEN_IMAGM, TXT_LINKX_URLXX, COD_IDULT_ATUAL, DAT_ULTIM_ATUAL ) VALUES ('$mysqlImg', '$msgImg', '$linkIMG', '$usurLoga', now())");
        } catch( PDOException $e ) {
            echo $e->getMessage();
        }

        // Apaga o arquivo
        unlink($nomeFinal); 
    endif;

else:
    echo"Você não realizou o upload de forma satisfatória."; 
endif;

//fechando a conexao com o banco
mysql_close($conn);

    header("Location: publicacaoPF.php?P_COD_IDENT_CLIEN=$cdClie"); exit; // Redireciona o visitante

?>

Page code where I send the image:

<input id="files" name="imagem" type="file" multiple/>
                <output id="result" />

However my project has a particularity I do the preview of the image and together with the preview i specify 2 more fields to be added for each image, one of these fields is the caption and the link if it exists, I do so using the jquery.

Follow the jquery code:

 <script>

window.onload = function(){

//Check File API support
if(window.File && window.FileList && window.FileReader)
{
    var filesInput = document.getElementById("files");

    filesInput.addEventListener("change", function(event){

        var files = event.target.files; //FileList object
        var output = document.getElementById("result");

        for(var i = 0; i< files.length; i++)
        {
            var file = files[i];

            //Only pics
            if(!file.type.match('image'))
              continue;

            var picReader = new FileReader();

            picReader.addEventListener("load",function(event){

                var picFile = event.target;

                var div = document.createElement("div");

                div.innerHTML = "<img class='thumbnail' src='" + picFile.result + "'" +
                        "title='" + picFile.name + "'/> <a href='#' class='remove_pict del'><i class=\"icon-remove\"></i></a> <div class=\"control-group\">                         <label class=\"control-label\" for=\"MEM_LEGEN_IMAGM\">Legenda</label>                          <div class=\"controls\"><input id=\"MEM_LEGEN_IMAGM\" name=\"MEM_LEGEN_IMAGM\" type=\"text\" class=\"input-xlarge\">     </div>                </div>         <div class=\"control-group pull-right linkImg\"><label class=\"control-label\" for=\"TXT_LINKX_URLXX\">Link</label><div class=\"controls\"><input id=\"TXT_LINKX_URLXX\" name=\"TXT_LINKX_URLXX\" type=\"text\" class=\"input-xlarge\">        </div>            </div>";

                output.insertBefore(div,null);   
                div.children[1].addEventListener("click", function(event){
                   div.parentNode.removeChild(div);
                });         

            });

             //Read the image
            picReader.readAsDataURL(file);
        }                               

    });
}
else
{
    console.log("Your browser does not support File API");
}
}

The mistake you’re making is :

[Fri Jun 26 12:37:24 2015] [error] [client 192.168.1.105] PHP Warning: move_uploaded_file(1435311444.jpg) [Function.move-uploaded-file]: failed to open stream: Permission denied in /opt/lampp/htdocs/Renan/jrassessoria/admin/paginas/C_salvapublicacaopf.php on line 35, referer: http://192.168. 1.20/Renan/jrassessoria/admin/paginas/cadastroPublicacaoPF.php? P_COD_IDENT_CLIEN=12 [Fri Jun 26 12:37:24 2015] [error] [client 192.168.1.105] PHP Warning: move_uploaded_file() [Function.move-uploaded-file]: Unable to move '/tmp/phpMAzIYb' to '1435311444.jpg' in /opt/lampp/htdocs/Renan/jrassessoria/admin/paginas/C_salvapublicacaopf.php on line 35, referer: http://192.168. 1.20/Renan/jrassessoria/admin/paginas/cadastroPublicacaoPF.php? P_COD_IDENT_CLIEN=12

After changing some things in the code gave this error:

[Fri Jun 26 16:13:51 2015] [error] [client 192.168.1.105] PHP Warning: filesize() >[Function.filesize]: stat failed for 1435324431.jpg >in /opt/lampp/htdocs/Renan/jrassessoria/admin/paginas/C_salvarpublicacaopj.php on >line 31, referer: >http://192.168. 1.20/Renan/jrassessoria/admin/paginas/cadastroPublicacaoPJ.php?>P_COD_IDENT_CLIEN=17 [Fri Jun 26 16:13:51 2015] [error] [client 192.168.1.105] PHP Warning: >fopen(1435324431.jpg) [Function.fopen]: failed to open >stream: No such file or directory in >/opt/lampp/htdocs/Renan/jrassessoria/admin/paginas/C_salvarpublicacaopj.php on referline >32, er: >http://192.168. 1.20/Renan/jrassessoria/admin/paginas/cadastroPublicacaoPJ.php?>P_COD_IDENT_CLIEN=17 [Fri Jun 26 16:13:51 2015] [error] [client 192.168.1.105] PHP Warning: fread() >expects Parameter 1 to be Resource, Boolean Given in >/opt/lampp/htdocs/Renan/jrassessoria/admin/paginas/C_salvarpublicacaopj.php on line >32, referer: >http://192.168. 1.20/Renan/jrassessoria/admin/paginas/cadastroPublicacaoPJ.php?>P_COD_IDENT_CLIEN=17 [Fri Jun 26 16:13:51 2015] [error] [client 192.168.1.105] PHP Notice: Undefined >index: MEM_LEGEN_IMAGM in >/opt/lampp/htdocs/Renan/jrassessoria/admin/paginas/C_salvarpublicacaopj.php on line >34, referer: >http://192.168. 1.20/Renan/jrassessoria/admin/paginas/cadastroPublicacaoPJ.php?>P_COD_IDENT_CLIEN=17

He’s even saving but doesn’t get my caption fields and link.

  • It seems to be something to do with folder permission on the server, already thought about converting the photo to Base64 string and saving it in the bank as well?

  • Not how I do it ?

  • I think the answer to this question might be helpful,http://stackoverflow.com/questions/3967515/how-to-convert-image-to-base64-encoding

  • Later I come back here and put a more complete solution, I’m out of time now

  • @Kaizonaro I keep waiting.

  • 4
Show 1 more comment

1 answer

1

You are using lampp. In this case the permissions for the folder are probably configured for your local user. In order to be able to move the object, Voce has to give permissions to the apache user (in this case it must be the folder’s Owner, or at least have permissions to write to it). The default user of apache is www-data. The command below should solve your problem.

chown -R www-data:www-data minha_pasta_www
  • I’ll test and tell you.

  • also use chown -R www-data:www-data /tmp

Browser other questions tagged

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