Including a .txt file

  • ace5p1d0r
  • Expert
  • Expert
  • User avatar
  • Joined: Apr 19, 2005
  • Posts: 629
  • Loc: UK
  • Status: Offline

Post May 30th, 2006, 1:14 am

I need to include a .txt file in a page on a CubeCart website.

Now PHP Include does not work =[ with this. Is there any other way you can think of?

And I have tried the support forum on cubecart, but they have offered no help.

Thanks
Webhost Reviews | Honda Civic Forum
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post May 30th, 2006, 1:14 am

  • onlyican.com
  • Mastermind
  • Mastermind
  • User avatar
  • Joined: Nov 20, 2005
  • Posts: 1582
  • Loc: Hants, UK
  • Status: Offline

Post May 30th, 2006, 3:37 am

You should be able to include txt files no problem

Run a little test, to check if it your hosting company

Have a text file
COntent
$testing12 = "This is a test";

Call the file, err, testing12.txt

Then create a new php page

Have this content
PHP Code: [ Download ] [ Select ]
 
<html>
 
<head>
 
<title>Testing</title>
 
</head>
 
<body>
 
<?
 
require("testing12.txt");
 
echo $testing12;
 
 
 
?>
 
</body>
 
</html>
 
 
 
 
  1.  
  2. <html>
  3.  
  4. <head>
  5.  
  6. <title>Testing</title>
  7.  
  8. </head>
  9.  
  10. <body>
  11.  
  12. <?
  13.  
  14. require("testing12.txt");
  15.  
  16. echo $testing12;
  17.  
  18.  
  19.  
  20. ?>
  21.  
  22. </body>
  23.  
  24. </html>
  25.  
  26.  
  27.  
  28.  


Now 1 of 3 things can happen

You will get a horrid error.
Your page will be blank
OR
You page will show "This is a test"
Heal your mind, and the body will follow
  • ace5p1d0r
  • Expert
  • Expert
  • User avatar
  • Joined: Apr 19, 2005
  • Posts: 629
  • Loc: UK
  • Status: Offline

Post May 30th, 2006, 3:42 am

I know for a fact that its not my host. Its the CubeCart software that disallows PHP Include.

Also - I want the text file to contain only the text I want shown.

Thanks for the help onlyican.com
Webhost Reviews | Honda Civic Forum
  • ace5p1d0r
  • Expert
  • Expert
  • User avatar
  • Joined: Apr 19, 2005
  • Posts: 629
  • Loc: UK
  • Status: Offline

Post May 31st, 2006, 12:33 am

Anybody help me here?
Webhost Reviews | Honda Civic Forum
  • Impel GD
  • Professor
  • Professor
  • No Avatar
  • Joined: Oct 26, 2004
  • Posts: 838
  • Loc: Cologne, Germany
  • Status: Offline

Post May 31st, 2006, 1:41 am

Erol shoping cart software doesn't allow SSI. A real pain.

Would be interested if anyone knows how to circumvent this kind of restriction.
Web and print design
  • ace5p1d0r
  • Expert
  • Expert
  • User avatar
  • Joined: Apr 19, 2005
  • Posts: 629
  • Loc: UK
  • Status: Offline

Post May 31st, 2006, 1:50 am

Exactly. Why is this? Probably some security vulnerability.

So there is no way to simply automatically include my poor little .txt file?
Webhost Reviews | Honda Civic Forum
  • ace5p1d0r
  • Expert
  • Expert
  • User avatar
  • Joined: Apr 19, 2005
  • Posts: 629
  • Loc: UK
  • Status: Offline

Post May 31st, 2006, 11:34 am

Perhaps a Javascript way? Surely. I have Googled but no success.
Webhost Reviews | Honda Civic Forum
  • knexor2
  • Proficient
  • Proficient
  • User avatar
  • Joined: May 27, 2006
  • Posts: 445
  • Loc: US
  • Status: Offline

Post May 31st, 2006, 2:50 pm

I take it this problem includes include_once(), require(), and require_once()?

Also, why does it have to be included? Is it a site-wide include, or a single page?

Unfortunately, I'm not familiar with CubeCart, so I know of no workarounds...
"People can school you, but you must educate yourself." ~ John Taylor Gatto
Tech Knack Blog
The purpose of these forums is not to get an answer, but to learn an answer.
  • ace5p1d0r
  • Expert
  • Expert
  • User avatar
  • Joined: Apr 19, 2005
  • Posts: 629
  • Loc: UK
  • Status: Offline

Post May 31st, 2006, 2:57 pm

Im not sure if those are disabled, but like Impel GD said SSI is disabled so I assume so.

It needs to be included because I want my client to be able to simply edit the .txt file, which will change wherever that .txt file is included.

Javascript possibly?
Webhost Reviews | Honda Civic Forum
  • knexor2
  • Proficient
  • Proficient
  • User avatar
  • Joined: May 27, 2006
  • Posts: 445
  • Loc: US
  • Status: Offline

Post May 31st, 2006, 3:16 pm

I doubt you'd be able to do it in JavaScript. JS is client-side, so you wouldn't be able to access server files.

However, one option is to open the text file using fopen(), read the contents into a variable, then print the variable where you need it included.

PHP Code: [ Download ] [ Select ]
 
<?php
 
// get contents of a file into a string
 
$filename = "/usr/local/something.txt";
 
$handle = fopen($filename, "r");
 
$contents = fread($handle, filesize($filename));
 
fclose($handle);
 
?>
 
 
  1.  
  2. <?php
  3.  
  4. // get contents of a file into a string
  5.  
  6. $filename = "/usr/local/something.txt";
  7.  
  8. $handle = fopen($filename, "r");
  9.  
  10. $contents = fread($handle, filesize($filename));
  11.  
  12. fclose($handle);
  13.  
  14. ?>
  15.  
  16.  
  • ace5p1d0r
  • Expert
  • Expert
  • User avatar
  • Joined: Apr 19, 2005
  • Posts: 629
  • Loc: UK
  • Status: Offline

Post May 31st, 2006, 3:24 pm

Now that looks exactly what I'm looking for.

However I dont understand the method fully. Could you explain exactly how it would work for me?

Thanks so much knexor2 :)
Webhost Reviews | Honda Civic Forum
  • knexor2
  • Proficient
  • Proficient
  • User avatar
  • Joined: May 27, 2006
  • Posts: 445
  • Loc: US
  • Status: Offline

Post May 31st, 2006, 6:17 pm

Hurried post, I'll try to write some detailed code tomorrow.

PHP Code: [ Download ] [ Select ]
 
<?php
 
// get contents of a file into a string
 
$filename = "/usr/local/something.txt";
 
$handle = fopen($filename, "r");
 
$contents = fread($handle, filesize($filename));
 
fclose($handle);
 
?>
 
 
  1.  
  2. <?php
  3.  
  4. // get contents of a file into a string
  5.  
  6. $filename = "/usr/local/something.txt";
  7.  
  8. $handle = fopen($filename, "r");
  9.  
  10. $contents = fread($handle, filesize($filename));
  11.  
  12. fclose($handle);
  13.  
  14. ?>
  15.  
  16.  



$filename = "/usr/local/something.txt";

This is the path to the file, obviously. You might use something like "$DOCUMENT_ROOT/users/userA/textfile.txt".

$handle = fopen($filename, "r");

This opens the file for reading, obviously.

$contents = fread($handle, filesize($filename));

this reads in a number of bytes specified by the second argument from the file pointed to by the first argument. This line reads in the entire file.

fclose($handle);
Obviously closes the file.

So, you want the user to be able to edit the file, like from a textarea?

In this case, you just print($contents) in the middle of a textarea form (make sure you include a submit button). Target a processing script with a method of "post". When the user is done editing and submits the form, the processing script opens the file for writing (this will erase the file upon opening it) using

fopen($filename, "w");

then use

fwrite($handle, $newcontents);

(I think that's the right function) to write the new contents into the file. Don't forget to close the file when done.
"People can school you, but you must educate yourself." ~ John Taylor Gatto
Tech Knack Blog
The purpose of these forums is not to get an answer, but to learn an answer.
  • ace5p1d0r
  • Expert
  • Expert
  • User avatar
  • Joined: Apr 19, 2005
  • Posts: 629
  • Loc: UK
  • Status: Offline

Post June 1st, 2006, 2:21 am

Yes but I have the writing to the .txt file sorted. I just need it displayed.

So anywhere I want the text file to automatically show, I place the code you just gave me?
Webhost Reviews | Honda Civic Forum
  • Impel GD
  • Professor
  • Professor
  • No Avatar
  • Joined: Oct 26, 2004
  • Posts: 838
  • Loc: Cologne, Germany
  • Status: Offline

Post June 1st, 2006, 2:42 am

I think you'd echo $contents to display the result.
Web and print design
  • Funny_Fuzz
  • Mastermind
  • Mastermind
  • User avatar
  • Joined: Jan 18, 2005
  • Posts: 1519
  • Loc: Australia
  • Status: Offline

Post June 1st, 2006, 2:49 am

Yes. Thats correct, you must Echo the contents of a variable.

PHP Code: [ Download ] [ Select ]
<?php  
 
// get contents of a file into a string  
 
$filename = "/usr/local/something.txt";  
 
$handle = file_get_contents($filename);  
 
echo $handle;
 
?>
  1. <?php  
  2.  
  3. // get contents of a file into a string  
  4.  
  5. $filename = "/usr/local/something.txt";  
  6.  
  7. $handle = file_get_contents($filename);  
  8.  
  9. echo $handle;
  10.  
  11. ?>


This method uses a command called "file_get_contents". The name explains it's use. The command loads the file's contents into the variable, hence allowing you to echo it onto the screen. Hope that helps you.
Good Luck! :thumbsup:
THE BEST THINGS IN LIFE ARE FREE...
JOIN THE MEDIASHARK COMMUNITY TODAY!
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post June 1st, 2006, 2:49 am

Post Information

  • Total Posts in this topic: 29 posts
  • Users browsing this forum: No registered users and 214 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© Unmelted Enterprises 1998-2009. Driven by phpBB © 2001-2009 phpBB Group.