Welcome to WebmasterWorld Guest from 54.162.239.134

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

How do I create a new page with PHP Code.

How do I create a new page with PHP Code.

     
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
3:15 pm on Jun 8, 2013 (gmt 0)

10+ Year Member



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
5:01 pm on Jun 8, 2013 (gmt 0)

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



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
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
9:20 pm on Jun 8, 2013 (gmt 0)

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



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;
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.
11:02 pm on Jun 8, 2013 (gmt 0)

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



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
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
11:44 pm on Jun 8, 2013 (gmt 0)

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



did you try the line I posted there?

$path = $_SERVER['DOCUMENT_ROOT'] . $post_path . $html_file_name;
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
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
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
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
 

Featured Threads

Hot Threads This Week

Hot Threads This Month