Warmics Posted April 16, 2012 Share Posted April 16, 2012 This is probably a trivial task to most of you, but I can't for the life of me figure out how to do this. I want to write a script that will open an image directory, go through each of the images inside and change their permissions, owner and group. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/261017-file-ownership-loop/ Share on other sites More sharing options...
requinix Posted April 16, 2012 Share Posted April 16, 2012 A shell script (or a single command, even) would be easier. If you can't do that then is your PHP script running as root? Quote Link to comment https://forums.phpfreaks.com/topic/261017-file-ownership-loop/#findComment-1337733 Share on other sites More sharing options...
Warmics Posted April 17, 2012 Author Share Posted April 17, 2012 Thanks for the reply, I wrote a PHP script to upload the images. These images are getting uploaded as user: apache, group: apache. I therefore can't edit them nor download them via my FTP client. I was thinking the easiest thing to do was run a PHP script that will modify these files as PHP scripts DO have access to them. They are a couple hundred though, so doing this one by one would be extremely inefficient. I'm not familiar with this thing about the shell, I'll do some research about it. I've been teaching myself this stuff, so I have a lot of knowledge gaps here and there. I appreciate the time. Quote Link to comment https://forums.phpfreaks.com/topic/261017-file-ownership-loop/#findComment-1337986 Share on other sites More sharing options...
requinix Posted April 17, 2012 Share Posted April 17, 2012 Only root can change the ownership of a file. You shouldn't need to change file permissions. Why do you want to change the ownership? Quote Link to comment https://forums.phpfreaks.com/topic/261017-file-ownership-loop/#findComment-1337993 Share on other sites More sharing options...
Warmics Posted April 17, 2012 Author Share Posted April 17, 2012 i thought I needed to change ownership in order to download the images to my computer. You see, the images are being uploaded from different computers via a very amateur CMS. I noticed the person uploading the images is not making them small, so they are uploading 2 MB images (for thumbnails and stuff). I wanted to download all the images to my personal pc and shrink'em. But I can't because my FTP Client tells me I have no access to them. I know how to change one by one using a simple PHP script, I was just hoping there was a way to use a loop for this, but I'm not sure how to use PHP to do so. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/261017-file-ownership-loop/#findComment-1338220 Share on other sites More sharing options...
xyph Posted April 17, 2012 Share Posted April 17, 2012 No, you need to give the read permission to the user running your web server. You probably need to give the directory/folder execute permission to the user running the web server as well, in order to iterate through the directory. I could be wrong about this though, I don't have a nix box handy to check at the moment. Quote Link to comment https://forums.phpfreaks.com/topic/261017-file-ownership-loop/#findComment-1338231 Share on other sites More sharing options...
requinix Posted April 17, 2012 Share Posted April 17, 2012 Ownership controls what you can do to something while permissions control what you can do with it. When the PHP script uploads the file, make it chmod($filename, 0644); the file. That should take care of not being able to download future files; for the existing files you can write a very quick script that does that chmod() on everything. It would look like foreach (glob("/path/to/uploads/*.*") as $file) chmod($file, 0644); // and that's all there is to it, really Otherwise if you have a PHP script that can shrink one file, modify it so that it can shrink a series of files. Or easier, make a new script which sets up a couple variables as needed and then include()s the shrinking script. But ultimately this problem should be solved in the CMS: when images are uploaded they should be resized if above a certain size (be that pixel dimensions or file size or whatever). You probably need to give the directory/folder execute permission to the user running the web server as well, in order to iterate through the directory. I could be wrong about this though, I don't have a nix box handy to check at the moment. Yes, that's required. Odds are this is one of two situations: either the files are world-not-readable (like 0600 or 0640) or the directory has world-not-executable (like 0700 or 0750). Or both. What's unusual is that this is quite rare to have set up by default. Normally files are naturally 0644 and directories 0755 and then something manually changes those permissions to be more restrictive. Quote Link to comment https://forums.phpfreaks.com/topic/261017-file-ownership-loop/#findComment-1338234 Share on other sites More sharing options...
Warmics Posted April 17, 2012 Author Share Posted April 17, 2012 ok, this solutions do make sense in the context of the scripts i'm using, I'll try them out and get back to you, thanks a lot guys, really appreciated Quote Link to comment https://forums.phpfreaks.com/topic/261017-file-ownership-loop/#findComment-1338258 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.