KDBartleby
KDBartleby

Reputation: 43

Can't upload picture to localhost php - permission denied

So, I'm trying to upload pictures(book cover images, specifically) using a PHP script, but I'm getting a "failed to open stream: Permission denied" error. I'm just doing it through my Ubuntu VM, so I should be able to change any permissions necessary.

Here's my php code:

if (!empty($cover))
        {
            if ((($cover_type == 'image/gif') || ($cover_type == 'image/jpeg') || ($cover_type == 'image/pjpeg') ||
              ($cover_type == 'image/png')) && ($cover_size > 0) && ($cover_size <= MAXFILESIZE) &&
              ($cover_width <= MAXIMGWIDTH) && ($cover_height <= MAXIMGHEIGHT))
            {
                if ($_FILES['cover']['error'] == 0)
                {
                      // Move the file to the target upload folder
                      $target = UPLOADPATH . basename($cover);
                      if (move_uploaded_file($_FILES['cover']['tmp_name'], $target))
                      {
                      }

UPLOADPATH = images/

My php.ini file:

upload_tmp_dir = /var/www/html/my_site/tmp_file_upload/

My apache2.conf file:

<Directory /var/www/html/my_site/tmp_file_upload/
    Options -Indexes
    AllowOverride None
    Require all granted
</Directory>

<Directory /var/www/html/my_site/images/>
     Options -Indexes
</Directory>

I own the tmp_file_upload directory and the images directory, and I have write permissions on both. There are several other similar questions on this site, and that's how I knew to look in the php.ini file and the apache2.conf file, but nothing really answers why it's still not allowing me to add the image.

Upvotes: 1

Views: 1862

Answers (1)

andrew
andrew

Reputation: 9583

You need to set the ownership of the folder so that Apache may write to it

In your terminal type

sudo chown www-data:www-data /var/www/html/my_site/tmp_file_upload/

where www-data is the normal user and group of apache

Upvotes: 2

Related Questions