Matpatnik Posted January 23, 2007 Share Posted January 23, 2007 Hi guys,I have this issue now: With my testing server it was fine but it's an other thing with the website server. When a file is uploaded into the images/ folder it write the 600 permission automatically. What is the best way to set the permission to 644 within this code? // upload image and check for image type if (move_uploaded_file($_FILES['image_filename']['tmp_name'], $ImageName)) { // get info about the image being uploaded list($width, $height, $type, $attr) = getimagesize($ImageName); if ($type > 3) { echo "Sorry, but the file you uploaded was not a GIF, JPG, or PNG file.<br>"; echo "Please hit your browser's 'back' button and try again."; } else { // image is acceptable; ok proceed // insert info into image table $insert = "INSERT INTO images (images_caption, images_username, images_date) VALUES ('$image_caption', '$image_username', '$today')"; $insertresult = mysql_query($insert) or die(mysql_error()); // get the last mysql insert id $lastpicid = mysql_insert_id(); $newfilename = $ImageDir . $lastpicid . ".jpg"; if ($type == 2) { rename($ImageName, $newfilename); } else { if ($type == 1) { $image_old = imagecreatefromgif($ImageName); } elseif ($type == 3) { $image_old = imagecreatefrompng($ImageName); } // "convert" the image to .jpg $image_jpg = imagecreatetruecolor($width, $height); imagecopyresampled($image_jpg, $image_old, 0, 0, 0, 0, $width, $height, $width, $height); imagejpeg($image_jpg, $newfilename); chmod($newfilename, 0644); imagedestroy($image_old); imagedestroy($image_jpg); } Thank you for your helpmatpatnik Link to comment Share on other sites More sharing options...
justsomeguy Posted January 23, 2007 Share Posted January 23, 2007 The chmod function will change file or directory permissions. If you do not have access to use the chmod function, then you will need to connect to the server using FTP and then use the FTP chmod command. Link to comment Share on other sites More sharing options...
Matpatnik Posted January 24, 2007 Author Share Posted January 24, 2007 I've insert the chmod($newfilename, 0644); in between the imagejpeg($image_jpg, $newfilename); and imagedestroy($image_old); and it doesn't work. Do I have to call the function before?edited: Do you think, because I rename the file it change the permission because my thumbnails are auto generated lower in this code and there permission are fine they are set to 644 like they are suppose to be. I'm trying to understand why it does this. Link to comment Share on other sites More sharing options...
vijay Posted January 24, 2007 Share Posted January 24, 2007 Hi.. First see the folder's owner or file's owner which you have create is same as other.. and if yes then that owner has given permission to change the file mode.. If yes then and then we can go for chmod().. otherwise we have to hosting server people to give permission that owner to write and execute..Reagards,Vijay Link to comment Share on other sites More sharing options...
Matpatnik Posted January 24, 2007 Author Share Posted January 24, 2007 Hi.. First see the folder's owner or file's owner which you have create is same as other.. and if yes then that owner has given permission to change the file mode.. If yes then and then we can go for chmod().. otherwise we have to hosting server people to give permission that owner to write and execute..Reagards,VijayI'm not sure to understand about the folder permission. I rent the server (well I payed for few years), the folder test/images/ permission is set to 777, the test is set to 775 and the thumbs is set to 755. Is it what you've ask? Link to comment Share on other sites More sharing options...
justsomeguy Posted January 24, 2007 Share Posted January 24, 2007 Check if safe mode is on: if (ini_get("safe_mode")) echo "Safe mode is enabled"; Link to comment Share on other sites More sharing options...
Matpatnik Posted January 24, 2007 Author Share Posted January 24, 2007 yes safe mode is on, it's on by default. Do I need to turn it off because this will make my site more vulnerable isn't it? Link to comment Share on other sites More sharing options...
justsomeguy Posted January 24, 2007 Share Posted January 24, 2007 You need to turn it off if you want the chmod function to work. Also, when safe mode is on, any folder or file that PHP creates gets set with a different owner then the PHP process. So, you can create a folder, but then not be able to change anything about it because you don't own it.Safe mode is only necessary for a shared host environment, when you wouldn't want one guy's PHP process to be able to mess around with the server or someone else's files. If you are running your own server then you are only protecting against yourself (which also may not be a bad idea).So you can either disable safemode and be able to use chmod and chown, or you can use the FTP functions. Even when safe mode is on, you can use FTP to create a folder or upload a file. When you create a folder through FTP the owner is set to the same user as the PHP process runs under, so PHP can change the folder.Here's an explanation from php.net: Beware that when in safe mode, mkdir creates folders with apache's UID, though the files you create in them are of your script's UID (usually the same as your FTP uid).What this means is that if you create a folder, you won't be able to remove it, nor to remove any of the files you've created in it (because those operations require the UID to be the same).Ideally mkdir should create the folder with the script's UID, but this has already been discussed and will not be fixed in the near future. In the meantime, I would advice NOT to user mkdir in safe mode, as you may end up with folders you can't remove/use.Here is a function that uses the FTP functions to do mkdir and chmod:<?php// create directory through FTP connection function FtpMkdir($path, $newDir) { $server='ftp.yourserver.com'; // ftp server $connection = ftp_connect($server); // connection // login to ftp server $user = "me"; $pass = "password"; $result = ftp_login($connection, $user, $pass); // check if connection was made if ((!$connection) || (!$result)) { return false; exit(); } else { if (!ftp_chdir($connection, $path)) { // go to destination dir $r = false; } else if (!ftp_mkdir($connection,$newDir)) { // create directory $r = false; } else if (!ftp_site($connection, "CHMOD 0777 $newDir") { // change attributes $r = false; } else { $r = $newDir; } } ftp_close($connection); // close connection return $r;}?> Link to comment Share on other sites More sharing options...
Matpatnik Posted January 24, 2007 Author Share Posted January 24, 2007 I'm gonna turn off the safe mode and see if the chmod() work* by the way did I insert the chmod() at the right place?I wont need your code right now but I will surely use it later on in my project Thank you Link to comment Share on other sites More sharing options...
justsomeguy Posted January 24, 2007 Share Posted January 24, 2007 You would use chmod any time after you have created the file. Since the imagejpeg function creates the file, any time after that you can use chmod. Link to comment Share on other sites More sharing options...
Matpatnik Posted January 24, 2007 Author Share Posted January 24, 2007 ok good thxedited: I moved the chmod() after the creation of the picture like you said and now it work perfectly imagejpeg($image_jpg, $newfilename); imagedestroy($image_old); imagedestroy($image_jpg); } chmod($newfilename, 0644); Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now