homepage Welcome to WebmasterWorld Guest from 54.197.15.196
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
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 create a new page with PHP Code.
How do I create a new page with PHP Code.
Archiie



 
Msg#: 4582308 posted 12:28 pm on Jun 8, 2013 (gmt 0)

I don't understand why it doesn't create a new page with this code. I'd be happy for any help! Since I'm using,

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

Which means that the new file name will be HopeForIt.html.




<?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>




Thanks,
Archiie

 

adb64

10+ Year Member



 
Msg#: 4582308 posted 3:15 pm on Jun 8, 2013 (gmt 0)

Do you get any errors? Are the access rights of the directory in which you want to write the new file set correctly?

Regards, Arjan

jatar_k

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



 
Msg#: 4582308 posted 5:01 pm on Jun 8, 2013 (gmt 0)

Welcome to WebmasterWorld Archiie,

it could just be a permissions issue, you should get a warning if the file pointer is not created but you can test to see if it was created like so

$fp = fopen($post_path.$html_file_name, "w");
if ($fp == false) {
echo "could not create the file pointer";
} else {
echo "look it worked";
}

after that have you checked to see if anything got created? maybe the filename was funny or something else went wrong

you should echo then contents of $new_post_file to see if maybe something is wrong in there

scanning the code it looks ok but I only scanned it

Archiie



 
Msg#: 4582308 posted 7:44 pm on Jun 8, 2013 (gmt 0)

Do you get any errors? Are the access rights of the directory in which you want to write the new file set correctly?

Regards, Arjan


The file pointer isn't correct.

"could not create the file pointer"


//path to the directory at the server, where you store the "page.html" file.
$tpl_path = "page/sidor"; <--- page.html is at (http://sajtfix.se/page/page.html).

//path to the directory where you will store the new page.
$post_path = "page/"; and this one is at (http://sajtfix.se/page/sidor/...)

So I've tried to switch them to:

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

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

- Still getting an error.

Thanks,
Archiie

jatar_k

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



 
Msg#: 4582308 posted 9:20 pm on Jun 8, 2013 (gmt 0)

does the user running php have permission to create files in the directory?

have you tried outputting the path you are trying to create to be sure it is exactly right?

echo '<br>fullpath is: ', $post_path.$html_file_name;

Archiie



 
Msg#: 4582308 posted 10:52 pm on Jun 8, 2013 (gmt 0)

could not create the file pointer
fullpath is: /page/sidor/HopeForIt.html
firstpath is: /page/

This is what comes, from the following;

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

I still don't understand the problem.

jatar_k

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



 
Msg#: 4582308 posted 11:02 pm on Jun 8, 2013 (gmt 0)

it looks like you are trying to create a file on a path of
/page/sidor/HopeForIt.html

i doubt this is correct as there probably is no 'page' directory at the root of your server, I am guessing you need something to reference you site root instead, maybe like

$path = $_SERVER['DOCUMENT_ROOT'] . $post_path . $html_file_name;

I am not 100% sure that the slashes will be correct but if you echo that var you will probably get something that is starting to look a little more sane

Archiie



 
Msg#: 4582308 posted 11:36 pm on Jun 8, 2013 (gmt 0)

Do you have any solution for that?

What i'm trying to do;

I upload this post;
-> On submit, I got the information:

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

I want to make it a new page, and print those stuff in the new Page I create. Do you have any idea of a way I could do it.

Thanks,
Archiie

jatar_k

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



 
Msg#: 4582308 posted 11:44 pm on Jun 8, 2013 (gmt 0)

did you try the line I posted there?

$path = $_SERVER['DOCUMENT_ROOT'] . $post_path . $html_file_name;

Archiie



 
Msg#: 4582308 posted 10:14 am on Jun 9, 2013 (gmt 0)

Yes I did.

- Path: /customers/b/2/d/sajtfix.se/httpd.www/page/sidor/
...
is what I got when I did;
echo "<br/>Path: ".$path;

Where am I suppose to use the variable $path, which is still unclear for me.

Thanks,
Archiie

Archiie



 
Msg#: 4582308 posted 10:15 am on Jun 9, 2013 (gmt 0)

Yes I did.

- Path: /customers/b/2/d/sajtfix.se/httpd.www/page/sidor/
...
is what I got when I did;
echo "<br/>Path: ".$path;

Where am I suppose to use the variable $path, which is still unclear for me.

Thanks,
Archiie

Archiie



 
Msg#: 4582308 posted 10:16 am on Jun 9, 2013 (gmt 0)

Yes I did.

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

This is what I get;

- Path: /customers/b/2/d/sajtfix.se/httpd.www/page/sidor/


Where am I suppose to use the variable $path, which is still unclear for me.

Shouldn't I use the variable $path somewhere or is it just okay with the code:
$path = $_SERVER['DOCUMENT_ROOT'] . $post_path . $html_file_name;

Thanks,
Archiie

Archiie



 
Msg#: 4582308 posted 11:00 am on Jun 9, 2013 (gmt 0)

Arjan,

I got it to work, but it doesn't fetch any data from "page.html".

Any clue why?

Thanks,
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.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved