Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Help with a PHP script
Old 09-09-2008, 01:42 PM Help with a PHP script
Junior Talker

Posts: 3
Name: John
Trades: 0
Hello,

I'm having an issue with a bit of code to work on dotster.com windows hosting.

I am trying to call data from a text file using PHP. Here is my original code which works on my own server:

Code:
            <?php
            // set file to read
               $file = 'jobs/jobs.txt' or die('Could not read file!');
            // open file 
            $fh = fopen($file, 'r') or die('Could not open file!'); 
            // read file contents 
            $data = fread($fh, filesize($file)) or die('Could not read file!');
            
            // close file 
            fclose($fh); 
              $rows = sizeof($data);
            
              for ($r = 1; $r <= $rows; $r = $r+2){
              echo '<a href="jobs/'.$data[$r].'">'.$data[$r-1].'</a><br />';
              }
            ?>
When I use the code above on the dotster server, the output is "Could not open file!" Their fopen() is turned on so I'm not sure what is going on. So I came up with a workaround, since I know that PHP includes are working on their server:

Code:
            <?php
            $data = array(str_replace("/n",",",include("jobs/jobs.txt"))); 
            echo $data; 
            echo 'this is the data <br>'; 
            
              $rows = sizeof($data); 
              
              for ($r = 1; $r <= $rows; $r = $r+2){ 
              echo '<a href="jobs/'.$data[$r].'">'.$data[$r-1].'</a><br />'; 
              } 
            ?>
The above code outputs the contents of the text file but it does not separate the content in rows nor does it properly link them, as it says in the code.

The output is supposed to look like (in html):

<a href="jobs/Sample job.pdf">Human Resources Position</a><br />
<a href="jobs/Sample job.pdf">Engineering Position</a><br />
<a href="jobs/Sample job.pdf">IT Position</a><br />

But instead it comes out as (with no break tags or link tags, it appears in one line):

Human Resources Position
Sample job.pdf
Engineering Position
Sample job.pdf
IT Position
Sample job.pdfArraythis is the data <br><a href="jobs/">1</a>

Any help is greatly appreciated.
Revo is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-09-2008, 03:27 PM Re: Help with a PHP script
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Ok, first thing first, welcome to the board.
Now, to the business.

Starting from the end, your "include" solution don't work, because when you do an include, the content of the file included is imported into the web page.
It's not a returned from the include() function.
The return from the include() in your case is "TRUE", so nothing to split by line breaks.

fopen() is really the way to go.
What I see, is that it's a path related problem.
include() look into the default path to search for the file if it's not found at the current location (in the filesystem).
Fopen() does not try to find it. It looks just where you tell it to find the file.

Second, I'm not 100% sure, but I believe you must give it an absolute location. Ie, the fopen looks from the system file path root, not from the web site root point. But I don't see nothin in the docs about this. Maybe it's an old limitation that have been lifted.
Third, if you are on windows, according to php documentation, you must use the double backslashe notation to specify the file location.
http://www.php.net/manual/en/function.fopen.php

For all those reason, I recommend you to use an absolute path and to check for the file existence before trying to open it.
PHP Code:
$file $_SERVER['DOCUMENT_ROOT'].'/jobs/jobs.txt'
if(!
file_exists($file)){ 
  die(
"file $file was not found"); 

if(!
is_readable($file)){ 
  die(
"file $file is not readable"); 

$fh=fopen($file,"r"); 
$prevLine=""
while(!
feof($fh)){ 
  
$line=fgets($fh); 
  if(
$prevLine!=""){ 
    echo 
'<a href="/jobs/'.$line.'">'.$prevLine.'</a><br />'
  } 
  
$prevLine=$line

That way, you will test that the file exists, and that you can read it.
And rather than putting it into memory and then parsing the array (which is highly inneficient, and can led to php error if the file is too big) you read it line by line and echo the content without using more memory than 2 lines of the file.

By the way, this script imply that your server is running linux and that the jobs directory is located into the root directory of the site.
If you are on windows, replace "/jobs/jobs.txt" with "\\jobs\\jobs.txt" and adjust the path to your file location.
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 09-09-2008 at 03:29 PM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 09-09-2008, 04:01 PM Re: Help with a PHP script
Junior Talker

Posts: 3
Name: John
Trades: 0
Thanks for the welcome!

And thank you for the answer! It was a simple path issue! Yeah, it is on a Windows server. Thanks again!
Revo is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Help with a PHP script
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.37431 seconds with 12 queries