Datnigz2002 Posted April 30, 2008 Share Posted April 30, 2008 I am using this chat program and it is doing something wrong when a user uploads their picture. I have taken the upload script that the program uses and placed it hear. It works fine but I need this upload action to CHMOD the file to 755. I have no idea why it is defaulting to 600. Any help? <?PHP // For now this simply contains some duplicates of functions found in // xupdater.php and uploads.php, my hope is that in the future it will // be consolidated into this file only to help clean out the other two // Upload a new file function upload_file($incoming,$filename=""){ global $x7c; // Moves uploaded file into the uploads directory move_uploaded_file($_FILES[$incoming]['tmp_name'],"{$x7c->settings['uploads_path']}/$filename"); } // Unzip a file / directory function unzip_file($file_name){ if(!mkdir("./uploads/temp")) return 0; $shell = exec("unzip ./uploads/$file_name -d ./uploads/temp"); if(!eregi("inflating",$shell)){ // Unzipping the file failed return 0; }else{ // Ok to continue return 1; } } // Remove a directory function remove_file($dirx){ // This code seems pretty damn ineffective to me, maybe there is a better way.. $dir = dir($dirx); while($file = $dir->read()){ if($file != "." && $file != ".."){ if(is_dir("$dirx/$file")) remove_file("$dirx/$file"); else unlink("$dirx/$file"); } } rmdir($dirx); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/103613-upload-file-then-chmod-it/ Share on other sites More sharing options...
DarkWater Posted April 30, 2008 Share Posted April 30, 2008 function upload_file($incoming,$filename=""){ global $x7c; // Moves uploaded file into the uploads directory move_uploaded_file($_FILES[$incoming]['tmp_name'],"{$x7c->settings['uploads_path']}/$filename"); chmod($x7c->settings['uploads_path']}/$filename, "0755") OR die("CHMOD of $filename failed!"); } You can try that. The PHP user might not have permission to do it though, so be careful. Quote Link to comment https://forums.phpfreaks.com/topic/103613-upload-file-then-chmod-it/#findComment-530583 Share on other sites More sharing options...
Datnigz2002 Posted April 30, 2008 Author Share Posted April 30, 2008 Thanks! but actually I may have given you either the wrong upload code or another that belonged to another part of the program. I'm sure this is the one. How should this one be changed?? <?PHP // This file handles uploads of avatars // * Returns 1 if avatar uploads are disabled // * Returns 2 if avatar is to large // * Returns 3 if the avatar is not an image type // * Returns 4 if the avatar upload directory is not writeable // * Returns URL to avatar is everythign went ok function handle_uploaded_avatar(){ global $x7c, $x7s; // See if avatar uploads are enabled if($x7c->settings['enable_avatar_uploads'] == 1){ // Make sure the directory is writeable if(!is_writable($x7c->settings['uploads_path'])) return 4; // See if the file is within the correct size limitations if($_FILES['avatar']['size'] > $x7c->settings['avatar_max_size']){ // To large return 2 return 2; }else{ // See if the file is a correct image type, either gif, png or jpg if($_FILES['avatar']['type'] == "image/gif"){ remove_other_avatars(); move_uploaded_file($_FILES['avatar']['tmp_name'],"{$x7c->settings['uploads_path']}/avatar_{$x7s->username}.gif"); return "{$x7c->settings['uploads_url']}/avatar_{$x7s->username}.gif"; }elseif($_FILES['avatar']['type'] == "image/png"){ remove_other_avatars(); move_uploaded_file($_FILES['avatar']['tmp_name'],"{$x7c->settings['uploads_path']}/avatar_{$x7s->username}.png"); return "{$x7c->settings['uploads_url']}/avatar_{$x7s->username}.png"; }elseif($_FILES['avatar']['type'] == "image/jpeg" || $_FILES['avatar']['type'] == "image/pjpeg"){ remove_other_avatars(); move_uploaded_file($_FILES['avatar']['tmp_name'],"{$x7c->settings['uploads_path']}/avatar_{$x7s->username}.jpeg"); return "{$x7c->settings['uploads_url']}/avatar_{$x7s->username}.jpeg"; }else{ // Incorrect type, return 3 return 3; } } }else{ // They are not so return 1 return 1; } } // This function removes a users other avatars function remove_other_avatars(){ global $x7s, $x7c; // Gif ones if(file_exists("{$x7c->settings['uploads_path']}/avatar_{$x7s->username}.gif")) unlink("{$x7c->settings['uploads_path']}/avatar_{$x7s->username}.gif"); // PNG ones if(file_exists("{$x7c->settings['uploads_path']}/avatar_{$x7s->username}.png")) unlink("{$x7c->settings['uploads_path']}/avatar_{$x7s->username}.png"); // JPG ones if(file_exists("{$x7c->settings['uploads_path']}/avatar_{$x7s->username}.jpeg")) unlink("{$x7c->settings['uploads_path']}/avatar_{$x7s->username}.jpeg"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/103613-upload-file-then-chmod-it/#findComment-530592 Share on other sites More sharing options...
DarkWater Posted April 30, 2008 Share Posted April 30, 2008 <?PHP // This file handles uploads of avatars // * Returns 1 if avatar uploads are disabled // * Returns 2 if avatar is to large // * Returns 3 if the avatar is not an image type // * Returns 4 if the avatar upload directory is not writeable // * Returns URL to avatar is everythign went ok function handle_uploaded_avatar(){ global $x7c, $x7s; // See if avatar uploads are enabled if($x7c->settings['enable_avatar_uploads'] == 1){ // Make sure the directory is writeable if(!is_writable($x7c->settings['uploads_path'])) return 4; // See if the file is within the correct size limitations if($_FILES['avatar']['size'] > $x7c->settings['avatar_max_size']){ // To large return 2 return 2; }else{ // See if the file is a correct image type, either gif, png or jpg if($_FILES['avatar']['type'] == "image/gif"){ remove_other_avatars(); move_uploaded_file($_FILES['avatar']['tmp_name'],"{$x7c->settings['uploads_path']}/avatar_{$x7s->username}.gif"); chmod({$x7c->settings['uploads_path']}/avatar_{$x7s->username}.gif, "0755") OR die("Chmod error"); return "{$x7c->settings['uploads_url']}/avatar_{$x7s->username}.gif"; }elseif($_FILES['avatar']['type'] == "image/png"){ remove_other_avatars(); move_uploaded_file($_FILES['avatar']['tmp_name'],"{$x7c->settings['uploads_path']}/avatar_{$x7s->username}.png"); chmod({$x7c->settings['uploads_path']}/avatar_{$x7s->username}.png, "0755") OR die("Chmod error"); return "{$x7c->settings['uploads_url']}/avatar_{$x7s->username}.png"; }elseif($_FILES['avatar']['type'] == "image/jpeg" || $_FILES['avatar']['type'] == "image/pjpeg"){ remove_other_avatars(); move_uploaded_file($_FILES['avatar']['tmp_name'],"{$x7c->settings['uploads_path']}/avatar_{$x7s->username}.jpeg"); chmod({$x7c->settings['uploads_path']}/avatar_{$x7s->username}.jpeg, "0755") OR die("Chmod error"); return "{$x7c->settings['uploads_url']}/avatar_{$x7s->username}.jpeg"; }else{ // Incorrect type, return 3 return 3; } } }else{ // They are not so return 1 return 1; } }?> Should work if you have the right permissions. Quote Link to comment https://forums.phpfreaks.com/topic/103613-upload-file-then-chmod-it/#findComment-530598 Share on other sites More sharing options...
Datnigz2002 Posted April 30, 2008 Author Share Posted April 30, 2008 Well using that it doesn't upload at all. I only get a white page after I click the upload button. However, I am not getting any PHP errors back from it. Quote Link to comment https://forums.phpfreaks.com/topic/103613-upload-file-then-chmod-it/#findComment-530603 Share on other sites More sharing options...
DarkWater Posted April 30, 2008 Share Posted April 30, 2008 Do this for me. Put an image in the directory that you know the name of and do this in a new php file: <?php (chmod("/path/to/image/", "0755")) ? echo "Chmod works" : echo "Chmod failed. Check permissions."; ?> Just do that to make sure you can use chmod on the files in PHP. Quote Link to comment https://forums.phpfreaks.com/topic/103613-upload-file-then-chmod-it/#findComment-530623 Share on other sites More sharing options...
Datnigz2002 Posted May 1, 2008 Author Share Posted May 1, 2008 I created the PHP file and made sure the full path to the image was correct. But I am only getting a white page when I launch the PHP file in the broswer. Quote Link to comment https://forums.phpfreaks.com/topic/103613-upload-file-then-chmod-it/#findComment-531084 Share on other sites More sharing options...
Datnigz2002 Posted May 1, 2008 Author Share Posted May 1, 2008 I even ran it on my Windows box and got this error. Parse error: syntax error, unexpected T_ECHO in c:\wamp\www\perm-test.php on line 2 Quote Link to comment https://forums.phpfreaks.com/topic/103613-upload-file-then-chmod-it/#findComment-531176 Share on other sites More sharing options...
Datnigz2002 Posted May 1, 2008 Author Share Posted May 1, 2008 I found this on the web. If the PHP user does not match the owner. (I'm not sure it does) Can a work around be found in the code below? If so how would I integrate it?? Also I may not even have to integrate it in if some way this piece a code will chmod -R a directory each time it is executed I can do it that way. <?php // Connect to the FTP to chmod the file via FTP $ftpUserName = 'username'; $ftpUserPass = 'userpass'; $ftpServer = 'ftp.example.com'; $ftpConn = ftp_connect($ftpServer); if (!$ftpConn) { die("Unable to connect to $ftpServer"); } if (@ftp_login($conn_id, $ftpUserName, $ftpUserPass)) { echo "Connected as $ftpUserName @ $ftpServer"; } else { echo "Couldn't connect as $ftpUserName"; ftp_close($ftpConn); die("Closed connection to $ftpServer"); } //Now change permissions to 666 or whatever you need echo ftp_chmod($ftpConn, 0666, $ftpFilename) ? "CHMOD successful!" : 'Error'; // do what you need here //Now change permissions back to 644 or whatever echo ftp_chmod($ftpConn, 0644, $ftpFilename) ? "CHMOD successful!" : 'Error'; // Close the connection ftp_close($conn_id); ?> Quote Link to comment https://forums.phpfreaks.com/topic/103613-upload-file-then-chmod-it/#findComment-531208 Share on other sites More sharing options...
Datnigz2002 Posted May 2, 2008 Author Share Posted May 2, 2008 I'm still stuck on this.. I was able to CHMOD using PHP so the thing about the PHP user is okay. However this code just shows a blank page and does not CHMOD the files. <?php // This file handles uploads of avatars // * Returns 1 if avatar uploads are disabled // * Returns 2 if avatar is to large // * Returns 3 if the avatar is not an image type // * Returns 4 if the avatar upload directory is not writeable // * Returns URL to avatar is everythign went ok function handle_uploaded_avatar(){ global $x7c, $x7s; // See if avatar uploads are enabled if($x7c->settings['enable_avatar_uploads'] == 1){ // Make sure the directory is writeable if(!is_writable($x7c->settings['uploads_path'])) return 4; // See if the file is within the correct size limitations if($_FILES['avatar']['size'] > $x7c->settings['avatar_max_size']){ // To large return 2 return 2; }else{ // See if the file is a correct image type, either gif, png or jpg if($_FILES['avatar']['type'] == "image/gif"){ remove_other_avatars(); move_uploaded_file($_FILES['avatar']['tmp_name'],"{$x7c->settings['uploads_path']}/avatar_{$x7s->username}.gif"); chmod({$x7c->settings['uploads_path']}/avatar_{$x7s->username}.gif, "0755") OR die("Chmod error"); return "{$x7c->settings['uploads_url']}/avatar_{$x7s->username}.gif"; }elseif($_FILES['avatar']['type'] == "image/png"){ remove_other_avatars(); move_uploaded_file($_FILES['avatar']['tmp_name'],"{$x7c->settings['uploads_path']}/avatar_{$x7s->username}.png"); chmod({$x7c->settings['uploads_path']}/avatar_{$x7s->username}.png, "0755") OR die("Chmod error"); return "{$x7c->settings['uploads_url']}/avatar_{$x7s->username}.png"; }elseif($_FILES['avatar']['type'] == "image/jpeg" || $_FILES['avatar']['type'] == "image/pjpeg"){ remove_other_avatars(); move_uploaded_file($_FILES['avatar']['tmp_name'],"{$x7c->settings['uploads_path']}/avatar_{$x7s->username}.jpeg"); chmod({$x7c->settings['uploads_path']}/avatar_{$x7s->username}.jpeg, "0755") OR die("Chmod error"); return "{$x7c->settings['uploads_url']}/avatar_{$x7s->username}.jpeg"; }else{ // Incorrect type, return 3 return 3; } } }else{ // They are not so return 1 return 1; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/103613-upload-file-then-chmod-it/#findComment-531825 Share on other sites More sharing options...
MadTechie Posted May 2, 2008 Share Posted May 2, 2008 chmod({$x7c->settings['uploads_path']}/avatar_{$x7s->username}.png, "0755") OR die("Chmod error"); is wrong try chmod("{$x7c->settings['uploads_path']}/avatar_{$x7s->username}.png", 0755) OR die("Chmod error"); note the quotes! updates others JPEG GIF etc Quote Link to comment https://forums.phpfreaks.com/topic/103613-upload-file-then-chmod-it/#findComment-531828 Share on other sites More sharing options...
MadTechie Posted May 2, 2008 Share Posted May 2, 2008 infact heres a tidy version replace <?php // See if the file is a correct image type, either gif, png or jpg if($_FILES['avatar']['type'] == "image/gif"){ remove_other_avatars(); move_uploaded_file($_FILES['avatar']['tmp_name'],"{$x7c->settings['uploads_path']}/avatar_{$x7s->username}.gif"); chmod({$x7c->settings['uploads_path']}/avatar_{$x7s->username}.gif, "0755") OR die("Chmod error"); return "{$x7c->settings['uploads_url']}/avatar_{$x7s->username}.gif"; }elseif($_FILES['avatar']['type'] == "image/png"){ remove_other_avatars(); move_uploaded_file($_FILES['avatar']['tmp_name'],"{$x7c->settings['uploads_path']}/avatar_{$x7s->username}.png"); chmod({$x7c->settings['uploads_path']}/avatar_{$x7s->username}.png, "0755") OR die("Chmod error"); return "{$x7c->settings['uploads_url']}/avatar_{$x7s->username}.png"; }elseif($_FILES['avatar']['type'] == "image/jpeg" || $_FILES['avatar']['type'] == "image/pjpeg"){ remove_other_avatars(); move_uploaded_file($_FILES['avatar']['tmp_name'],"{$x7c->settings['uploads_path']}/avatar_{$x7s->username}.jpeg"); chmod({$x7c->settings['uploads_path']}/avatar_{$x7s->username}.jpeg, "0755") OR die("Chmod error"); return "{$x7c->settings['uploads_url']}/avatar_{$x7s->username}.jpeg"; }else{ // Incorrect type, return 3 return 3; } ?> WITH <?php // See if the file is a correct image type, either gif, png or jpg switch($_FILES['avatar']['type']) { case "image/gif": $ext = ".gif"; break; case "image/png": $ext = ".png"; break; case "image/jpeg": $ext = ".jpeg"; break; default: return 3; break; } $filepath = "{$x7c->settings['uploads_path']}/avatar_{$x7s->username}".$ext; remove_other_avatars(); chmod($filepath, 0755) OR die("Chmod error"); return $filepath; ?> Quote Link to comment https://forums.phpfreaks.com/topic/103613-upload-file-then-chmod-it/#findComment-531832 Share on other sites More sharing options...
Datnigz2002 Posted May 2, 2008 Author Share Posted May 2, 2008 I'm a little confused. This is REALLY NEW to me I don't understanding coding well. I see you posted 2 here what should I be doing with the second one? Quote Link to comment https://forums.phpfreaks.com/topic/103613-upload-file-then-chmod-it/#findComment-531845 Share on other sites More sharing options...
MadTechie Posted May 2, 2008 Share Posted May 2, 2008 okay try this (FULL script) <?php // This file handles uploads of avatars // * Returns 1 if avatar uploads are disabled // * Returns 2 if avatar is to large // * Returns 3 if the avatar is not an image type // * Returns 4 if the avatar upload directory is not writeable // * Returns URL to avatar is everythign went ok function handle_uploaded_avatar() { global $x7c, $x7s; // See if the file is a correct image type, either gif, png or jpg switch($_FILES['avatar']['type']) { case "image/gif": $ext = ".gif"; break; case "image/png": $ext = ".png"; break; case "image/jpeg": $ext = ".jpeg"; break; default: return 3; break; } $filepath = $x7c->settings['uploads_path']."/avatar_".$x7s->username.$ext; remove_other_avatars(); chmod($filepath, 0755) OR die("Chmod error"); return $filepath; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/103613-upload-file-then-chmod-it/#findComment-531847 Share on other sites More sharing options...
Datnigz2002 Posted May 2, 2008 Author Share Posted May 2, 2008 Don't mean to sound like a idiot but do you mean to actually overwrite all the PHP with what you have above? Quote Link to comment https://forums.phpfreaks.com/topic/103613-upload-file-then-chmod-it/#findComment-531852 Share on other sites More sharing options...
MadTechie Posted May 2, 2008 Share Posted May 2, 2008 everything you from what you posted in this post Quote Link to comment https://forums.phpfreaks.com/topic/103613-upload-file-then-chmod-it/#findComment-531854 Share on other sites More sharing options...
MadTechie Posted May 2, 2008 Share Posted May 2, 2008 TWAT! not you me i missed a chunk! heres the full thing! <?php // This file handles uploads of avatars // * Returns 1 if avatar uploads are disabled // * Returns 2 if avatar is to large // * Returns 3 if the avatar is not an image type // * Returns 4 if the avatar upload directory is not writeable // * Returns URL to avatar is everythign went ok function handle_uploaded_avatar(){ global $x7c, $x7s; // See if avatar uploads are enabled if($x7c->settings['enable_avatar_uploads'] == 1){ // Make sure the directory is writeable if(!is_writable($x7c->settings['uploads_path'])) return 4; // See if the file is within the correct size limitations if($_FILES['avatar']['size'] > $x7c->settings['avatar_max_size']){ // To large return 2 return 2; }else{ // See if the file is a correct image type, either gif, png or jpg switch($_FILES['avatar']['type']) { case "image/gif": $ext = ".gif"; break; case "image/png": $ext = ".png"; break; case "image/jpeg": $ext = ".jpeg"; break; default: return 3; break; } $filepath = $x7c->settings['uploads_path']."/avatar_".$x7s->username.$ext; remove_other_avatars(); chmod($filepath, 0755) OR die("Chmod error"); return $filepath; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/103613-upload-file-then-chmod-it/#findComment-531859 Share on other sites More sharing options...
Datnigz2002 Posted May 2, 2008 Author Share Posted May 2, 2008 I see so I just replace everything I have with what you wrote and not worry about the rest? I'll see if it works. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/103613-upload-file-then-chmod-it/#findComment-531872 Share on other sites More sharing options...
Datnigz2002 Posted May 6, 2008 Author Share Posted May 6, 2008 I replaced my php file with what was written above but now I only get a blank page with no errors. Quote Link to comment https://forums.phpfreaks.com/topic/103613-upload-file-then-chmod-it/#findComment-534576 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.