Welcome to WebmasterWorld Guest from 54.196.212.62

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

How do I write data to the page I create?

php, post, page, data

     

Archiie

8:49 am on Jun 15, 2013 (gmt 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>

jatar_k

2:20 pm on Jun 15, 2013 (gmt 0)

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



and what result are you getting, or not getting?

Archiie

11:16 pm on Jun 15, 2013 (gmt 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?

phranque

12:53 am on Jun 16, 2013 (gmt 0)

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



welcome to WebmasterWorld, Archiie!


add some error checking to the fopen/fwrite/fclose code.

Archiie

10:02 am on Jun 16, 2013 (gmt 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.

Archiie

10:16 am on Jun 16, 2013 (gmt 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;

jatar_k

2:51 pm on Jun 16, 2013 (gmt 0)

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



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.

Archiie

3:20 pm on Jun 16, 2013 (gmt 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

jatar_k

5:10 pm on Jun 16, 2013 (gmt 0)

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



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

Archiie

2:12 pm on Jun 17, 2013 (gmt 0)



It worked. Thanks jatar!

Archiie

5:15 pm on Jun 17, 2013 (gmt 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]

jatar_k

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

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



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

Archiie

2:24 pm on Jun 18, 2013 (gmt 0)



I just want to thank you guys so much for the help!

jatar_k

2:31 pm on Jun 18, 2013 (gmt 0)

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



glad it's all working Archiie
 

Featured Threads

Hot Threads This Week

Hot Threads This Month