PHP Upload Problem

Joined
Mar 31, 2010
Messages
6
Reaction score
0
Points
1
I am fairly familiar with PHP and have just recently got Apache and MySQL to work on Snow Leopard. My idea is to create a sort of "digital file cabinet" to manage PDFs for my home use. However, I have ran into a snag...

In researching how to upload files in PHP, I came across the following w3 website: PHP File Upload

After copying their exact code (minus the restriction part), my code is the following:

Code:
<?php	
  //upload_file.php
	
  if($_FILES["file"]["error"] > 0) 
  {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
  }
  else
  {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp File: " . $_FILES["file"]["tmp_name"] . "<br />";
		
    move_uploaded_file($_FILES["file"]["tmp_name"],
    "upload/" . $_FILES["file"]["name"]);
    echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
  }	
?>

After I upload a file, my browser returns the following:

Upload: test.png
Type: image/png
Size: 92.7666015625 Kb
Temp File: /private/var/tmp/php6ZY6Ad
Warning: move_uploaded_file(upload/test.png): failed to open stream: Permission denied in /Users/jg/Sites/development/testing/upload_file.php on line 14 Warning: move_uploaded_file(): Unable to move '/private/var/tmp/php6ZY6Ad' to 'upload/test.png' in /Users/jg/Sites/development/testing/upload_file.php on line 14 Stored in: upload/test.png

I've hunted around on the web...but no one seems to have a the same problem...
 

Raz0rEdge

Well-known member
Staff member
Moderator
Joined
Jul 17, 2009
Messages
15,822
Reaction score
2,177
Points
113
Location
MA
Your Mac's Specs
2022 Mac Studio M1 Max, 2023 M2 MBA
What are the permissions of the 'upload' directory? You'll need to give the web server access to it before it can move files into it..

Regards
 
OP
D
Joined
Mar 31, 2010
Messages
6
Reaction score
0
Points
1
Duh! I went to "Get Info" on the upload folder and Everyone was set to "Read only". Once I changed Everyone to "Read & Write", it worked! Thanks!

Now this shouldn't pose any kind of security issue since I'm just using this within my own network should it?
 

Raz0rEdge

Well-known member
Staff member
Moderator
Joined
Jul 17, 2009
Messages
15,822
Reaction score
2,177
Points
113
Location
MA
Your Mac's Specs
2022 Mac Studio M1 Max, 2023 M2 MBA
That's correct..you shouldn't have any security issues with this since you are testing everything locally..

If you are paranoid..you can always figure out under what user the web server runs as and then explicitly give that user read&write access..

Regards
 
OP
D
Joined
Mar 31, 2010
Messages
6
Reaction score
0
Points
1
Well...I'm not exactly paranoid. How would I go about finding out what "user" the web server runs under? Would it be root? I know you have to use sudo to start/restart Apache.
 
OP
D
Joined
Mar 31, 2010
Messages
6
Reaction score
0
Points
1
Well I think I know the "user" now, but I just don't know how to add it. I looked at the permissions of the files that I uploaded using my script and user "_www" has "Read & Write" access and user "wheel" has "Read Only". So I'm thinking that is what I need to grant permissions to my "upload" folder. But when I click the (+) to add a user to the permissions list "_www" isn't there. Maybe a Terminal command?
 
OP
D
Joined
Mar 31, 2010
Messages
6
Reaction score
0
Points
1
Alright, I have found my own answer but I will share just in case someone else would like to know how I did it.

_www is the user and group that Apache runs under in Mac OS. I found this discussion which talks about how to grant permissions to that user.

It suggests two ways but the easiest is to use a free program called BatChmod.app (it can be found here).

BatChmod allows you to select a directory or file and grant permissions to a whole slew of hidden users within the MacOS System and _www is one of them.
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top