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

php include code problem

Post by mohsin360 »

my php include code is running very well by which i can impot contents of a .txt and print store in a same directory.
my code is

Code: Select all

<?PHP include "textfile.txt" ; ?>
this
-----------------------------------------------------------------

but i want to include a .txt file store in another directory like the address of the file is http://www.somesite.com/textfile.txt. is there some way by include or other than include function to include a file hosted on some server plz help.
webmonkey88
Forum Newbie
Posts: 20
Joined: Fri Aug 14, 2009 4:30 am

Re: php include code problem

Post by webmonkey88 »

it should be include("");

and you can use file_get_contents()
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: php include code problem

Post by cpetercarter »

To be totally pedantic, you don't need to use brackets with 'include' (or 'require'), because 'include' is a statement, not a function. It's just one of those useless bits of information that clutter my brain.....
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 »

include is a construct, just like echo, or...return. You don't need to use parenthesis.

Anyway, you can use fopen(), file_get_contents() curl, sockets, or include if you have allow url include turned on.
mohsin360
Forum Newbie
Posts: 18
Joined: Sat Aug 15, 2009 2:39 am

Re: php include code problem

Post by mohsin360 »

thankz to u all for answering my question.
i have another question plz help me again.

problem is that file_get_contents is work well and reflects all contents in my txt file.

but if there is any possiblity that file_get_contents or any other function reflects only one line of a txt file likemy txt file is something like this
  • 123
    456
    789
    101
    111
    121
and i want to only get line no 3 which contains content[789]
is there some way
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 »

Code: Select all

$file = explode("\n", file_get_contents('txtfile.txt'));
 
echo $file[2];
Try that.
mohsin360
Forum Newbie
Posts: 18
Joined: Sat Aug 15, 2009 2:39 am

Re: php include code problem

Post by mohsin360 »

thankX a lot this also work and reduce my problem thanX again
mohsin360
Forum Newbie
Posts: 18
Joined: Sat Aug 15, 2009 2:39 am

Re: php include code problem

Post by mohsin360 »

thank X for all reply
sorry if i ask too many questions but i have 1 more

is there any possibility that a php page can automatically rename itself from a line of text from a remote .txt file ? i know its tricky
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 »

Use that code to find the filename, then use rename().
mohsin360
Forum Newbie
Posts: 18
Joined: Sat Aug 15, 2009 2:39 am

Re: php include code problem

Post by mohsin360 »

use what code to find file name?

i read about it at php official web and find that this code used to rename some file other than the file containing code ?
mohsin360
Forum Newbie
Posts: 18
Joined: Sat Aug 15, 2009 2:39 am

Re: php include code problem

Post by mohsin360 »

i uploaded two files in http://www.myweb.com/mohsin3 named index.php containing code

Code: Select all

<?php
rename("index2.php","index3.php");
?>
and another file index2.php to rename but when i try to visit page it shows something like this

Code: Select all

Warning: rename(index2.php,index3.php) [function.rename]: Permission denied in /home/jcipakis/public_html/mohsin3/index.php on line 2
mohsin360
Forum Newbie
Posts: 18
Joined: Sat Aug 15, 2009 2:39 am

Re: php include code problem

Post by mohsin360 »

i tried another hosting service it also shows something like this

Code: Select all

Warning: rename(index2.php,index3.php) [function.rename]: Permission denied in /home/a2992386/public_html/mohsin3/index.php on line 2
this server have php version 5.2.9
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 »

Have you tried giving the file 777 permissions?
mohsin360
Forum Newbie
Posts: 18
Joined: Sat Aug 15, 2009 2:39 am

Re: php include code problem

Post by mohsin360 »

no i dont kno abt that how to do it ?
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 »

If you're using an FTP client, you should be able right click the file and click "permissions" or "chmod" or something similar.
Post Reply