View Full Version : adding permissions
bluewalrus
12-21-2008, 08:33 PM
How can I set the permissions of images that are being uploaded from the site itself? I tried chmod last week but using that I did not have permissions on it from the ftp which I still want to be able to do. I think I had chmod ( name , 0777). I want the images to be 777 right now they are uploading as 600 and aren't displayable (403 forbidden) without going threw the ftp and changing it
This is the php I'm using.
//increase the number of files uploaded
$counterfile = "number.txt";
$fp = fopen($counterfile,"r");
$hits = fgets($fp,100);
fclose($fp);
$hits++;
$myFile = "uploads.txt";
$fp = fopen($counterfile,"w");
fputs($fp, $hits);
fclose($fp);
$success = 0;
$fail = 0;
$uploaddir = 'uploads/';
for ($i=0;$i<3;$i++)
{
if($_FILES['userfile']['name'][$i])
{
$uploadfile = $uploaddir . basename($_FILES['userfile']['name'][$i]);
$ext = strtolower(substr($uploadfile,strlen($uploadfile)-3,3));
if (preg_match("/(jpg|gif|png|bmp)/",$ext))
{
if (move_uploaded_file($_FILES['userfile']['tmp_name'][$i], $uploadfile))
{
$success++;
}
else
{
echo "Error Uploading the file. Retry after sometime.\n";
$fail++;
}
}
else
{
$fail++;
}
}
}
echo "<br> Number of files Uploaded:".$success;
echo "<br> Number of files Failed:".$fail;
}
Can you send us the code you tried when you tried using chmod?
bluewalrus
12-21-2008, 09:07 PM
I didnt try it with this code and I deleted it from the last because I didnt know why it was doing what it was doing last time. I thought chmod was something that would do the same thing everytime. This is what my service provider told me last time ...
Our PHP works as apache module. And httpd is apache user. So all files and folders that were created or changed by apache would have httpd as owner. To restore your default owner you should enter your control panel->FTP service->Enable FTP for one of your domain (if you have all turned off)->click edit button and add virtual ftp directory where you have wrong owner for your files. For example yourdomain.com/ After this you will be able to change or delete this files.
If you could tell me how to use the chmod to avoid this I'll try it again. I wasnt sure if it was the chmod or the mkdir that did it but I was using both and then this happened.
hmsnacker123
12-25-2008, 01:55 AM
have you tried to chown function (http://www.php.net/chown)?
bluewalrus
12-25-2008, 02:17 AM
No, what is $user_name? I don't understand the root term. I've had to switch the permission each time in the ftp to 777 from 600. Would 700 go in place of root???
bluewalrus
12-26-2008, 04:21 AM
I've gone back to the chmod since 9/10 say that but its not working this way still. I still do not get the chown If anyone could explain that or thinks it would work better? Thanks. This does not create any errors and puts the file up but does not change the permissions still (600 should be 777).
//increase the number of files uploaded
$success = 0;
$fail = 0;
$uploaddir = 'uploads/';
for ($i=0;$i<3;$i++)
{
if($_FILES['userfile']['name'][$i])
{
$uploadfile = $uploaddir . $name . $age . $denim . $lifestyle . basename($_FILES['userfile']['name'][$i]);
chmod($uploadfile , 0777); // THIS LINE IS THE NEW ONE
$ext = strtolower(substr($uploadfile,strlen($uploadfile)-3,3));
if (preg_match("/(jpg|gif|png|bmp)/",$ext))
{
if (move_uploaded_file($_FILES['userfile']['tmp_name'][$i], $uploadfile))
{
$success++;
}
else
{
echo "Error Uploading the file. Retry after sometime.\n";
$fail++;
}
}
else
{
$fail++;
}
}
}
echo '<div style="margin-top:210px" text-align:center;>';
echo "<br> Number of files Uploaded:".$success;
echo "<br> Number of files Failed:".$fail;
echo '</div>';
}
EDIT
I also found this, here (http://www.phpbuilder.com/board/showthread.php?t=10314483) which sounded like my problem by still doesn't work. The new if statement here just loads the page blank.
//increase the number of files uploaded
$success = 0;
$fail = 0;
$uploaddir = 'uploads/';
for ($i=0;$i<3;$i++)
{
if($_FILES['userfile']['name'][$i])
{
$uploadfile = $uploaddir . $name . $age . $denim . $lifestyle . basename($_FILES['userfile']['name'][$i]);
$perms = substr(sprintf('%o', fileperms($uploadfile)), -4); //THE WHOLE NEW STATEMENT STARTS HERE
if($perms !== '0777')
{
if(chmod($uploadfile, 0777)===TRUE)
{
echo 'Changed permissions of file successfully.<br>';
}
else
{
echo 'Failed to change permissions of file.<br>';
} //ENDS HERE
// chmod($uploadfile , 0777); // THIS IS THE LINE FROM ABOVE THAT ALSO DIDNT WORK BUT APPEARED CLOSER ( NO BLANK PAGE)
$ext = strtolower(substr($uploadfile,strlen($uploadfile)-3,3));
if (preg_match("/(jpg|gif|png|bmp)/",$ext))
{
if (move_uploaded_file($_FILES['userfile']['tmp_name'][$i], $uploadfile))
{
$success++;
}
else
{
echo "Error Uploading the file. Retry after sometime.\n";
$fail++;
}
}
else
{
$fail++;
}
}
}
echo '<div style="margin-top:210px" text-align:center;>';
echo "<br /> Number of files Uploaded:".$success;
echo "<br /> Number of files Failed:".$fail;
echo '</div>';
}
bluewalrus
12-31-2008, 05:42 AM
Does anyone know where I can find other functions for swapping the permissions or any tutorials that relate to it? I've been here and tried this http://us3.php.net/chmod as well as the link above but still with no luck and the chown.
bluewalrus
01-02-2009, 09:29 PM
Solution (put on the gallery display page not on the upload page itself)...(it won't let me change to resolved)
<?php
$dir = '/uploads/';
$d = dir($dir);
while(FALSE !== ($entry = $d->read()))
{
if($entry != '.' && $entry != '..')
{
// get the file permissions
$perms = substr(sprintf('%o', fileperms($dir.$entry)), -4);
if($perms !== '0755')
{
if(chmod($dir.$entry, 0755)===TRUE)
{
//WORKED DID CHANGE
}
else
{
//ERROR DIDN'T CHANGE
}
}
}
}
?>
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.