Viewed   1.5k times

I keep getting this error when trying to configure the upload directory with Apache 2.2 and PHP 5.3 on CentOS.

In php.ini:

upload_tmp_dir = /var/www/html/mysite/tmp_file_upload/

In httpd.conf:

Directory /var/www/html/mysite/tmp_file_upload/>
    Options  -Indexes
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>
<Directory /var/www/html/mysite/images/>
                Options -Indexes
</Directory>

CentOS directory permissions:

drwxrwxr-x 2 root root 4096 Nov 11 10:01 images
drwxr-xr-x 2 root root 4096 Nov 12 04:54 tmp_file_upload

No matter what I do, I keep getting this error from PHP when I upload the file:

Warning: move_uploaded_file(images/robot.jpg): failed to open stream: Permission denied in /var/www/html/mysite/process.php on line 78

Warning: move_uploaded_file(): Unable to move '/tmp/phpsKD2Qm' to 'images/robot.jpg' in /var/www/html/mysite/process.php on line 78

As you can see, it never did take the configuration from the php.ini file regarding the upload file.

What am I doing wrong here?

 Answers

4

This is because images and tmp_file_upload are only writable by root user. For upload to work we need to make the owner of those folders same as httpd process owner OR make them globally writable (bad practice).

  1. Check apache process owner: $ps aux | grep httpd. The first column will be the owner typically it will be nobody
  2. Change the owner of images and tmp_file_upload to be become nobody or whatever the owner you found in step 1.

    $sudo chown nobody /var/www/html/mysite/images/
    
    $sudo chown nobody /var/www/html/mysite/tmp_file_upload/
    
  3. Chmod images and tmp_file_upload now to be writable by the owner, if needed [Seems you already have this in place]. Mentioned in @Dmitry Teplyakov answer.

    $ sudo chmod -R 0755 /var/www/html/mysite/images/
    
    $ sudo chmod -R 0755 /var/www/html/mysite/tmp_file_upload/
    
  4. For more details why this behavior happend, check the manual http://php.net/manual/en/ini.core.php#ini.upload-tmp-dir , note that it also talking about open_basedir directive.

Monday, October 24, 2022
2

Typically, we use ftp in these situations. /public_html permissions may remain to 750 and run this code.

$server = 'localhost';
$ftp_user_name = 'username';
$ftp_user_pass = 'passw';
$dest = 'public_html/new.file';
$source = '/home/username/public_html/path/to/existing.file';

$connection = ftp_connect($server);
$login = ftp_login($connection, $ftp_user_name, $ftp_user_pass);
if (!$connection || !$login) { die('Ftp not connected.'); }
$copied = ftp_put($connection, $dest, $source, FTP_BINARY);
if ($copied) { 
    echo 'File copied';
} else {
    echo 'Copy failed!'; 
}
ftp_close($connection);

The page with final destination in public_html can be created in the other directory and then this script will copy it in public_html. The old file will remain and if a file exists with the same destination name will be overwritten.
The $dest is relative path to user home directory. The $source is absolute path.
The connection will fail if the ftp is concurrently used by filezilla or something. A solution to that is to create a second ftp user account in cPanel.

Tuesday, November 1, 2022
 
5

There is an error in your code:

You need to change your move_uploaded_file funciton. There is an extra space i think which is causing the problem:

move_uploaded_file($_FILES["ref1pod"]["tmp_name"],"/home/hulamyxr/public_html/POD/" .($ref1.".pdf"));

Also i am not sure where is the

$ref1 = $_POST[ref1]; //this is the name I want the file to be
$ref1pod = $_POST[ref1pod];

coming from .There is no such values in your form. Did you upload only the form with upload only. Also be sure to put quotes around attribute values in your form and post value.

Is ref1 and ref1pod are constants. If you din't put quotes PHP will take it as constants. If they are not constants change to:

$ref1 = $_POST['ref1']; //this is the name I want the file to be
$ref1pod = $_POST['ref1pod'];

Also in your form, put quotes:

<form enctype="multipart/form-data" method="post" action="capturelocal2.php">
     <input type="file" size="6" name="ref1pod" id="ref1pod"/>
</form>

Be sure you set permissions to your upload folder .

Hope this helps you :)

Wednesday, November 30, 2022
 
blindy
 
3

Thanks to the comment left on my original post I was able to figure it out with a little more help from http://www.howyoudo.info/index.php/how-to-fix-windows-server-upload-file-inherit-permissions-error/

The problem only happens when you use PHP to upload a file. When you upload a file, PHP sends the file to a temporary directory on the hard drive (for me it is C:WindowsTemp) and then copies it over to it’s intended directory. Once the file has landed in the temporary directory, it is assigned the permissions of that directory. The problem is when Windows copies that file, it keeps the temporary directory’s permissions and doesn’t inherit your web directory’s permissions.

The easiest way to fix this problem is to add to the temporary directory your intended web directory’s permissions. There’s no need to erase the permissions already in the temporary directory, just add the web directory’s permissions to them. In other words, follow these steps

  1. To change the permissions of your temporary upload directory, find the “upload_tmp_dir” in your php.ini file.
  2. Set it to the directory of your choosing (outside your web folders of course) or leave it at default (for me it is C:WindowsTemp).
  3. Browse to this folder and add the permissions of your web folders to it.
Sunday, November 20, 2022
1

I assume that maybe your user has rights over /opt/lampp and because of that you can edit the files. However, if that's not the case you can grant access to both, your user and the apache group. The next shows firs the user, then the group:

sudo chown -R youruser:www-data /opt/lampp

In that way you're setting the ownership to the user and then the group. After that you must grant the access for the three groups: User, group and others:

sudo chmod -R xxx /opt/lampp

Note: The rightmost refer to permissions for the file owner, then the group and other users.

Now, what does the xxx mean?

No - Permission - rwx

7 - full - 111

6 - read and write - 110

5 - read and execute - 101

4 - read only - 100

3 - write and execute - 011

2 - write only - 010

1 - execute only - 001

0 - none - 000

Or you can use:

chmod [reference][operator][mode] fileOrFolder

Where the reference is: u(ser), g(roup), o(thers) and a(ll).

The operator is: + (adds the specified modes to the specified references), - (removes) and = (the modes specified must be made the exact modes for the specified references)

Finally the modes are: r(ead), w(rite), x(execute), [X(special execute), s and t (that are not so common)].

Hope this is what you're looking for.

Wednesday, October 19, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :