Jump to content

Recommended Posts

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);
}
?> 

 

 

Link to comment
https://forums.phpfreaks.com/topic/103613-upload-file-then-chmod-it/
Share on other sites

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.

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");

}

?>

<?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.

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.

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);
?> 

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;
	}

}
?>

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

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;
?>

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;
}
?>

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;
		}
	}
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.