PHP and Files

Pulling Files and posting content with PHP

Page 1 of 1

10 Replies - 2480 Views - Last Post: 12 November 2010 - 05:19 AM Rate Topic: -----

#1 nikc121  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 133
  • Joined: 12-April 10

PHP and Files

Posted 11 November 2010 - 02:45 PM

Ive been playing around with this all day, I think my path is wrong, I keep getting [Parse error: parse error, unexpected ';' in /class/ct386b/##/www/cgi-bin/french5.php on line 73]
Here are how my files are set up and I am linking to my college unix system thru my signon ~ct386##
<secret> inside this is<exercise5>and in that is <content> this folder contains lesson5.txt and vocabulary.txt files. Then under my <cgi-bin> folder if have french5.php. The instructions are pretty simple, I understand what it wants me todo, just cant make it do it.(story of my life)
INSTRUCTIONS§ 1.Declare a variable named strLessonDescription that contains the contents of the lesson5.txt file. In the declaration, if the opening of the file fails, include the message "Error - lesson5.txt cannot be opened" (without quotes)
2.within the lessonDescription ID, create a PHP block of code that outputs the contents of the strLessonDescription variable.
Here is my PHP CODE
<?php

$StrLessDescription = ("http://nova.umuc.edu/~ct386b##/secret/exercise5/content/lesson5.txt");
$newfile = @fopen($StrLessDescription, "r") or exit ('Error - lesson5.txt cannot be opened');
$file_contents = @fgetcsv($newfile, filesize($StrLessDescription)) or exit ('Could not read file 

contents');
while ($i=0; $i < sizeof($file_contents); $i++);
{
	echo $file_contents[$i];
	echo "<br/>";
}
					
fclose($newfile);
?>
</p>


Is This A Good Question/Topic? 0
  • +

Replies To: PHP and Files

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: PHP and Files

Posted 11 November 2010 - 02:54 PM

Post the french5.php file in its entirety.
Was This Post Helpful? 0
  • +
  • -

#3 nikc121  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 133
  • Joined: 12-April 10

Re: PHP and Files

Posted 11 November 2010 - 02:59 PM

View PostJackOfAllTrades, on 11 November 2010 - 01:54 PM, said:

Post the french5.php file in its entirety.
Here ya go.
   
<title>French 101 - Week 5 Lesson Plan</title>
</head>
	<style type="text/css">
	*                  {margin: 0px; padding: 0px}
	#page              {width: 700px; border: 1px solid rgb(155, 102, 102)}
	
	#head              {width: 700px; color: white; font-size: 14px;
	                    font-family: Arial, Helvetica, sans-serif;
	                    background-color: rgb(155, 102, 102);
	                    padding: 10px 0px}
	#rightHead         {float: right; margin-right: 10px}
	#leftHead          {margin-left: 10px}
	
	#doc               {float: left;}
	#doc p    			{margin-left: 15px; margin-top: 5px; margin-bottom: 10px;}
	
	h1, h2             {font-family: Arial, Helvetica, sans-serif; 
	                    padding: 0px 10px}
	p                  {padding: 0px 10px; margin: 2px}
	h1                 {font-size: 18px; margin: 20px 0px 5px 0px}
	h2                 {font-size: 14px; font-weight: bold; margin: 5px}
	address            {clear: left; text-align: center; font-style: normal;
	                    background-color: rgb(155, 102, 102); color: white;
	                    font-family: Arial, Helvetica, sans-serif}
	#date				{font-size: 12px;}
	</style>


</head>

<body>
   <div id="page">
      <div id="head">
         <div id = "rightHead">
            <b>Prof. Eve Granger</b><br />
            Office: 810 Linton Hall<br />
            Hours: TR: 3:00-4:30
         </div>
         <div id = "leftHead">
            <b>French 101</b><br />
            MWF: 9:00-9:50<br />
            Rm. 402 Linton Hall
         </div>
      </div>

      <div id = "doc">
         <h1>Week 5 Lesson Plan</h1>
		
		 	<h2>Week Description</h2>
		 	<!-- Lesson Description -->
<p>
<?php

$StrLessDescription = ("http://nova.umuc.edu/~ct386b##/secret/exercise5/content/lesson5.txt");
$newfile = @fopen($StrLessDescription, "r") or exit ('Error - lesson5.txt cannot be opened');
$file_contents = @fgetcsv($newfile, filesize($StrLessDescription)) or exit ('Could not read file contents');
while ($i=0; $i < sizeof($file_contents); $i++);
{
	echo $file_contents[$i];
	echo "<br/>";
}
					
fclose($newfile);
?>
</p>

		
	        <h2>Week Vocabulary</h2>
			<p>
			<!-- Lesson Vocabulary -->

			</p>
      </div>
      <address>
         French-English Lesson 
		
      </address>
   </div>
   <p id="date">You are viewing this page on:  <!-- Date --></p>
<li>
<a href="http://nova.umuc.edu/~ct386b##/secret/index.html">Home</a>
</li>

</body>

</html>


Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: PHP and Files

Posted 11 November 2010 - 03:05 PM

while ($i=0; $i < sizeof($file_contents); $i++);

That's not a while loop syntax, it's a for loop syntax. And there's an unexpected ; at the end of it too.
Was This Post Helpful? 0
  • +
  • -

#5 nikc121  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 133
  • Joined: 12-April 10

Re: PHP and Files

Posted 11 November 2010 - 08:00 PM

View PostJackOfAllTrades, on 11 November 2010 - 02:05 PM, said:

while ($i=0; $i < sizeof($file_contents); $i++);

That's not a while loop syntax, it's a for loop syntax. And there's an unexpected ; at the end of it too.

Took out the extra ; but still not working. not sure how to change it wot a for loop, any suggestions?
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2999
  • View blog
  • Posts: 10,384
  • Joined: 08-August 08

Re: PHP and Files

Posted 11 November 2010 - 08:19 PM

Removing a semicolon isn't enough.

A while loop:
while($something == true)



A for loop:
for($i =0 ; $i < $N; $i++)

Note the difference.
Was This Post Helpful? 0
  • +
  • -

#7 nikc121  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 133
  • Joined: 12-April 10

Re: PHP and Files

Posted 11 November 2010 - 09:04 PM

ok, so my page shows the error message now, thanks for pointing out the while and for loop error. But I cant get it to display the array from the .txt file§ 1.Declare a variable named arrLessonVocabulary that contains an array of the lines of the vocabulary5.txt file. In the declaration, if the opening of the file fails, include the message "Error - vocabulary5.txt cannot be opened" (without quotes)
iv. 2.Within the lessonDescription ID, create a PHP block of code that outputs the contents of the strLessonDescription variable. 3. Within the lessonVocabulary ID, create a PHP block of code that outputs the contents of the arrLessonVocabulary array. To do this, loop through each element of the arrLessonVocabulary array outputting the element followed by a line break. Is the next line of code something like this, or am I completly off?
$filename = "c:\info.txt";
$fd = fopen ($filename, "r");
$contents = fread ($fd,filesize ($filename));

fclose ($fd);
$delimiter = "of";
$splitcontents = explode($delimiter, $contents);
$counter = "";
?>

Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2999
  • View blog
  • Posts: 10,384
  • Joined: 08-August 08

Re: PHP and Files

Posted 11 November 2010 - 09:20 PM

For starters, I'd hope that php doesn't have access to your c: drive!
If you're going to read in the entire file, you could use file_get_contents().
Was This Post Helpful? 0
  • +
  • -

#9 nikc121  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 133
  • Joined: 12-April 10

Re: PHP and Files

Posted 11 November 2010 - 09:28 PM

No they dont, for that line I will be using the same path as above. DOes file_get_content, will that print the text file on separate lines. Basicall the file I am getting is
black - noir(e)
blue - bleu(e)
green - vert(-e)
orange - orange
pink - rose
purple - violet(te)
red - rouge
white - blanc(-he)
yellow - jaune
ANd once I get the php code to pull the vocabulary list, I then have to add
iv. Within the lessonDescription ID, create a PHP block of code that outputs the contents of the strLessonDescription variable.
v. Within the lessonVocabulary ID, create a PHP block of code that outputs the contents of the arrLessonVocabulary array. To do this, loop through each element of the arrLessonVocabulary array outputting the element followed by a line break.
Was This Post Helpful? 0
  • +
  • -

#10 kinggeorge1987  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 130
  • Joined: 22-September 10

Re: PHP and Files

Posted 12 November 2010 - 12:49 AM

View Postnikc121, on 11 November 2010 - 08:28 PM, said:

No they dont, for that line I will be using the same path as above. DOes file_get_content, will that print the text file on separate lines. Basicall the file I am getting is
black - noir(e)
blue - bleu(e)
green - vert(-e)
orange - orange
pink - rose
purple - violet(te)
red - rouge
white - blanc(-he)
yellow - jaune
ANd once I get the php code to pull the vocabulary list, I then have to add
iv. Within the lessonDescription ID, create a PHP block of code that outputs the contents of the strLessonDescription variable.
v. Within the lessonVocabulary ID, create a PHP block of code that outputs the contents of the arrLessonVocabulary array. To do this, loop through each element of the arrLessonVocabulary array outputting the element followed by a line break.


Considering you appear to be a student, giving you simple solutions will do you no good. On the other hand, I am no expert with PHP and struggle the same as you, and sometimes you just need some insight.

Lastly, and most importantly. Have you looked at the PHP Manual concerning any of the questions that you have asked on this forum as of yet ? I have been directed to it countless times by some of our top rated members, and it has helped them I'm sure by allowing me to not ask them the obvious. Consult it. Try reading the File section of the manual. It can do you a lot of good.
---

Im not sure if this is a proper solution, but something as simple as using the file function alone could echo the entire text file with line breaks. If you were needing to echo this text file onto a web page, you would use:
<?php
$file= file("yourFile.txt");

	foreach ($file as $line){
	
		echo $line . "<br>" // <BR> As you should know, is the line break for HTML pages. If you were saving this file into .TXT, you would reverse this function to fwrite, and use the "\n" tag to make line breaks for each ilne;
	}
	
?>



If you needed to open any other files after that particular one, you would repeat this query. If you needed to echo single lines of data after the text file is read, you would just enter the
echo "What you want next";
after the closing curly bracket.
Was This Post Helpful? 0
  • +
  • -

#11 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: PHP and Files

Posted 12 November 2010 - 05:19 AM

Note, we also have a tutorial on working with local files.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1