Jump to content

a question concering file_put_contents


smartalco

Recommended Posts

A little background first: i am making a photo album page for a local university student group, and im trying to make this easy for them, and so far i have made it so they could stick a folder with the name of the event the pictures are from in the photo album folder, then run a php script, and it will make the appropriate pages for the photos (12 per page)I'm trying to use file_put_contents() to create the .php files for each page, but i have a few questions concerning the use of it.On the php documentation, the later two parameters are flags, and context. Which of these (if either) should i worry about, and what do i need to use for each one.The alternative option is to just use fopen(), fwrite(), and fclose(), if that would be a better option then just tell me so.ThanksNote: To avoid confusion, this is unrelated to my previous topic.

Link to comment
Share on other sites

I'm not very familiar with those arguments either, but I can tell you fopen(), fwrite(), and fclose() are certanly not better alternatives (unless you must keep compatability with PHP4). What those arguments are used for is if the file needs to be handled in a special way. That is, if you need to do something other then simply appending the contents of the file (if it exists I mean), or if the content itself needs to be specially formatted. I don't know the details, but that are the basics. So, before you need those powers, you don't need to worry about those arguments.

Link to comment
Share on other sites

Just curious: why have script that makes other php-scripts to display the images (which you use when creating the php-scripts.. sort of..)? Why not just make a php-script that display the pictures in a dir. when called (no need for the "man-in-the-middle" nor a call to a file everytime you upload new pictures!?

Link to comment
Share on other sites

Sorry for the double post, but I have one more quick question about this. When specifying the file path, do i start it with a '/', or just use the name of the file?('/whatever.php' or just 'whatever.php')
It depends... the first saves the file at the document root (the base location of all files on the server), and the second in the folder the PHP file is in. And unless the PHP file is actually in the document root, the two resulting locations will be different.
Link to comment
Share on other sites

ok, im still having problems with thisi found out that the server this site is running on is only up to php 4, so file_put_contents isn't supported, but i copied a substitute from the doc page on php.net (here)my code is as following

function file_put_contents($n,$d) {  $f=@fopen($n,"w");  if (!$f) {	return false;  } else {	fwrite($f,$d);	fclose($f);	return true;  }}/*other unrelated code*/$url = $event . "_photos" . $i . ".php";$code = '<?php \n		session_start(); \n		$_SESSION["page_num"] = ' . $i . '; \n		$_SESSION["event"] = ' . $event . '; \n		include("photo_template.php");  \n		   ?>'		file_put_contents($url, $code);

but i get this error when trying to load the page (line 106 is the last line in the code i posted)"Parse error: parse error in /usr2/web/alumni/T4L/index_photos.php on line 106"any help?

Link to comment
Share on other sites

First, you left out the semicolon on the line before, but that's not the problem. The issue is that PHP sees the ?> tag and stops. When you have PHP tags in a string, you need to split them up so PHP does not react to them.

$code = '<' . '?php \n		session_start(); \n		$_SESSION["page_num"] = ' . $i . '; \n		$_SESSION["event"] = ' . $event . '; \n		include("photo_template.php");  \n		   ?' . '>';

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...