I have this script:
$dl = array('st' => false, 'smg' => '');
$fileR = file($PhatToFile);
$fileR = array_reverse($fileR);
$c = count($fileR) + 1;
foreach ($fileR as $line) {
if (!strlen(rtrim($line))) {
$dl['smg'] .= 'Incorrect space found file: <b> ' . $file . '</b> Line: <b>' . $c . '</b><br>';
$dl['st'] = true;
$c = $c - 1;
} else {
break;
}
}
if($dl['st'] == true){
echo $dl['smg'];
}
I use it to determine if file End with Space o Line Break, but not work with this:
<?php
echo "hello world";
?> (Line break)
(no find this... line 5 have line break and file end in 6)
Check I have enumerate each line.
please try this, I have replaced your if statement.
foreach ($fileR as $line) {
if (!trim(preg_replace('/\s+/', '', $line))) {
$dl['smg'] .= 'Incorrect space found file: <b> ' . $file . '</b> Line: <b>' . $c . '</b><br>';
$dl['st'] = true;
$c = $c - 1;
} else {
break;
}
}
i solve this with, is capable detecting:
white space or line break at start of file.
white space or line break at end of file.
$dl = array('st' => false, 'smg' => '');
$smg1 = $smg3 = '';
$smg2 = [];
$fileR = file($file);
$c = 1;
foreach ($fileR as $line) {
if (!strlen(trim($line))) {
$smg1 .= 'Incorrect space/Linebreak found file: <b> ' . $file . '</b> Line: <b>' . $c . '</b><br>';
$dl['st'] = true;
$c++;
} else {
break;
}
}
$fileR = array_reverse($fileR);
$rc = count($fileR);
foreach ($fileR as $line) {
if (!strlen(trim($line))) {
$smg2[] = 'Incorrect space/Linebreak found file: <b> ' . $file . '</b> Line: <b>' . $rc . '</b><br>';
$dl['st'] = true;
$rc--;
} else {
break;
}
}
$fileR = array_reverse($fileR);
if ((substr($fileR[$rc - 1], -1) == "\n") AND strlen(trim($fileR[$rc - 1])) > 0) {
$smg3 .= 'Incorrect Linebreak found file: <b> ' . $file . '</b> Line: <b>' . $rc . '</b><br>';
$dl['st'] = true;
}
$smg2 = array_reverse($smg2);
echo $dl['smg'] = $smg1 . $smg3 . implode('', $smg2);
Related
I have created a form,I read from a INI file and show its values in the form.
Now I want to edit that file.
So I show values in text box and let the user edit the file.
Now I am using INI Library to write my files, but my code changes the structure of INI file, also it remove the comments.
Is it possible that when I write to the file only those value is changed and other things like comments and Section remain as it is ?
This is my code:
$CFG_file = $this->data['file_data'] = parse_ini_file("CFG/fms.cfg" ,true);
if (isset($_POST['submit']))
{
//var_dump($_POST);
$data_file = $this->parameter_m->array_from_post(array('VAL1','VAL2','InMedia','VAL','VAL','VAL5','VAL3'));
$this->load->helper('file');
$file = "CFG/fms.cfg";
$this->load->library('ini');
$ini = new INI($file);
$ini->write($file, $data_file );
}
And the library code that writes file :
function write($file = NULL, $file_content = array(), $sections = FALSE) {
$this->file_content = (!empty($file_content)) ? $file_content : $this->file_content;
$this->file = ($file) ? $file : $this->file;
$this->sections = $sections;
$content = NULL;
if ($this->sections) {
foreach ($this->file_content as $section => $file_content) {
$content .= '[' . $section . ']' . PHP_EOL;
foreach ($file_content as $key => $val) {
if (is_array($val)) {
foreach ($val as $v) {
$content .= $key . '[]=' . (is_numeric($v) ? $v : $v ) . PHP_EOL;
}
} elseif (empty($val)) {
$content .= $key . '=' . PHP_EOL;
} else {
$content .= $key . '=' . (is_numeric($val) ? $val : $val ) . PHP_EOL;
}
}
$content .= PHP_EOL;
}
} else {
foreach ($this->file_content as $key => $val) {
if (is_array($val)) {
foreach ($val as $v) {
$content .= $key . '[] = ' . (is_numeric($v) ? $v : '"' . $v . '"') . PHP_EOL;
}
} elseif (empty($val)) {
$content .= $key . ' = ' . PHP_EOL;
} else {
$content .= $key . ' = ' . (is_numeric($val) ? $val : '"' . $val . '"') . PHP_EOL;
}
}
}
return (($handle = fopen($this->file, 'w+')) && fwrite($handle, trim($content)) && fclose($handle)) ? TRUE : FALSE;
}
And this is my file :
[]
VAL1= 0.0.0.0
VAL2= 5070
;media settings
;ON/OFF/AUTO
VAL3= OFF
VAL4= 8000
VAL5= 4000
VAL6= 360
;Transport settings
;ON/OFF
VAL7= OFF
When i execute my code this becomes :
VAL1= "0.0.0.0"
VAL2 = 5071
VAL3 =
VAL4 = 8000
VAL5 = 4000
VAL6 = 360
VAL7 = "ON"
I open a .ini, then write some add some values to its parameters and then i saved it back.
My writing the content works perfectly fine, but when i open the file the previous content of that file is cleared.[Maybe due to my write mode].
What change should i do in my code to let user see what the previous content was when he edits a file.
file_get_contents($path.$filename);
/*Get All The datas from that file*/
$this->data['params'] = $this->parameter_m->get();
/*Getting the parameters to display on view*/
$this->data['parameters'] = parse_ini_file($path.$filename,true);
while (current($this->data['parameters']) )
{
$param_set = current($this->data['parameters']);
$param_type = key($this->data['parameters']);
foreach ($param_set as $key =>$value)
{
$this->data['parameters'][$param_type][$key] = $this->input->post($key);
}
next($this->data['parameters']);
}
$this->load->helper('file');
$this->load->library('ini');
$file = $path.$filename;
$ini = new INI($file);
$ini->read($file);
$ini->write($file, $this->data['parameters']);
My Write Function
function write($file = NULL, $data_file = array(), $sections = TRUE) {
$this->data_file = (!empty($data_file)) ? $data_file : $this->data_file;
$this->file = ($file) ? $file : $this->file;
$this->sections = $sections;
$content = NULL;
if ($this->sections) {
foreach ($this->data_file as $section => $data_file) {
$content .= '[' . $section . ']' . PHP_EOL;
foreach ($data_file as $key => $val) {
if (is_array($val)) {
foreach ($val as $v) {
$content .= $key . '[] = ' . (is_numeric($v) ? $v : $v ) . PHP_EOL;
}
} elseif (empty($val)) {
$content .= $key . ' = ' . PHP_EOL;
} else {
$content .= $key . ' = ' . (is_numeric($val) ? $val : $val ) . PHP_EOL;
}
}
$content .= PHP_EOL;
}
} else {
foreach ($this->data_file as $key => $val) {
if (is_array($val)) {
foreach ($val as $v) {
$content .= $key . '[] = ' . (is_numeric($v) ? $v : '"' . $v . '"') . PHP_EOL;
}
} elseif (empty($val)) {
$content .= $key . ' = ' . PHP_EOL;
} else {
$content .= $key . ' = ' . (is_numeric($val) ? $val : '"' . $val . '"') . PHP_EOL;
}
}
}
return (($handle = fopen($this->file, 'w+')) && fwrite($handle, trim($content)) && fclose($handle)) ? TRUE : FALSE;
}
I what to put a span element for $term['nodes']
I have tried to put after bracket and between but nothing works for me
if (isset($term['nodes'])) {
$term['name'] = $term['name'] . ' (' . $term['nodes'] . ')';
}
here is the all functin
function bootstrap_taxonomy_menu_block($variables) {
$tree = $variables['items'];
$config = $variables['config'];
$num_items = count($tree);
$i = 0;
$output = '<ul class="nav nav-pills nav-stacked">';
foreach ($tree as $tid => $term) {
$i++;
// Add classes.
$attributes = array();
if ($i == 1) {
$attributes['class'][] = '';
}
if ($i == $num_items) {
$attributes['class'][] = '';
}
if ($term['active_trail'] == '1') {
$attributes['class'][] = 'active-trail';
}
if ($term['active_trail'] == '2') {
$attributes['class'][] = 'active';
}
// Alter link text if we have to display the nodes attached.
if (isset($term['nodes']))
{
$term['name'] = $term['name'] . ' (<span>' . $term['nodes'] . '</span>)';
}
// Set alias option to true so we don't have to query for the alias every
// time, as this is cached anyway.
$output .= '<li' . drupal_attributes($attributes) . '>' . l($term['name'], $term['path'], $options = array('alias' => TRUE));
if (!empty($term['children'])) {
$output .= theme('taxonomy_menu_block__' . $config['delta'], (array('items' => $term['children'], 'config' => $config)));
}
$output .= '</li>';
}
$output .= '</ul>';
return $output;
}
i what this for the bootstrap cdn class , i have move the function on template.php , of drupal theme , but the span element is in plain text in browser
Try this:
if (isset($term['nodes']))
{
$term['name'] = $term['name'] . ' (<span>' . $term['nodes'] . '</span>)';
echo $term['name']; // To see the output
}
My following code is tested by my friends. They said its working fine. But I am unable to use it. Can anybody find where the error is.
`
<?php
try
{
// Change to your mail server
$host = "pop.gmail.com";
// Connecting to POP3 email server.
$connection = imap_open("{" . $host . ":995/pop3/notls}", 'someusername#gmail.com', 'somepassword');
// Total number of messages in Inbox
$count = imap_num_msg($connection);
echo $count . " messages found<br />";
// Read Messages in Loop, Forward it to Actual User email and than delete it from current email account.
for ($i = 1; $i <= $count; $i++) {
$headers = imap_headerinfo($connection, $i);
$subject = $headers->subject;
$from = $headers->from[0]->mailbox . '#' . $headers->from[0]->host;
if ($headers->cc[0]->mailbox)
$cc = $headers->cc[0]->mailbox . '#' . $headers->cc[0]->host;
$subject = $headers->subject;
$structure = imap_fetchstructure($connection, $i);
$type = $this->get_mime_type($structure);
// GET HTML BODY
$body = $this->get_part($connection, $i, "");
//$raw_body = imap_body($connection, $i);
$attachments = array();
if (isset($structure->parts) && count($structure->parts)) {
for ($e = 0; $e < count($structure->parts); $e++) {
$attachments[$e] = array('is_attachment' => false, 'filename' => '', 'name' => '', 'attachment' => '');
if ($structure->parts[$e]->ifdparameters) {
foreach ($structure->parts[$e]->dparameters as $object) {
if (strtolower($object->attribute) == 'filename') {
$attachments[$e]['is_attachment'] = true;
$attachments[$e]['filename'] = $object->value;
} //if (strtolower($object->attribute) == 'filename')
} //foreach ($structure->parts[$e]->dparameters as $object)
} //if ($structure->parts[$e]->ifdparameters)
if ($structure->parts[$e]->ifparameters) {
foreach ($structure->parts[$e]->parameters as $object) {
if (strtolower($object->attribute) == 'name') {
$attachments[$e]['is_attachment'] = true;
$attachments[$e]['name'] = $object->value;
} //if (strtolower($object->attribute) == 'name')
} //foreach ($structure->parts[$e]->parameters as $object)
} //if ($structure->parts[$e]->ifparameters)
if ($attachments[$e]['is_attachment']) {
$attachments[$e]['attachment'] = #imap_fetchbody($connection, $i, $e + 1);
if ($structure->parts[$e]->encoding == 3) {
// 3 = BASE64
$attachments[$e]['attachment'] = base64_decode($attachments[$e]['attachment']);
} //if ($structure->parts[$e]->encoding == 3)
elseif ($structure->parts[$e]->encoding == 4) {
// 4 = QUOTED-PRINTABLE
$attachments[$e]['attachment'] = quoted_printable_decode($attachments[$e]['attachment']);
} //elseif ($structure->parts[$e]->encoding == 4)
} //if ($attachments[$e]['is_attachment'])
if ($attachments[$e]['is_attachment']) {
$filename = $attachments[$e]['filename'];
$filename = $attachments[$e]['name'];
$filecontent = $attachments[$e]['attachment'];
} //if ($attachments[$e]['is_attachment'])
} //for ($e = 0; $e < count($structure->parts); $e++)
} //if (isset($structure->parts) && count($structure->parts))
/**** ****/
/*echo "<pre>";
echo "From: " . $headers->Unseen . "<br />";
echo "From: " . $from . "<br />";
echo "Cc: " . $cc . "<br />";
echo "Subject: " . $subject . "<br />";
echo "Content Type: " . $type . "<br />";
echo "Body: " . $body . "<br />";*/
$mail = new Zend_Mail();
$mail->settype(Zend_Mime::MULTIPART_MIXED);
for ($k = 0; $k < count($attachments); $k++) {
$filename = $attachments[$k]['name'];
$filecontent = $attachments[$k]['attachment'];
if ($filename && $filecontent) {
$file = $mail->createAttachment($filecontent);
$file->filename = $filename;
} //if ($filename && $filecontent)
} //for ($k = 0; $k < count($attachments); $k++)
$mail->setFrom($from);
$mail->addTo('testmail#softmail.me');
if ($cc)
$mail->addCc($cc);
$mail->setSubject($subject);
$mail->setBodyHtml($body);
$mail->send();
// Mark the email messages once read
//imap_delete($mbox, 1);
} //for ($i = 1; $i <= $count; $i++)
// Delete all marked message from current email account.
imap_expunge($mbox);
echo 'If you see this, the number is 1 or below';
}
catch(Exception $e)
{
echo 'Message: ' .$e->getMessage();
}
?>
Please check if you have enabled
extension=php_openssl.dll;
ext in you php.ini. I had faced this problem once and enabling the above extension worked for me.
I have used to the Rackspace API to upload files to the RackSpace cloud. But this method seems to be a little on the slow side. Is there a better or faster way to upload a file to the cloud(curl, http adapters, etc)?
I am currently uploading with PHP and using the provided API.
Here is my solution how to make it fast:
I'm uploading only missing files using simple PHP script below. Thanks to it I do it in just one click and in just a few seconds.
PHP source code:
function UploadMissingFilesToRackFileCDN($file_paths_to_upload, $b_force_upload = false)
{
include_once("cloudfiles.php");
// Connect to Rackspace
$username = cloudfile_username; // username
echo "Connecting to CDN..." . date("H:i:s") . "<br>"; ob_flush();
$key = cloudfile_api_key; // api key
$auth = new CF_Authentication($username, $key);
$auth->authenticate();
$conn = new CF_Connection($auth);
echo " Connected!" . date("H:i:s") . "<br>"; ob_flush();
// Get the container we want to use
$container_name = 'vladonai';//'test_container';
echo "Obtaining container $container_name..." . date("H:i:s") . "<br>"; ob_flush();
$container = $conn->get_container($container_name);
echo " The container is obtained." . date("H:i:s") . "<br>"; ob_flush();
if (!$b_force_upload)
{
echo "Receiving container objects list..." . date("H:i:s") . "<br>"; ob_flush();
$existing_object_names = $container->list_objects();
$existing_files_count = count($existing_object_names);
echo " Objects list obtained: $existing_files_count." . date("H:i:s") . "<br>"; ob_flush();
$existing_object_names_text .= "\r\n";
foreach ($existing_object_names as $obj_name)
{
$existing_object_names_text .= $obj_name . "\r\n";
}
}
// upload files to Rackspace
$uploaded_file_n = 0;
$skipped_file_n = 0;
$errors_count = 0;
foreach ($file_paths_to_upload as $localfile_path => $file_info)
{
$filename = basename($localfile_path);
if (!file_exists($localfile_path))
{
echo "<font color=red>Error! File $localfile_path doesn't exists!</font>" . date("H:i:s") . "<br>"; ob_flush();
$errors_count ++;
} else
if (is_dir($localfile_path))
{
//simply skip it
} else
if (strpos($existing_object_names_text, "\r\n" . $filename . "\r\n") !== false)
{
//file is already uploaded to CDN (at least file name is present there). Would be good to have date/size checked, but CDN api has no such feature
//echo "<font color=gray>Skipped file $localfile_path - it already exists!</font><br>"; ob_flush();
$skipped_file_n ++;
} else
{
echo "<font color=green>Uploading file $localfile_path (file #$uploaded_file_n)..." . date("H:i:s") . "</font><br>"; ob_flush();
try
{
$object = $container->create_object($filename);
$object->load_from_filename($localfile_path);
$uploaded_file_n ++;
}
catch (Exception $e)
{
echo "<font color=red>Error! Caught exception: ", $e->getMessage(), " on uploading file <strong>$localfile_path</strong>!</font>" . date("H:i:s") . "<br>"; ob_flush();
$errors_count ++;
}
}
// if ($uploaded_file_n >= 10)
// break;
}
echo "Done! $uploaded_file_n files uploaded. Disconnecting :)" . date("H:i:s") . "<br>"; ob_flush();
echo "Skipped files: $skipped_file_n<br>"; ob_flush();
if ($errors_count > 0)
echo "<font color=red>Erorrs: $errors_count</font><br>"; ob_flush();
}
function UploadChangedImagesToRackFileCDN($b_force_upload = false)
{
$exclude = array
(
'.',
'..',
'*.html',
'*.htm',
'*.php',
'*.csv',
'*.log',
'*.txt',
'*.cfg',
//'*sub/forum/files/*',
);
$files_array_images = get_dirlist("/var/www/html/vladonai.com/images/", '*', $exclude, false);
$files_array = array_merge(get_dirlist("/var/www/html/vladonai.com/js/", '*', $exclude, false), $files_array_images);
UploadMissingFilesToRackFileCDN($files_array, $b_force_upload);
}
function get_dirlist($path, $match = '*', $exclude = array( '.', '..' ), $b_short_path = true)
{
$result = array();
if (($handle = opendir($path)))
{
while (false !== ($fname = readdir($handle)))
{
$skip = false;
if (!empty($exclude))
{
if (!is_array($exclude))
{
$skip = fnmatch($exclude, $fname) || fnmatch($exclude, $path . $fname);
} else
{
foreach ($exclude as $ex)
{
if (fnmatch($ex, $fname) || fnmatch($ex, $path . $fname))
$skip = true;
}
}
}
if (!$skip && (empty($match) || fnmatch($match, $fname)))
{
$file_full_path_and_name = $path . $fname;
//echo "$file_full_path_and_name<br>";
$b_dir = is_dir($file_full_path_and_name);
$b_link = is_link($file_full_path_and_name);
$file_size = ($b_dir || $b_link) ? 0 : filesize($file_full_path_and_name);
$file_mod_time = ($b_dir || $b_link) ? 0 : filemtime($file_full_path_and_name);
$new_result_element = array();
if ($b_short_path)
$file_name = str_replace("/var/www/html/vladonai.com/", "", $file_full_path_and_name);//'[' . str_replace("/var/www/html/vladonai.com/", "", $file_full_path_and_name) . ']';
else
$file_name = $file_full_path_and_name;
$result[$file_name] = array();
$result[$file_name]['size'] = $file_size;
$result[$file_name]['modtime'] = $file_mod_time;
if ($b_dir && !$b_link)
{
//recursively enumerate files in sub-directories
$result = array_merge(get_dirlist($file_full_path_and_name . "/", $match, $exclude, $b_short_path), $result);
}
}
}
closedir($handle);
}
return $result;
}