Log in

View Full Version : Setting file permissions



captainjustin
12-11-2010, 12:26 AM
I'm using a script that re sizes and uploads pictures to a directory in my website. The script seems to be working fine, but when I try to view the uploaded pictures I'm getting a 403 forbidden error. I contacted my hosting provider and they told me that the permissions need to be changed in the script using chmod.... I did a little research and found a way to do this using php but I'm not quite sure how to combine the scripts together.. I was wondering if some one could help me out here.


<?
set_time_limit ( 240 ) ;

$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['Filedata']['name']);
$orig=$target_path;
$info = pathinfo($target_path);

$ctr=1;
while(file_exists($target_path)){
$target_path=$info['dirname']."/".$info['filename'].$ctr.".".$info['extension'];
$ctr++;

}

if(move_uploaded_file($_FILES['Filedata']['tmp_name'], $target_path)) {
echo "status=1&filepath=$target_path&size=".$_FILES['Filedata']['size'];


} else{
echo "status=0&msg=Error";
}



?>


Here is the chmod setting I think I need?



<?
chmod("upoads/somefile", 0755);


////where do I put this and what should go where (somefile) is?
?>


Thanks in advance

Schmoopy
12-11-2010, 12:49 AM
You wanna do that right after you've uploaded the file, so like this:



if(move_uploaded_file($_FILES['Filedata']['tmp_name'], $target_path)) {
echo "status=1&filepath=$target_path&size=".$_FILES['Filedata']['size'];

if(!chmod($target_path, 0755))
echo "status=0&msg=Error";

} else{
echo "status=0&msg=Error";
}

captainjustin
12-11-2010, 01:56 AM
Thank you so much!!!!! That worked like a charm. You have made my day!

djr33
12-11-2010, 02:59 AM
My recommendation is always adding a chmod command after creating any new file automatically. This solves problems (avoids them actually) that you might find later. For example, it might be visible by HTTP but not to your FTP user-- this has happened to me a few times.
Using the same basic approach, just always add this line:
chmod($file,0755);
That way nothing unpredictable happens.