Posts tagged ‘apache’

Apache looking for .htaccess in the wrong places – Fixed!

Is Apache looking for a .htaccess file in all the wrong places? Maybe you have an area where one doesn’t even exist, nor should it. Do you get a message such as:

Permission denied: /srv/http/domain/files/images/.htaccess pcfg_openfile: unable to check htaccess file, ensure it is readable

If you are, you may be wondering why apache is looking for a .htaccess file in the images sub-folder. You may even be wondering why it is asking for one at all because you don’t even use .htaccess file anywhere. Turns out there is a very simple fix.

The problem has to do with file and directory permissions on the server. First off we need to check permissions, here is a fake directory tree for an example.

-rw-r--r--  1 root root    72K 2006-09-30 17:06 file1.htm
drw-r--r--  2 root root   4.0K 2007-05-09 11:52 images/
-rw-r--r--  1 root root    72K 2006-09-30 17:06 index.htm
-rw-r-----  1 root root    625 2010-10-17 14:25 news.php
-rw-------  1 root root    598 2010-10-17 14:25 stuff.php
-rw-r--r--  1 root root     56 2008-03-23 13:42 stuff2.php

See how everything is owned by root? Sometimes this is OK, but for some users it is not possible. Find out from your service provider if you need to use another name, such as apache, or your login user name. For me, I like having everything owned by root. That way other programs or users on my server can’t edit my files.

Look at the images/ directory. It’s permissions are wrong. It turns out Apache wants the directory to be executable. Currently the directory has read-write on user, read on group, and read on world level permissions. We need to make is rwx-r-xr-x, or read-write-execute on user, read-execute on group, and read-execute on world levels.

chmod 755 images/

That should do it. Did you see any other problems with the directory tree above? I actually put 2 more mistakes. They are news.php and stuff.php. Chmod them both to 644. Then they will have the same permissions as index.htm.

If you are like me and want to set the following permissions for everything. I have an easy way of doing it.

-rw-r--r-- for files
drwxr-xr-x for directories
chmod 644 * -R
find . -type d -exec chmod 755 \{\} \;

When complete you will get a list like this.

-rw-r--r--  1 root root    72K 2006-09-30 17:06 file1.htm
drwxr-xr-x  2 root root   4.0K 2007-05-09 11:52 images/
-rw-r--r--  1 root root    72K 2006-09-30 17:06 index.htm
-rw-r--r--  1 root root    625 2010-10-17 14:25 news.php
-rw-r--r--  1 root root    598 2010-10-17 14:25 stuff.php
-rw-r--r--  1 root root     56 2008-03-23 13:42 stuff2.php

That will do it. Keep in mind that depending on your configuration this may not work, but I hope it does. If this did work for you please drop a comment to let others know. Thank you, and good luck!

Using PHP to upload an image and rename it.

If your like me you have scoured the internet trying to find a simple php script that will let a user upload an image and then rename the image to something unique. Well I have just the solution.

First, the only real requirement is PHP and a web server (I use Apache).

Create a new file, lets call it ‘image_upload.php’
At the bottom of the page we are going to put in a basic form to give a browse and upload button.

<form action="./image_upload.php" enctype="multipart/form-data" method="post">
Select Image to upload
<input name="thefile" type="file" />
<input name="submit" type="submit" value="upload" />
</form>

The form name isn’t important. The action is. That is the name of the file that needs to be accessed when the upload button is clicked.
Input type is very important, and so is the name.

Now that we have a starting point go to the top of the file. Just to make it easy I’m going to paste the entire code here then explain it part by part.

if(isset($_POST['submit']))
{
 
	$tmp_name = $_FILES['thefile']['tmp_name'];
	$newarray = explode( ".", $_FILES['thefile']['name']);
	$thecount = count($newarray);
	$fileprefix = time() . "." . $newarray[$thecount - 1];
	move_uploaded_file($tmp_name, "../img/$fileprefix");
	exit();
}

To start the if statement at the top is to ensure that the ‘submit’ button was hit before running the script.
The ‘$tmp_name‘ variable is used to get rid of that damn array.
$newarray takes the file’s real name (see note below) and explodes the array into several smaller arrays by the ‘.’. For example, if the file uploaded it called ‘best.friends.pic.jpg’ it breaks that name up to ‘best’, ‘friends’, ‘pic’, and ‘jpg’. Trust me, this is important (If you don’t understand how arrays work I recommend doing some research and learning).
The next line I use the count function. This will help in the event there is more than one ‘.’ in the name of the file being uploaded.
Now to create the new name of the file. $fileprefix equals the time of the upload. This works well, it will grab the time from the server and use it for the file name. Then it adds a ‘.’, then adds the suffix of the file (example: jpg)
Time to move the uploaded file. In my example I added the directory before the newly created file name. This will go back 1 directory and then to ‘img/‘.
Lastly, we use the ‘exit();‘ function. This is so that after the file has been uploaded the script stops. If you remove the function it will display the upload form again.

NOTE: There is a reason (although I don’t know it) on why there are 2 arrays. It seems to me that the first ($_FILES[‘thefile’][‘tmp_name’]) holds 2 parts, a pointer for ‘thefile‘ and the temp name php gives files before we do something with it, and the second ($_FILES[‘thefile’][‘name’]) contains the original name of the file.

btw, make sure apache has full access to the folder you are uploading images to.
For example, if this is on your own server check /etc/apache.conf (or httpd.conf) for the user and group apache runs under. This is usually either ‘nobody‘ or ‘apache‘.
If you have SSH into your server (or are sitting infront of it) goto the directory your files are being sent to and type

chown apache.apache img/ -R

This will give both user and group ‘apache‘ access to write files.

p.s. I will be creating another script here that deals with multiple picture uploads.