1

I'm trying make a file upload using PHP. To do it I'm using $_FILE but I can't understand why does not works. Looking for solutions I'm found some suggestion to use $_FILE, but still can't do this works. To I see if upload works I'm using Postman of Chrome. I'm using Ubuntu with LAMP.

How could I do it ?

<?php

$arquivo = isset($_FILES["file"]) ? $_FILES["file"] : FALSE;

if(!$arquivo) { 
    echo "You can not access this file directly!"; 
}else{
    $diretorio = "/home/fernando/Imagens/";

    if (move_uploaded_file($arquivo["tmp_name"], $diretorio.$arquivo["name"])) { 
            echo "File upload ok!"; 
    }else{
         echo "File not upload!"; 
    }   
}

?>

Exception

<br />
<b>Warning</b>:  move_uploaded_file(/home/fernando/Imagens/avatar_empresa.jpg): failed to open stream: Permission denied in 
<b>/var/www/TelefonesUteis/ws/add_file.php</b> on line 
<b>12</b>
<br />
<br />
<b>Warning</b>:  move_uploaded_file(): Unable to move '/tmp/phpq9AlVw' to '/home/fernando/Imagens/avatar_empresa.jpg' in 
<b>/var/www/TelefonesUteis/ws/add_file.php</b> on line 
<b>12</b>
<br />

Postman

enter image description here

FernandoPaiva
  • 4,410
  • 13
  • 59
  • 118

1 Answers1

2

Your script doesn't have permissions to add and/or execute files in /home/fernando/Imagens/. Your will have to use chmod:

chmod -R 775 /home/fernando/Imagens

Marcus Olsson
  • 2,485
  • 19
  • 35
  • `775` should be enough, don't need to execute anything there surely? – AStopher Feb 24 '15 at 19:46
  • 1
    Yes, you are correct `775` is probably a safer alternative in most cases. Edited my answer. – Marcus Olsson Feb 24 '15 at 19:50
  • Remember that CHMOD only gives permissions to the current files there, if you're gonna add new folders/files they will be blocked by default, you would have to repeat the chmod thing each time your application uploads or generates a new thing inside that folder. Dealing with web applications on Linux (that make use of uploads or file management) is a HEADACHE, enough said hahaha, get ready to use a lot of console. – JuanBonnett Feb 24 '15 at 19:55
  • 1
    Yes, but his problem is that he doesn't have permissions to write files to the folder. If he use chmod 775 or 777, he will have those permissions. – Marcus Olsson Feb 24 '15 at 19:57