Jump to content

[SOLVED] how to apply permission upon upload


soycharliente

Recommended Posts

This code works fine on my server, but when I loaded it onto the production server, all the uploaded files have 600 permissions. How can I modify it so that upon uploading, the files have 777 permissions? Thanks!

 

<?php
function upload_files($temp_file_name, $file_name, $upload_dir, $upload_log_dir, $max_file_size, $banned_array, $ext_array) {
dbconnect();

//Get Day and Time Variables
$m = date("m");          //Get month
$d = date("d");          //Get day
$y = date("Y");          //Get year
$date = date("m/d/Y");   //Get today's date
$time = date("h:i:s A"); //Get now's time

//Get User Passed Variables
$temp_file_name = trim($temp_file_name);   //Trim Temp File Name
$file_name = trim(strtolower($file_name)); //Trim File Name
$upload_dir = trim($upload_dir);           //Trim Upload Directory
$upload_log_dir = trim($upload_log_dir);   //Trim Upload Log Directory
$max_file_size = trim($max_file_size);     //Trim Max File Size

//Figure if last character for the upload directory is a back slash
$ud_len = strlen($upload_dir);                     //Get upload log directory size
$last_slash = substr($upload_dir, $ud_len - 1, 1); //Get Last Character
if ($last_slash <> "/") {                          //Check to see if the last character is a slash
	$upload_dir = $upload_dir."/";                 //Add a backslash if not present
} else {
	$upload_dir = $upload_dir; //If backslash is present, do nothing
}

//Figure if last character for the upload log directory is a back slash
$udl_len = strlen($upload_log_dir);                     //Get upload log directory size
$last_slash = substr($upload_log_dir, $udl_len - 1, 1); //Get Last Character
if ($last_slash <> "/") {                               //Check to see if the last character is a slash
	$upload_log_dir = $upload_log_dir."/";              //Add a backslash if not present
} else {
	$upload_log_dir = $upload_log_dir; //If backslash is present, do nothing
}

//Validate the extension array
foreach ($ext_array as $key => $value) {            //Start extension loop
	$first_char = substr($value, 0, 1);             //Get first character
		if ($first_char <> ".") {                   //If not a period,
			$extensions[] = ".".strtolower($value); //Write value with a period to a new array
		} else {                                    //Else
			$extensions[] = strtolower($value);     //Write the value to a new array
		}
}

//Get Counts
$ext_count = count($extensions);      //Count the number of extensions
$banned_count = count($banned_array); //Count the number of banned dh_users

//Figure if anyone is banned
if ($banned_count >= 1) {   //If number of banned dh_users if 1 or greater
	$banned_dh_users = "TRUE"; //Set banned_dh_users to TRUE
}

//Get server constants
$ip = $_SERVER['REMOTE_ADDR']; //Get IP address
$self = $_SERVER['PHP_SELF'];  //Get PHP Self
$site = $_SERVER['HTTP_HOST']; //Get Start of Web URL
$ip = $_SERVER['REMOTE_ADDR']; //Get IP Address

//Set file size variables
$kb = 1024;       //Set KB
$mb = 1024 * $kb; //Set MB
$gb = 1024 * $mb; //Set GB
$tb = 1024 * $gb; //Set TB

//Get The File's Extension
$extension = strtolower(strrchr($file_name, ".")); //Get the file extension

//Validate Extension
foreach ($extensions as $key => $value) { //Start extract loop of valid extensions
	if ($value == $extension) {           //If extension is equal to any in the array
		$valid_extension = "TRUE";        //Set valid extension to TRUE
	}
	$all_ext .= $value.", ";              //Get all the extensions
}
$all_ext_len = strlen($all_ext);              //Get the number of characters
$all_ext = substr($all_ext, 0, $all_ext_len - 2); //Extract all text except the last (2) characters

//Validate the file's size
$size = filesize($temp_file_name); //Get the size of the file
	if ($size > $max_file_size) {  //Set over limit statement
		$over_limit = "TRUE";      //Set over_limit to TRUE
	}

//Get the size of the uploaded file in other than bytes
if ($size < $kb) {
	$screen_size = "$size Bytes"; //Set screen_size in bytes, if applicable
} else if ($size < $mb) {
	$final = round($size/$kb, 2);
	$screen_size = "$final KB"; //Set screen_size in kilo-bytes, if applicable
} else if ($size < $gb) {
	$final = round($size/$mb, 2);
	$screen_size = "$final MB"; //Set screen_size in mega-bytes, if applicable
} else if($size < $tb) {
	$final = round($size/$gb, 2);
	$screen_size = "$final GB"; //Set screen_size in giga-bytes, if applicable
} else {
	$final = round($size/$tb, 2);
	$screen_size = "$final TB"; //Set screen_size in tera-bytes, if applicable
}

//Get the size of the max file size in other than bytes
if ($max_file_size < $kb) { //Set screen_max in bytes, if applicable
	$screen_max = "$max_file_size Bytes";
} else if ($max_file_size < $mb) {
	$final = round($max_file_size/$kb, 2);
	$screen_max = "$final KB"; //Set screen_max in kilo-bytes, if applicable
} else if ($max_file_size < $gb) {
	$final = round($max_file_size/$mb, 2);
	$screen_max = "$final MB"; //Set screen_max in mega-bytes, if applicable
} else if($max_file_size < $tb) {
	$final = round($max_file_size/$gb, 2);
	$screen_max = "$final GB"; //Set screen_max in giga-bytes, if applicable
} else {
	$final = round($max_file_size/$tb, 2);
	$screen_max = "$final TB"; //Set screen_max in tera-bytes, if applicable
}

//Validate the banned dh_users list
if ($banned_dh_users) { //If banned dh_users are present
	foreach($banned_array as $key => $value) { //Start extraction of banned dh_users from the array
		if ($value == $ip) {                   //If the user's IP address is found in list, continue
			$banned_ip = "TRUE";               //and set the banned_ip to TRUE
		}
	}
}

//Start the validation process
if ($banned_ip) {
	$result = "You have been banned from uploading any files to this directory!";
	$log = $upload_log_dir."banned.txt";                   //Log Banned File Name
	$fp = fopen($log, "a+");                               //Set File Pointer
	fwrite($fp, "$ip tried to upload on $date at $time."); //Write File
	fclose($fp);                                           //Close File Pointer
} else if (!$valid_extension) {
	return FALSE;
} else if ($over_limit) {
	return FALSE;
} else {
	if (is_uploaded_file($temp_file_name)) {
		$new_file_name = time();
		if (move_uploaded_file($temp_file_name, $upload_dir . $new_file_name . ".jpg")) {
			$log = $upload_log_dir.$y."_".$m."_".$d.".txt";
			if(is_file($log))
			   $fileparams = "a+";
			else
			   $fileparams = "w+";
			$fp = fopen($log, $fileparams);
			/*$fp = fopen($log, "a+");*/
			fwrite($fp, "$ip | $file_name//$new_file_name | $screen_size | $date | $time\r\n");
			fclose($fp);
			return $new_file_name;
		} else {
			$result =  "Your file could not be uploaded, please try again.";
			return FALSE;
		}
	} else {
		$result =  "Your file could not be uploaded, please try again.";
		return FALSE;
	}
}
}
?>

I already tried looking at the chmod page. I put the chmod command in the code in about 10 different places. Didn't work in any of them.

 

Are you on Windows? If so I do not think you can change the permission via php. (Nor do I think it is needed).

I don't understand why it's not working. Is it in the wrong place?

<?php
if (is_uploaded_file($temp_file_name)) {
$new_file_name = time();
if (move_uploaded_file($temp_file_name, $upload_dir . $new_file_name . ".jpg")) {
	$log = $upload_log_dir.$y."_".$m."_".$d.".txt";
	if(is_file($log))
	   $fileparams = "a+";
	else
	   $fileparams = "w+";
	$fp = fopen($log, $fileparams);
	/*$fp = fopen($log, "a+");*/
	fwrite($fp, "$ip | $file_name//$new_file_name | $screen_size | $date | $time\r\n");
	chmod("../upload/$new_file_name.jpg", 0777);
	fclose($fp);
	return $new_file_name;
} else {
	$result =  "Your file could not be uploaded, please try again.";
	return FALSE;
}
} else {
$result =  "Your file could not be uploaded, please try again.";
return FALSE;
}
?>

<?php

// Thanks to "imoldgreg at o2 dot co dot uk" for the base 'CHMOD via FTP' script.

function chmod_open()
{
    // Use your own FTP info
    $ftp_user_name = '[email protected]';
    $ftp_user_pass = 'XXXXXXXXXX';
    $ftp_root = '/';
    $ftp_server = 'localhost';
    $conn_id = ftp_connect($ftp_server);
    $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
    return $conn_id;
}

function chmod_file($conn_id, $permissions, $path)
{
    if (ftp_site($conn_id, 'CHMOD ' . $permissions . ' ' . $ftp_root . $path) !== false)
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}

function chmod_close($conn_id)
{
    ftp_close($conn_id);
}

// CHMOD the required setup files

// Connect to the FTP
$conn_id = chmod_open();

// CHMOD each file and echo the results
echo chmod_file($conn_id, 777, 'master/cache/') ? 'CHMODed successfully!' : 'Error';
echo chmod_file($conn_id, 777, 'master/files/') ? 'CHMODed successfully!' : 'Error';
echo chmod_file($conn_id, 777, 'master/store/') ? 'CHMODed successfully!' : 'Error';
echo chmod_file($conn_id, 766, 'master/config.php') ? 'CHMODed successfully!' : 'Error';
echo chmod_file($conn_id, 777, 'master/images/avatars/upload/') ? 'CHMODed successfully!' : 'Error';

// Close the connection
chmod_close($conn_id);

?>

 

Taken from www.php.net/chmod

 

Maybe that will work out for you using the ftp setup.

  • 3 weeks later...

Has anyone ever changed the file permission of a file once it was uploaded to a server? I can't get this to work. I tried using the code below and it says that it couldn't find the function ftp_connect and I am using PHP 4.

 

This is my code.

<?php
if (is_uploaded_file($temp_file_name)) {
$new_file_name = time();
if (move_uploaded_file($temp_file_name, $upload_dir . $new_file_name . ".jpg")) {
	$log = $upload_log_dir.$y."_".$m."_".$d.".txt";
	if(is_file($log))
	   $fileparams = "a+";
	else
	   $fileparams = "w+";
	$fp = fopen($log, $fileparams);
	/*$fp = fopen($log, "a+");*/
	fwrite($fp, "$ip | $file_name//$new_file_name | $screen_size | $date | $time\r\n");



	fclose($fp);
	return $new_file_name;
} else {
	$result =  "Your file could not be uploaded, please try again.";
	return FALSE;
}
} else {
$result =  "Your file could not be uploaded, please try again.";
return FALSE;
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.