php include code problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

mohsin360
Forum Newbie
Posts: 18
Joined: Sat Aug 15, 2009 2:39 am

Re: php include code problem

Post by mohsin360 »

ya i find it and do it
mohsin360
Forum Newbie
Posts: 18
Joined: Sat Aug 15, 2009 2:39 am

Re: php include code problem

Post by mohsin360 »

Code: Select all

<?php
$path="/home/jcipakis/public_html/mohsin3/";
$msn=basename($path,".php");
echo $msn;
?>

this code tells me only directory but not the filename
my file store in public_html/mohsin3 and name of my file is index3.php

when i visit the page it prints mohsin3 the last folder of my dir ?
what can i do?
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: php include code problem

Post by jackpf »

I don't really understand what you're trying to do, but to include files from an absolute path, you can do this:

Code: Select all

include $_SERVER['DOCUMENT_ROOT'].'/index3.php';
mohsin360
Forum Newbie
Posts: 18
Joined: Sat Aug 15, 2009 2:39 am

Re: php include code problem

Post by mohsin360 »

i have a file A and name of file a is index.php
i want it to find its own name and if its name is index.php it changes it to index3.php and if it find its own name index3.php it will dont doany thing
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: php include code problem

Post by Eran »

you can use the __FILE__ magic constant that gives you the current complete path + file name.

Code: Select all

$file = basename(__FILE__);
if( $file == 'index.php' ) {
    header('Location: index3.php');
}
 
mohsin360
Forum Newbie
Posts: 18
Joined: Sat Aug 15, 2009 2:39 am

Re: php include code problem

Post by mohsin360 »

i tried this

Code: Select all

<?php
 
$filename= basename(__FILE__);
$newname= include "newname.txt" ;
$oldname= include "oldname.txt" ;
 
if($filename != $newname){
 
rename("$oldname","$newname");
}
else{
header('Location: $newname');
}
 
 
?>
the two files newname.txt have text [new.php]
and oldname.txt have text [old.php]


but does not work i think there is some problem about names
it shows me

new.phpold.php
Warning: rename(1,1) [function.rename]: No such file or directory in /home/a2992386/public_html/mohsin4/index.php on line 9
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: php include code problem

Post by cpetercarter »

If you write

Code: Select all

$newname = include "newname.txt";
 
$newname will have the value 1 if the include is successful or 0 if it is not successful. It will not contain the text of whatever is written in "newname.txt". To do this, you need to open and read "newname.txt" into $newname.
mohsin360
Forum Newbie
Posts: 18
Joined: Sat Aug 15, 2009 2:39 am

Re: php include code problem

Post by mohsin360 »

Code: Select all

<?php
 
$filename= basename(__FILE__);
$newname= readfile("newname.txt");
$oldname= readfile("oldname.txt");
 
if($filename != $newname){
 
rename("$oldname","$newname");
}
else{
header('Location: $newname');
}
 
 
?>
i tried this but results are same

it shows
new.phpold.php

Warning: rename(7,7) [function.rename]: No such file or directory in /home/a2992386/public_html/mohsin4/index.php on line 9
mohsin360
Forum Newbie
Posts: 18
Joined: Sat Aug 15, 2009 2:39 am

Re: php include code problem

Post by mohsin360 »

so what can i do now ?
and error is not in
header statement it shows in rename statement
Post Reply