• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 425
  • Last Modified:

File upload problem (PHP/Apache/Linux)

I know this is a common problem, but the solutions I've found so far do not work. I'm having a error like this: Warning: move_uploaded_file(/home/aixa/www/Phoenix/products/lite-table_main.jpg): failed to open stream: Permission denied in /home/aixa/www/Phoenix/cpanel/cpanel_functions.inc on line 249

The folder where I want to put the file into has the correct permissions. So does its parent folder, and the same is true for the temp folder. The code I have is as follows:

function file_upload($dest){
   if($dest=="products"){  
      $up_filename = $_FILES['nu_img']['name'];
      $up_dir = "/home/aixa/www/Phoenix/products/";
      $up_tmpname = $_FILES['nu_img']['tmp_name'];
   }
   if($dest=="restore"){
      $up_filename = $_FILES['nu_bk']['name'];
      $up_dir = "/home/aixa/www/Phoenix/cpanel";
      $up_tmpname = $_FILES['nu_bk']['tmp_name'];
   }
   $up_file = $up_dir . $up_filename;
   if (move_uploaded_file($up_tmpname, $up_file)) { # If uploading successful
      if ($dest == "products"){
         if ($_FILES['db_back']['type'] == "image/jpeg"){ # If correct file type
            $salida = $up_filename;
         }
         else { # Error handling for wrong filetype
            echo "javascript:alert('Wrong or Invalid File');";
         }
      }
      if ($dest == "restore"){
         if ($_FILES['db_back']['type'] == "text/plain"){ # If correct file type
            $salida = $up_filename;
         }
         else { # Error handling for wrong filetype
            echo "javascript:alert('Wrong or Invalid File');";
         }
      }
   }
   else { # Error handling for unsuccessful upload
      $salida = "Error!";
   }
   return $salida;
}

# and I call this function like this:

 $uploaded_file=file_upload("products");

after I sent a form that includes the part:

<form ...>
<input name="MAX_FILE_SIZE" value="1300000" type="hidden"> Upload new image: <input name="nu_img" class="text" type="file">
</form>

Any comments? what am I missing?
0
aixarat
Asked:
aixarat
  • 2
  • 2
3 Solutions
 
hernst42Commented:
I only get this error-message if that directory exits, but is not writeable by the webserver user. So how are the permisisons on the directory /home/aixa/www/Phoenix/products/ and under which user is the webserver running? If that directory is writeable by the webserver user It will overwrite any file in that directory regardingless of the permission of that file.

So far I can't locate any errors in that script which might cause that error.

So recheck the permissions again
0
 
Diablo84Commented:
Usually the permission is set to 666 so the directory has write access but commonly it must be 777 for the upload to work.

As long as your script and server is secure 777 permission will not be a problem.
0
 
minichickenCommented:
Hi aixarat

Are you using CPanel to manage your site?

Don't know if you already know this.

When you login to Cpanel, you can goto to the "File Manager"
In the file manager, browse down to the actual folder that you would like to upload the file to.
Click on the name of the folder and not the image of the folder. On the right of the window (Maybe different on different Cpanel versions) select "Change Permissions" and then check all the checkboxes and then click "change". The folder now should have "777" as the permission.

Note: you have to set "777" to the actual folder and not its parent folder.

Hope this helps

j311y:)
0
 
minichickenCommented:
We all provided suggested solutions, but no reply from author :(
0
 
Diablo84Commented:
I recommend split between the three participants.

Its fairly likely that the problem was as i said above, the permission was set as 666 not 777 and thats what everyone was touching on.

side note: Venabili, thanks for your efforts cleaning up the PHP TA, its appreciated.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now