making a newspost thingie

Discussion about the creation of websites in general, HTML, PHP, Java.

making a newspost thingie

Postby evtorb on Fri May 27, 2005 6:53 am

Hello,

I am the Web Over Lord Master for a website. I was told to have it so someone can post a news story to the homepage. I have the code to read the post from the hard disk. That was easy ( almost like C). The problem is reading the data in the HTML( or java scrip) form elements. Then turning them into PHP varibles to write to disk. Thanks in advance
evtorb
Newbie
Newbie
 
Posts: 5
Joined: Fri May 27, 2005 6:18 am

Postby Tim on Fri May 27, 2005 11:20 pm

By write to disk I'm assuming you want to use flatfiles (text files) not a db so I'll do it that way.

html file
Code: Select all
<form action=somefile.php method=post>
Subject: <input type=text name=subject>
<textarea name=content></textarea>
<input type=submit>
</form>


somefile.php
Code: Select all
<?php
$subject = $_POST['subject']; # Technically in most cases you don't need to define
$content = $_POST['content']; # these but it usually better to do so just in case.
// We're going to put all news articles in the dir "/news/"
// they will be named article[i]x[/i] assuming you go by
// this method don't put any other files in the news dir
// or you will get unexpected results which probably won't
// be a good thing.
$dir = "news";
$num = 0;
if ($w = opendir($dir)) {
while ($con = readdir($w)) {
$num++;
}
}

$f = fopen("news/article".$num.".txt", "x");
$out = $subject."\n".$content;
$write = fwrite($f, $out);
if (!$write) {
die("Script error");
} else {
echo "News added Successfully";
}
fclose($f);
?>

Bear in mind, I havn't tested the above in any way and it probably should have more check on the dirs and files its writing to (permissions, etc.) but if it doesn't work it shouldn't take too much playing with to make it work. To make it display them just read the news dir and for each file return it and replace the \n with <br> or something, I can't be bothered working it out now.

Tim.
User avatar
Tim
root
root
 
Posts: 736
Joined: Thu Mar 10, 2005 6:31 am
Location: Australia

Postby funman on Sat May 28, 2005 10:55 am

I belive the file you are writing to requires a CHMOD to 755 for it to function if my memory serves me correct, other then that it looked fine
funman
Contributer
Contributer
 
Posts: 61
Joined: Sat May 28, 2005 10:20 am
Location: Stavanger, Norway

Postby Tim on Sat May 28, 2005 12:28 pm

Unless of course its on a windows server which doesn't use those specific file perms, if its a server which has those specific file/dir perms then it might require some more fiddling.
Also, I just thought of a bug with this script, when you create a dir in some environments the OS creates some hidden files which the scripts will fight with so I recommend that, if possible, you remove all hidden files and folders before playing with a display script.
User avatar
Tim
root
root
 
Posts: 736
Joined: Thu Mar 10, 2005 6:31 am
Location: Australia

Postby funman on Sat May 28, 2005 2:45 pm

Another way of doing it would be writing all to one file and relying on lines and each newspost gets it's own, that way you can read from the en of the file to get latest news first, this will stop any unwanted dir problems.
funman
Contributer
Contributer
 
Posts: 61
Joined: Sat May 28, 2005 10:20 am
Location: Stavanger, Norway

Postby evtorb on Sun May 29, 2005 9:44 am

In order to et it to run it required a bit of hacking. I got 4 errors. But it was fixed. Very Happy. I praise thy Tim. Thanks for the code.
evtorb
Newbie
Newbie
 
Posts: 5
Joined: Fri May 27, 2005 6:18 am


Return to General Webmastering

Who is online

Users browsing this forum: No registered users and 1 guest

cron