Difficulty Uploading File

in question here is not so much in the programming (which is why I’ve not posted all the code) but in the configuration or possibly in the new versions of PHP.

The file is checked for being valid and in the proper format but I think the issue is not in all that but rather than the file isn’t being created in the /tmp folder even though that’s not what the actual error says. This is the bit that does the uploading.

if (move_uploaded_file($_FILES['UploadFile']['tmp_name'], $target_file)) :
	$Message = "The file ". htmlspecialchars( basename( $_FILES['UploadFile']['name'])). " has been uploaded.";
else :
	$Message = "Sorry, there was an error uploading your file.";
endif;

The actual errors:

Warning: move_uploaded_file(/var/www/html/domain.loc/internals/images/Front.jpg): Failed to open stream: Permission denied in /var/www/html/domain.loc/internals/administration/uploadsadmin.php on line 210

Warning: move_uploaded_file(): Unable to move "/tmp/php0IyD5c" to "/var/www/html/domain.loc/internals/images/test/Front.jpg" in /var/www/html/domain.loc/internals/administration/uploadsadmin.php on line 210

Line 210 with the error is the first line in the code above but I see no sign that any tmp file was ever created.

As I’ve always done, the Apache folder and file permissions are set using:

sudo chown -R $USER:www-data /var/www
sudo find /var/www -type d -exec chmod 0755 {}  +
sudo find /var/www -type f -exec chmod 0644 {}  +

Any ideas what’s happening?

The destination - /var/www/html/domain.loc/internals/images/Front.jpg being displayed in the first error message is what the - Failed to open stream: Permission denied … is referring to.

The tmp file exists, because there’s a file name listed - php0IyD5c. The tmp file is automatically deleted when your script ends, so it is only present for the duration of the upload, plus the few ms that your script takes to run.

The only information I can offer is that the user the web server is running under cannot access the destination path/folder.

Thank you and that what it seems like to me too so good that I wasn’t imaging it. Before posting, I tried watching the tmp folder content while uploading but it doesn’t refresh instantly so showed nothing. I’m sure this will work on the live service but I need to finish the reprogramming and testing here on my local development system and that’s where the problem is. The permissions are set as I had indicated which is what they’ve always needed to be and I am the owner but I am also a member of the www-data group which might now be an issue but otherwise I can’t think of any reason why the file won’t go as it was working before on the same PC. Of course, Ubuntu, Apache and PHP have all had updates so I’m wondering if something has changed that I am unaware of.

Okay, some success but a step backward. I’ve always had my development system set up with me as user and a member of the www-data group but as a test I changed it to www-data:www-data and the upload worked. However, now I can no longer save any programming as I don’t have permission on my own PC! How do I get both working again?

Your umask (chmod) is set in a wax that only the owner is allowed to write to that location.

If you need to write with different users, this can be resolved through the group privileges.

In the order from left to right, the umask is for
owner
group
everyone

whereas the bits in the mask are
1 can execute
2 can write
4 can read

any combination of those can be achieved by just adding.

so if you need read and write, that’s 2+4 = 6

Given that your users (the www-data one running the apache as well as your personal one) are both in the www-data group, setting your masks to
0775 instead of 0755
0664 instead of 0644
(so including 2 / write) for the second value (group) should fix your problem

If your users aren’t members of the same group, you can also use the last digit for “everyone” but be aware that this is indeed every user on the machine then. Thus I advise to NOT to do that on a production server but only on a local test environment and only if everything else fails.

I’m the only user so no worries there (this is my development system) and I am a member of www-data. I had already tried 0775 and 0664 which does allow the upload and still lets me edit the scripts so I can get by with that for now but the problem is, all uploads are locked so I can’t update or delete them. Although I have a static IP for it, my production server is hosted.

You can use php to “fix” that on your box.
Check out the chmod() function which also exists in php.

There may be other helpful functions for file operations as well.

If you describe your “locked” Situation on your production server as well as the desired situation, we can guide you through this.

Cheers

Thank you. What’s odd about the locked files and folders (they show a lock on them) is that I can often delete them through File Manager but sometimes I cannot. The folders were created using mkdir() and I’ve used chmod() in the past but trying to avoid it when not absolutely necessary.

In any event, the basics are working fairly well so if it comes to it, I’ll make a separate post about any actual PHP issues which I am expected to have but will try to work it through first myself. I used to be a wiz at programming but long retired and quite rusty now!

Sponsor our Newsletter | Privacy Policy | Terms of Service