Welcome to WebmasterWorld Guest from 107.20.104.110

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

How do I write data to the page I create?

php, post, page, data

     
8:49 am on Jun 15, 2013 (gmt 0)

New User

joined:June 8, 2013
posts: 17
votes: 0


<?php
$tpl_file = "page.html";

//path to the directory at the server, where you store the "page.html" file.
$tpl_path = "page/sidor";

//path to the directory where you will store the new page.
$post_path = "page/";

$data['title'] = "HopeForIt";
$data['imgSrc'] = "hello.jpg";
$data['uploadedBy'] = "Archiie";
$data['category'] = "Hope";
$data['bildId'] = "BildId3";

$placeholders = array("{title}","{imgSrc}", "{uploadedBy}", "{category}", "{bildId}");

$tpl = file_get_contents($tpl_path.$tpl_file);

$new_post_file = str_replace($placeholders, $data, $tpl);

$html_file_name = $data['title'].".html";



?>

<html>
<head>

<title>Spara filen p&aring; en sida.</title>

</head>

<body>

<?php
echo "1: ".$data['title']."<br>, 2: ".$data['imgSrc']."<br/>, 3: ".$data['uploadedBy']."<br/>, 4: ".$data['category']."<br/>, 5: ".$data['bildId'];


$fp = fopen($post_path.$html_file_name, "w");
fwrite($fp, $new_post_file);
fclose($fp);

?>

</body>
</html>
2:20 pm on June 15, 2013 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts: 15755
votes: 0


and what result are you getting, or not getting?
11:16 pm on June 15, 2013 (gmt 0)

New User

joined:June 8, 2013
posts: 17
votes: 0


It makes a new page, but the problem is that the new file is empty. How do I write the data into that file?
12:53 am on June 16, 2013 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10563
votes: 15


welcome to WebmasterWorld, Archiie!


add some error checking to the fopen/fwrite/fclose code.
10:02 am on June 16, 2013 (gmt 0)

New User

joined:June 8, 2013
posts: 17
votes: 0


if ($fp == false) {
echo "<br>could not create the file pointer.";
} else {
echo "<br>look it worked";
}

This is what I did to try it out,
ERROR MESSAGE: "could not create the file pointer."
so fp is equal to false.

and I did;

f(fwrite == true){
echo "<br>fWrite is equal to TRUE. ";
}
else if (fwrite == false){
echo "<br>fWrite is equal to false.";
}
if(fopen == true){
echo "<br>fOpen is equal to TRUE.";
}
if(fclose == true){
echo "<br>fClose is equal to TRUE.<br>";
}

the results:

fWrite is equal to TRUE.
fOpen is equal to TRUE.
fClose is equal to TRUE.

Got any idea mate? I think there's a problem to send data to the page i'm about to create.
10:16 am on June 16, 2013 (gmt 0)

New User

joined:June 8, 2013
posts: 17
votes: 0


$fp = fwrite($_SERVER['DOCUMENT_ROOT'].$post_path.$html_file_name, "w");
if ($fp == false) {
echo "<br>could not create the file pointer.";
} else {
echo "<br>look it worked";
}

echo '<br>fullpath is: ', $_SERVER['DOCUMENT_ROOT'] . $post_path . $html_file_name;

echo "<br/>Path: ".$path;

echo "<br/>Tpl Path: ".$tpl_path;
2:51 pm on June 16, 2013 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts: 15755
votes: 0


if you get 'could not create the file pointer' on the fopen then it can't create the file, so that is the first thing you need to figure out.

the main reason, usually, for not creating the file is the user running php not having permissions to write to the directory. Some hosts may not allow this but usually they will as long as you are in your own site's directories.

The more common issue with creating files is people aren't using the path they think they are, so...

you have to make sure that the path you pass to the fopen function is the correct path to where you want to create the file on the server, this has little or no bearing on the web path that you would see in the browser. Most often you to find the correct path by using ssh to log in to your server, sometimes you can also get this path from an ftp/scp client as well.

if/when you get a positive response from fopen then you can move onto messing with the fwrite, most of the time as long as you are passing a good pointer to fwrite then it will have no issues writing.

also, those tests you ran on fopen/fwrite/fclose aren't quite what phranque meant, more like the one in your last post where you have the function execute and test the return values from the function.

even for me to see the output of the path variables, I can only hazard a guess on where your files might be located based on what I see normally. You would need to compare the output from your script with where the path physically points to by looking on your server.
3:20 pm on June 16, 2013 (gmt 0)

New User

joined:June 8, 2013
posts: 17
votes: 0


I went into my FTP, and I changed so all files are allowed to write. So I'll run a test now. However it manages to create another page, but it doesn't manage to send the information i have on "Page.html".

The path is correct, I've tried to make small changes but I still can't manage to fix it. Well thanks for the help @jatar_k
5:10 pm on June 16, 2013 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts: 15755
votes: 0


mmm, I wonder if that str_replace even works?

you could also try writing the string in the same way you echo it instead of having the odd str_replace

$new_post_file = "1: ".$data['title']."<br>, 2: ".$data['imgSrc']."<br/>, 3: ".$data['uploadedBy']."<br/>, 4: ".$data['category']."<br/>, 5: ".$data['bildId'];

then do your echo this way

echo $new_post_file;


then you will actually be looking at what you are trying to write instead of writing one thing and echoing a different one.

if the strings
2:12 pm on June 17, 2013 (gmt 0)

New User

joined:June 8, 2013
posts: 17
votes: 0


It worked. Thanks jatar!
5:15 pm on June 17, 2013 (gmt 0)

New User

joined:June 8, 2013
posts: 17
votes: 0


I have another problem,

This is the problem:

I get this url:
- mysite.com/page/sidor/Every%20Mans%20Dream%20Body%20[Gallery].php

My input:
- mysite.com/page/sidor/Every Mans Dream Body [Gallery].php

I'd rather have:
- mysite.com/page/sidor/Every-Mans-Dream-Body-[Gallery].php

Thanks,
Archiie

[edited by: jatar_k at 8:28 pm (utc) on Jun 17, 2013]
[edit reason] making links readable [/edit]

8:35 pm on June 17, 2013 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts: 15755
votes: 0


easy enough to pass the string to str_replace and swap the spaces for hyphens

something like

$myString = 'Every Mans Dream Body Gallery';
$myString = str_replace(' ', '-', $myString);
echo $myString;

I also wouldn't put [] in the url
2:24 pm on June 18, 2013 (gmt 0)

New User

joined:June 8, 2013
posts: 17
votes: 0


I just want to thank you guys so much for the help!
2:31 pm on June 18, 2013 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts: 15755
votes: 0


glad it's all working Archiie