TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-29-2007, 06:53 AM   #1 (permalink)
The Frequenter
 
Join Date: Dec 2007
Location: In my basement
Posts: 386
Thanks: 47
Aaron is on a distinguished road
Default How would I include into a variable?

How would I have something like this work:

$pass = include("pass.txt");

It is moi importante.
Send a message via MSN to Aaron
Aaron is offline  
Reply With Quote
Old 12-29-2007, 06:54 AM   #2 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Look into file handling.
__________________

Village Idiot is offline  
Reply With Quote
Old 12-29-2007, 02:03 PM   #3 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

Quote:
Originally Posted by Aaron View Post
How would I have something like this work:

$pass = include("pass.txt");

It is moi importante.
You have to return the output (or what you need) from that include file, just like you would in a function. Here's an example:

include.php
Code:
$x = 2+4;
return $x;
index.php:
Code:
$sum = include_once 'include.php';
echo $sum * 4; // should output 24
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
The Following 3 Users Say Thank You to xenon For This Useful Post:
Aaron (12-29-2007), deflated (12-29-2007), sjaq (12-29-2007)
Old 12-29-2007, 04:25 PM   #4 (permalink)
The Wanderer
 
deflated's Avatar
 
Join Date: Dec 2007
Location: 127.0.0.1
Posts: 19
Thanks: 7
deflated is on a distinguished road
Default

Thank you very much, xenon. That's totally new to me.
deflated is offline  
Reply With Quote
Old 12-29-2007, 11:09 PM   #5 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

For regular files (ie, files that don't have any PHP code that needs parsing), I would recommend using file_get_contents():

PHP Code:
$contents file_get_contents('my_text_file.txt');

// $contents will now contain everything that was in my_text_file.txt 
Alan
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
The Following User Says Thank You to Alan @ CIT For This Useful Post:
Aaron (12-29-2007)
Old 12-30-2007, 12:09 AM   #6 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

Quote:
Originally Posted by Alan @ CIT View Post
For regular files (ie, files that don't have any PHP code that needs parsing), I would recommend using file_get_contents():

PHP Code:
$contents file_get_contents('my_text_file.txt');

// $contents will now contain everything that was in my_text_file.txt 
Alan
Honestly the best way. Make sure you have PHP, preferably 4.30 +. Else it completely screws up the code or it doesn't work at all. You can also use file handeling like fopen.
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 12-30-2007, 12:22 AM   #7 (permalink)
The Frequenter
 
Join Date: Dec 2007
Location: In my basement
Posts: 386
Thanks: 47
Aaron is on a distinguished road
Default

Yeah, I completely forgot to think about using file handles. The variables when you use those things are weird to me, though.

Like,

$connect = fopen(); (or whatever the syntax is) isn't a variable, but it connects to the file, SQL database, FTP, or whatever you are messing with.

(I am completely confused between these functions... So that is probably the wrong syntax).
Send a message via MSN to Aaron
Aaron is offline  
Reply With Quote
Old 12-30-2007, 12:32 AM   #8 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

Opening files using fopen/fclose can be a bit confusing at first. Here's a little example code which will hopefully clear things up a bit

PHP Code:
// This will open the file "myfile.txt" for reading (note the 2nd argument - "r" - you can use "w" for writing).  $file will then point to your newly opened file in memory.  Note: at this point, all that has happened is that the file has been opened for reading - nothing has been read yet

$file fopen('myfile.txt''r');

// The following code will read 2000 bytes from the file that we opened above (see how the $file pointer comes in to play here).

$contents fread($file2000);

// As a further example, if you wanted to read the entire file, you would take advantage of the filesize() function like so:

$contents fread($filefilesize('myfile.txt'));  // will read the entire file and store the contents in $contents

// When you are done with your file, you will want to close it.  To do this, we use the fclose() function:

fclose($file); 
fopen() can do other fancy things such as opening URLs for reading. For more info on reading/writing to files using fopen/fread/fwrite/fclose see:

PHP: fopen - Manual
PHP: fread - Manual
PHP: fwrite - Manual
PHP: fclose - Manual

All have excellent examples which you can work from.

Alan.
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
Old 12-30-2007, 09:43 AM   #9 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

The problem is quite simple. If you need to get the contents of a file unaltered, read the file contents and parse it. If you need to perform php operations in a file and just capture the result, use include/require (and they're _once forms).

Now, for the handles. Think of a handle as a door and the variable that references the handle as the key to the door. You need the key every time you use the door. Check out the analogy with the actual code:

PHP Code:
// get the 'door key'
$fh fopen('filename.txt.''rb');
// you need to use the key every time you do an operation that involves using 'the door'
// read the file, line by line
while( $line fgets$fh1024 ) )
{
echo 
$line;
}
// lock the 'door' & destroy the 'key' when you're done
fclose$fh ); 
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 06:14 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design