Jump to content

Recommended Posts

hey guys i have a small script below i have commented what i need help with if anyone has any ideas ill be very gratefull ive tryed but cannot get it to work at all.

 

This is a small fraction of a large script i'm writing but the easy way to get help with my problem was to section it into below code so don't worry if the script looks pointless as long as it works thats all im after!

 

<?php
    $bad_directory = 'home/username/public_html/';

// I need help to convert the following orange text into PHP please 

// search $bad_directory & list all files created within last hour of current day (file names as a array eg: 'blah.txt','ttl.gif')
// search $bad_directory & list all folders created within last hour of current day (folder names as a array eg: 'blah','ttl')
// get list of last hour created files from above and rename all files with _NEW at end EG: "just_added.php" to "just_added.php_NEW"
// get list of last hour created folders from above and add a .htaccess file to each folder EG on how to create file ,htaccess below

// Create the .htaccess file i need added to each newly added folder ^^^
$filename = '.htaccess';
$contents = 'AuthUserFile '.$htpass_dir.'.htpasswd
AuthName "THIS AREA HAS BEEN LOCKED FOR SECURITY REASONS"
AuthType Basic

require user Admin';
$handle = fopen($filename, 'x+');
fwrite($handle, $contents);
fclose($handle);	
?>

Link to comment
https://forums.phpfreaks.com/topic/266818-need-help-filling-in-the-blanks/
Share on other sites

Might be there are some typos or I have done something wrong, haven't really tested it that much, but I think you get the idea:

<?php

$folder_path = '.';

if($handle = opendir($folder_path)){
$folders = array();
$files = array();
while(false !== ($file = readdir($handle))){
	if($file != '.' && $file != '..' && $file != 'somefile.php'){
		if(filectime($folder_path.'/'.$file) > time()-(60*60)){
			if(is_dir($folder_path.'/'.$file)){
				$folders[] = $file;
			}else{
				$files[] = $file;
			}
		}
	}
}
closedir($handle);
foreach($folders AS $folder){
	//$fh = fopen($folder_path'/'.$folder.'/.htaccess', 'w') or die('Can\'t open file.');
	//fclose($fh);
	// OR
	//copy('folder_path_to/.htaccess', $folder_path'/'.$folder.'/.htaccess');
	// 'folder_path_to/.htaccess' is the path to the htaccess file you want to copy to the new folder
}
foreach($files AS $file){
	rename($folder_path.'/'.$file, $folder_path.'/'.$file.'_NEW');
}
}
?>

I've taken the liberty of cleaning up the code a bit, to make it more readable and utilize the recommended functions.

<?php

$folder_path = '.';

$folders = array ();
$files = array ();

// Fetch all files from the folder, and loop through them.
foreach (glob ($folder_path.'/*') as $file) {
// Skip unwanted files.
if (basename ($file) == 'somefile.php') {
	continue;
}

// Make sure the file has been changed during the last hour.
if (filectime ($file) <= strtotime ("-1 hour")) {
	// Otherwise skip it.
	continue;
}

if (is_dir ($file)) {
	$folders[] = $file;
} else {
	$files[] = $file;
}
}

foreach ($folders as $folder) {
//$fh = fopen($folder_path'/'.$folder.'/.htaccess', 'w') or die('Can\'t open file.');
//fclose($fh);
// OR
//copy('folder_path_to/.htaccess', $folder_path'/'.$folder.'/.htaccess');
// 'folder_path_to/.htaccess' is the path to the htaccess file you want to copy to the new folder
}

foreach ($files as $file) {
rename ($file, $file . '_NEW');
}

 

Notice how much easier it is to read what the code does with the use of strtotime () and glob (), plus the reduction in nesting by "exiting early" in the loop with the use of continue.

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.