|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Load text files as variables
Can anyone help me to load a text file, and turn it into a variable? Any help appreciated, thanks in advance.
|
|
#2
|
||||
|
||||
|
Working With Text Files in PHP by Mitchell Harper
|
|
#3
|
|||
|
|||
|
Thanks. But using that code I ended up getting a few errors. I think it partially worked, but there must be something wrong that I've done. Would appreciate it if someone could have a look at the errors that come up on http://dump.hollosite.com/blog/postsave.php
and could check the code pasted here: <?php echo header( 'location:index.php' ); $fp = fopen("admin/gmttime.txt", "r"); $gmttime = fread($fp, filesize($fp)); $fp = fopen("admin/yourname.txt", "r"); $yourname = fread($fp, filesize($fp)); $fp = fopen("admin/timezone.txt", "r"); $timezone = fread($fp, filesize($fp)); $hourdiff = $gmttime + 10; $timeadjust = ($hourdiff * 60 * 60); $time = date("g:i a",time() + $timeadjust); $title = date("GidMy",time() + $timeadjust); $Date = '<div id="pagetitle">'.date("l, d F Y",time() + $timeadjust).'</div>'; $From = "Posted by ".$yourname." at ".$time." ".$timezone."."; $Post = $_POST["post"]; $AddComment = '<a href="index.php?post='.$title.'">Add a comment!</a>'; $fn = fopen($title.".txt", "x") or die("Please don't post twice in the same minute."); fwrite($fn, "\r\n" . '<br />'); fwrite($fn, "\r\n" . '<br />'); fwrite($fn, "\r\n" . $Date); fwrite($fn, "\r\n" . '<br />'); fwrite($fn, "\r\n" . $Post); fwrite($fn, "\r\n" . '<br />'); fwrite($fn, "\r\n" . $From); fwrite($fn, "\r\n" . '<br />'); fwrite($fn, "\r\n" . $AddComment); fclose($fn); $fp = fopen("archive.txt", "a") or die("Error creating/writing to new file."); fwrite($fp, "\r\n" . '<?php Include("'.$title.'.txt"); ?>'); fclose($fp); $fp = fopen("admin/archive.txt", "a") or die("Error creating/writing to new file."); fwrite($fp, "\r\n" . '<a href="index.php?post='.$title.'">Edit '.$title.' post</a>'); fclose($fp); ?> Thanks again. |
|
#4
|
||||
|
||||
|
What exactly are the errors you get?
|
|
#5
|
|||
|
|||
|
Sorry have fixed that particular problem now. I am now wondering how to use stripslashes correctly. When I try to use them, Nothing happens, its as if they're not even part of the code. Thanks for the help.
|
|
#6
|
||||
|
||||
|
Stripslashes simply takes the slash away when its used to escape a character.
From the documentation PHP Code:
How are you trying to use it? |
|
#7
|
|||
|
|||
|
Probably in a way in which it won't work :P
$fn = fopen("../archive/".$title.".txt", "x") or die("Please don't post twice in the same minute."); stripslashes( fwrite($fn, "\r\n" . '<br />')); stripslashes( fwrite($fn, "\r\n" . '<br />')); stripslashes( fwrite($fn, "\r\n" . $Date)); stripslashes( fwrite($fn, "\r\n" . '<br />')); stripslashes( fwrite($fn, "\r\n" . $Post)); stripslashes( fwrite($fn, "\r\n" . '<br />')); stripslashes( fwrite($fn, "\r\n" . $From)); stripslashes( fwrite($fn, "\r\n" . '<br />')); stripslashes( fwrite($fn, "\r\n" . $AddComment)); fclose($fn); |
|
#8
|
||||
|
||||
|
You're trying to write to the comment to a file?
You might want to try: PHP Code:
I question your message "Please don't post twice in the same minute." There might be other reasons the file doesn't open... |
|
#9
|
|||
|
|||
|
That works great.
I hate to bother you again, though. I want to be able to read a file line by line, making each line part of an array, then use array_reverse to flip it around. I'm getting an error saying: Warning: main(Array): failed to open stream: No such file or directory in /home/www/dump.hollosite.com/blog/files/main.txt on line 25 Warning: main(): Failed opening 'Array' for inclusion (include_path='.:/usr/local/lib/php') in /home/www/dump.hollosite.com/blog/files/main.txt on line 25 When using this code: $archive= array ( file("files/archive.txt")); $main = array_reverse($archive); Include ("$main"); I've obviously done something wrong, but can't work it out. Thanks for all the help. |
|
#10
|
|||
|
|||
|
Have now solved the problem, thanks for all the help.
|
|
#11
|
||||
|
||||
|
How did you fix your problem?
|
|
#12
|
|||
|
|||
|
Figured out I was trying to Include an array, rather than Print ing it :P
|
|
#13
|
|||
|
|||
|
I'm having a bit of a problem with sessions. I've neaver used them before, and I'm probably missing something out which needs to be there. Could someone please mention the essential functions I need for a session? Thanks in advance.
|
|
#14
|
||||
|
||||
|
This would take the conversation out of scope of the original question, a new thread might have been a better idea
![]() But anyways... make sure session_start() is [one of] the first functions called on your page. then store stuff in the $_SESSION variable... eg: $_SESSION['username']='Bill'; session_destroy() kills the session too the documentation is a good source of help. Not to mention many google'ing tutorials, and even devarticles articles. |
|
#15
|
|||
|
|||
|
Yeh, i thought considering it was still part of the same project, that i would post here, instead of clogging up the forums with unnecessary threads :P
I have it working now, thankyou very much for your help. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > PHP Development > Load text files as variables |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|