...

View Full Version : Code to get a file



Cliffo
03-25-2010, 05:54 AM
Updated the this post with the segment of code that i need to edit. I am still trying to do this myself but if anyone can help me out thats appreciated.


I am using a cms called webspell and i am trying to create a theme selection system of sorts, but my programming experience is minimal at best. I have a segment of code from my cms that i believe retrieves the files (forums.php, downloads.php ect ect).


if(!function_exists('file_get_contents')) {
function file_get_contents($filename) {
$fd = fopen("$filename", "rb");
$content = fread($fd, filesize($filename));
fclose($fd);
return $content;
}
}

if(!function_exists('str_split')) {
function str_split($str,$split_length=1) {

$cnt = mb_strlen($str);

for ($i=0;$i<$cnt;$i+=$split_length)
$result[]= mb_substr($str,$i,$split_length);

return $result;
}
}

if(!function_exists('str_ireplace')) {
function str_ireplace($search,$replace,$subject) {
$search = preg_quote($search, "/");
return preg_replace("/".$search."/i", $replace, $subject);
}
}

That code retrieves the files from my root, what i want it to do is look for the file inside of root/themes/$activetheme/ then if it does not find the file there it will look inside the root as it does now. If someone can show me how to write that segment of code to do what i need (assuming this is the proper place to edit which i think it is) that would be much appreciated!

DJCMBear
03-25-2010, 06:08 AM
I am using a cms called webspell and i am trying to create a theme selection system of sorts, but my programming experience is minimal at best. I have a segment of code from my cms that i believe retrieves the files (forums.php, downloads.php ect ect).


function systeminc($file) {
if(!include('src/'.$file.'.php')) system_error('Could not get system file for '.$file);
}

That code retrieves the files from my root, what i want it to do is look for the file inside of root/themes/$activetheme/ then if it does not find the file there it will look inside the root as it does now. If someone can show me how to write that segment of code to do what i need (assuming this is the proper place to edit which i think it is) that would be much appreciated!

try this:


function systeminc($file,$activetheme) {
if(file_exists('themes/'.$activetheme.'/'.$file.'.php'){
if(!include('themes/'.$activetheme.'/'.$file.'.php')) system_error('Could not get system file for '.$file);
}else{
if(!include('src/'.$file.'.php')) system_error('Could not get system file for '.$file);
}
}


and then use:


systeminc('filename','current theme name');


but if it's like the last script and your using it inside a folder you may need to add ../ before the word themes.

Cliffo
03-25-2010, 06:15 AM
I got an error,

Parse error: syntax error, unexpected '{' in /home/cgshost/cgstest/_settings.php on line 172

which is the 2nd line of that code.

I might need to mess around with some other files to get this working.

DJCMBear
03-25-2010, 06:17 AM
Dumb me it needs to have two ')' at the end before the {.

Heres the fixed code:


function systeminc($file,$activetheme) {
if(file_exists('themes/'.$activetheme.'/'.$file.'.php')){
if(!include('themes/'.$activetheme.'/'.$file.'.php')) system_error('Could not get system file for '.$file);
}else{
if(!include('src/'.$file.'.php')) system_error('Could not get system file for '.$file);
}
}

Cliffo
03-25-2010, 06:40 AM
It doesn't seem to be working, that doesn't mean it is your code i might have to be making edits elsewhere i haven't discovered yet lol

DJCMBear
03-25-2010, 06:44 AM
It doesn't seem to be working, that doesn't mean it is your code i might have to be making edits elsewhere i haven't discovered yet lol

Yes there could be some other edits to make but im not the worlds number one coder so it could be my code =) and i used codes like this before and thats the way I would normal write it.

Cliffo
03-25-2010, 06:52 AM
When you say use;

systeminc('filename','current theme name');

do you mean

systeminc('filename','$activetheme');

????

DJCMBear
03-25-2010, 07:06 AM
When you say use;

systeminc('filename','current theme name');

do you mean

systeminc('filename','$activetheme');

????

i mean



systeminc('filename',$activetheme);


and if the file name is header lets say you would put header insted of filename.

Cliffo
03-25-2010, 07:11 AM
Just making sure, i am assuming i need to edit some code elsewhere to achieve what i want, not too sure at the moment, but thanks for the help.

A lot of themes overwrite .php files in the root, what i am trying to do is find what function calls those files and make it check for it in the activethemes folder first, but it seems that code might not be the part i need to edit.

Cliffo
03-25-2010, 08:07 AM
Would this be the code i need to edit?


if(!function_exists('file_get_contents')) {
function file_get_contents($filename) {
$fd = fopen("$filename", "rb");
$content = fread($fd, filesize($filename));
fclose($fd);
return $content;
}
}

if(!function_exists('str_split')) {
function str_split($str,$split_length=1) {

$cnt = mb_strlen($str);

for ($i=0;$i<$cnt;$i+=$split_length)
$result[]= mb_substr($str,$i,$split_length);

return $result;
}
}

if(!function_exists('str_ireplace')) {
function str_ireplace($search,$replace,$subject) {
$search = preg_quote($search, "/");
return preg_replace("/".$search."/i", $replace, $subject);
}
}

if not maybe


function makepagelink($link, $page, $pages, $sub='') {
$page_link = '<span class="pagelink"><img src="images/icons/multipage.gif" width="10" height="12" alt="" /> <small>';

if($page != 1) $page_link .= '&nbsp;<a href="'.$link.'&amp;'.$sub.'page=1">&laquo;</a>&nbsp;<a href="'.$link.'&amp;'.$sub.'page='.($page-1).'">&lsaquo;</a>';
if($page >= 6) $page_link .= '&nbsp;<a href="'.$link.'&amp;'.$sub.'page='.($page-5).'">...</a>';
if($page+4 >= $pages) $pagex=$pages;
else $pagex = $page+4;
for($i=$page-4 ; $i<=$pagex ; $i++) {
if($i<=0) $i=1;
if($i==$page) $page_link .= '&nbsp;<b><u>'.$i.'</u></b>';
else $page_link .= '&nbsp;<a href="'.$link.'&amp;'.$sub.'page='.$i.'">'.$i.'</a>';
}
if(($pages-$page) >= 5) $page_link .= '&nbsp;<a href="'.$link.'&amp;'.$sub.'page='.($page+5).'">...</a>';
if($page != $pages) $page_link .= '&nbsp;<a href="'.$link.'&amp;'.$sub.'page='.($page+1).'">&rsaquo;</a>&nbsp;<a href="'.$link.'&amp;'.$sub.'page='.$pages.'">&raquo;</a>';
$page_link .= '</small></span>';

return $page_link;
}


possibly


function detectfilesize($size, $round='2') {
$filesize=$size;
for($i=0;$filesize>=1024;$i++) {
$filesize = $filesize/1024;
}
$filesize = round($filesize,$round);
switch($i) {
case 0: $filesize = $filesize." Byte"; break;
case 1: $filesize = $filesize." kB"; break;
case 2: $filesize = $filesize." MB"; break;
case 3: $filesize = $filesize." GB"; break;
case 4: $filesize = $filesize." TB"; break;
default: $filesize = $size." Byte"; break;
}
return $filesize;
}

function getdirsize($dir) {

$size=0;
$handle = opendir($dir);
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if(is_dir($dir.$file)) $size = $size + getdirsize($dir.$file.'/');
else $size=$size+filesize($dir.$file);
}
}
return $size;
}

function rm_recursive($filepath){
if (is_dir($filepath) && !is_link($filepath)){
if ($dh = @opendir($filepath)){
while (($sf = readdir($dh)) !== false){
if ($sf == '.' || $sf == '..'){
continue;
}
if (!rm_recursive($filepath.'/'.$sf)){
return false;
}
}
closedir($dh);
}
return @rmdir($filepath);
}
return @unlink($filepath);
}


or it is the first one i tried;


function systeminc($file) {
if(!include('src/'.$file.'.php')) system_error('Could not get system file for '.$file);
}

i cant think of any other code segment that might be the one i need to edit, any of those seem like they would be the one i need?



That code retrieves the files from my root, what i want it to do is look for the file inside of root/themes/$activetheme/ then if it does not find the file there it will look inside the root as it does now.

DJCMBear
03-25-2010, 08:19 AM
I would suggest the first batch of code section because that seems to be looking for a file to read it.

Cliffo
03-25-2010, 08:39 AM
I am thinking the same, but i was hoping it was the last because it is the smallest hahaha

Cliffo
03-25-2010, 08:53 AM
So basically the following line need to be made into an if/else statement looking for root/themes/$activetheme/$filename and then root/$filename


$fd = fopen("$filename", "rb");

is that what i need?

DJCMBear
03-25-2010, 09:48 AM
basicly you need your code to look inside the active theme folder inside your themes folder and if its not in there then look in src dir so yes an if else would be needed.

Cliffo
03-25-2010, 06:22 PM
Bah i cant get it to work, anyone want to help me write this segment?

MattF
03-25-2010, 06:39 PM
If you want to e-mail me a copy of your code over, I could have a look at it later, (provided no-one gets you sorted with a solution beforehand, obviously :D).

Cliffo
03-25-2010, 10:21 PM
I deleted the code last night because I couldn't get it to work, ill give it another shot and link it here.

Cliffo
03-25-2010, 10:30 PM
Okay this is the best i could muster;

changing;


if(!function_exists('file_get_contents')) {
function file_get_contents($filename) {
$fd = fopen("$filename", "rb");
$content = fread($fd, filesize($filename));
fclose($fd);
return $content;
}
}

to;


if(!function_exists('file_get_contents')) {
function file_get_contents($filename) {

global $activetheme;

$customLocal = 'root/themes/' . $activetheme . '/';

if (!is_readable($customLocal))
{
$fd = fopen("themes/$activetheme/$filename", "rb");
}

else
{
$fd = fopen("$filename", "rb");
}

$content = fread($fd, filesize($filename));
fclose($fd);
return $content;
}
}

and basically it made no difference.

Cliffo
03-26-2010, 07:14 AM
nevermind



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum