homepage Welcome to WebmasterWorld Guest from 107.22.45.61
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
How do I write data to the page I create?
php, post, page, data
Archiie




msg:4584432
 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




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

and what result are you getting, or not getting?

Archiie




msg:4584523
 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




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

welcome to WebmasterWorld, Archiie!


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

Archiie




msg:4584588
 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




msg:4584591
 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




msg:4584616
 2:51 pm on Jun 16, 2013 (gmt 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.

Archiie




msg:4584621
 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




msg:4584643
 5:10 pm on Jun 16, 2013 (gmt 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

Archiie




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

It worked. Thanks jatar!

Archiie




msg:4585001
 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




msg:4585068
 8:35 pm on Jun 17, 2013 (gmt 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

Archiie




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

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

jatar_k




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

glad it's all working Archiie

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved