Link to home
Start Free TrialLog in
Avatar of ST3VO
ST3VOFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Sample code needed

Hi all,

I need to do the following:

On have several webpages which I need to make copies of and save them at specified paths.

Some on the main page I would hardcode the names of the files that I want to make a copy of rename to a user specified name and save to a hardcoded location.

So...these are the steps:

1. Select the file to want to clone from a list. (hardcoded)
    Each of these files are assigned a specific location to copy to.
2. User gives it a name (all are .php ext)
3. Hit save and the page will be cloned and the new renamed copy of it will be saved to
   a specified location.

Hope this makes sense.

thanks

st3vo
Avatar of ollyatstithians
ollyatstithians
Flag of United Kingdom of Great Britain and Northern Ireland image

You can have a look at PHPs filesystem commands here: http://uk.php.net/manual/en/ref.filesystem.php

I would warn you, however, that can be very dangerous to allow user-controlled copying (and it sounds like you want them to make changes to) files. What are you trying to achieve by doing this? There is usually a better way that copying php files about.

Olly.
Avatar of ST3VO

ASKER

Can anyone help please?
Avatar of ST3VO

ASKER

It's just 1 person who is going to use it really so I will password protect the page.

We can call it a creating a template from a page.

So I want to add a new page to an area...I just need to make a copy of a certain page and rename it...then I can add new content.

So, the website has 4 types of different page structures. Instead of having to do it manually I need this person to be able to do it himself via a php file.

So, I have a directory called templates and want to have copies of the 4 different templates there plus the templatecreator.php ... then the user can select what type of page he wants to clone, give it a name and save it.

That's basically it.

 
Avatar of ST3VO

ASKER

OK the code attached works be I need it to be different.

For example:

I am going to have thumbnails with a radio button which you select and each one of this clones a different file /template and saves it do a designated path.

Then the user add a new name and submits...which will create and save the copy.

hope this helps
<?php
if (isset($_POST['submit']) AND $_POST['submit']=="submit") {
$file = (string)$_POST['files'];
$newdir = (string)$_POST['dirs'];
$newname = (string)$_POST['newfile'];
 
if (!copy($file, $newdir."/".$newname)) {
    echo "failed to copy $file...\n";
} else {
echo "File ".$file." has been moved at ".$newdir." and renamed into: ".$newname.".";
}
} else {
?>
<form name="form" action="" method="POST">
<br>
<?php
if ($path = getcwd())
    {
		echo 'Which file<select name="files">';
		foreach ( scandir ($path)  as $val)
        {
		if ( is_file ( $val ) )
            {
				echo '<option value="'.$val.'">'.$val.'</option>';
			}
		}
		echo '</select>';
	}
?>
<br>
<?php
if ($path = getcwd())
    {
		echo 'Save in which directory<select name="dirs">';
		foreach ( scandir ($path)  as $val)
        {
		if ( is_dir ( $val ) AND $val != '.' AND $val != '..')
            {
				echo '<option value="'.$val.'">'.$val.'</option>';
			}
		}
		echo '</select>';
	}
?>
<br>
New File name: <input name="newfile">
 
<input type="submit" name="submit" value="submit">
</form>
<?php
}
?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ollyatstithians
ollyatstithians
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ST3VO

ASKER

I'm trying out your code but I keep getting this error:

Warning: copy(template1) [function.copy]: failed to open stream: No such file or directory in C:\wamp\www\clone\_templates\add_new_page.php on line 5

Copy failed. Click here to return.

p.s: This is line 5 code: if(copy($_POST['copy'], "../{$_POST['copyto']}"))

thx
It is saying that it can't find the file to copy. Check that the files are in the same directory as the script.
You may also get problems if the webserver doesn't have write permissions to the destination directory.

Olly.
Avatar of ST3VO

ASKER

It's all in the same directory....as for the permission...I'm testing on a windows pc on localhost...so I don't think I should have a permissions issue as I am about to create files and save them....let me try changing the path.
Avatar of ST3VO

ASKER

Not a permision issue as this works:

<?php
$file = 'example.txt';
$newfile = 'example.txt.bak';

if (!copy($file, $newfile)) {
    echo "failed to copy $file...\n";
}
?>

As for the path...I have the all the files in the same directory " _templates" so the files are there...

Any ideas ?
It worked fine on my machine, but that is a Linux / Apache system.
The script I wrote actually drops the copy into the parent directory, so check that that is OK, or remove the "../" from the destination path.
I assume that on your Windows box relative physical directory paths are relative to the directory the script is in?

Olly.
Avatar of ST3VO

ASKER

Yes, I'm using WAMP on localserver...

This is my structure:

root
  |
  ----> _templates (all php files here)
                       |
                       ------> tmb (all thumbnails are here)


Avatar of ST3VO

ASKER

Ref: remove the "../" from the destination path. ...

Removed it and still getting the same error :o/
Have you named the template files "template1" or "template1.php". I didn't bother to put extensions on the files in my example.

Olly.
Avatar of ST3VO

ASKER

Just template1 ...didn't insert any ext...wonder if that's the problem...will try
Avatar of ST3VO

ASKER

Yep....that was the problem...needed the .php, so I've been able to test it now and it creates the file...Just 1 thing I need which I specified in the question.

Right now .... I have the filename and thumb ...which is great...Just need to assign a saving url to each template as they are all different (The saving url must be hardcode).

Hope you can help...nealy there now :o)

thx
Not sure I understand what you mean by "saving url".

Olly.
Avatar of ST3VO

ASKER

Yes.....Right now all created files are saved in the same specified location right?

I need to be able to save each in a different place.
like a different subdirectory?
Here you go...

Olly.
<?php
 // Check for posted data
if(isset($_POST['copy']) and isset($_POST['copyto']) and $_POST['copyto'] != '' and isset($_POST['directory'])) // Add a check for the directory parameter
{
	if(copy($_POST['copy'], "../{$_POST['directory']}/{$_POST['copyto']}.htm")) // Add the directory component to the destination path
	{
		echo "<p>Template copied OK. <a href=".">Click here to return</a>.</p>";
	}
	else
	{
		echo "<p>Copy failed. <a href=".">Click here to return</a>.</p>";
	}
}
else
{
	// Display form
 
	$templates = array(
		'temp1' => 'tmb/1.png',
		'temp2' => 'tmb/2.png',
		'temp3' => 'tmb/3.png',
		'temp4' => 'tmb/4.png'
	);
	echo "\n<form method=\"post\">\n<p>Enter the name for the new page <input name=\"copyto\" value=\"newpage\">.htm</p>\n<p>Select a directory <select name=\"directory\">"; // Add a select control
	foreach(scandir('..') as $filename) // Get a list of directory names for ../
	{
		if(is_dir('../'.$filename) and $filename !== '.' and $filename !== '..' and $filename !== 'templates') // this should exclude any directories you don't want selected (including the templates directory)
			echo "\n\t<option value=\"$filename\">$filename</option>";
	}
	echo "\n</select></p>\n<p>then click a template.</p>";
	foreach($templates as $template=>$thumbnail)
	{
		echo "\n\t<input type=\"image\" src=\"$thumbnail\" name=\"copy\" value=\"$template\">";
	}
	echo "\n</form>";
}
?>

Open in new window

Avatar of ST3VO

ASKER

Sorry about this....I need to specify (in code - without selecting it from a list) when each template is going to be saved.

For example (p.s: This might not be legal code at all but it's only to show what I mean)

$templates = array(
                'temp1' => 'tmb/1.png' => '../somedir1',
                'temp2' => 'tmb/2.png' => '../somedir/sub',
                'temp3' => 'tmb/3.png' => ../anotherdir',
                'temp4' => 'tmb/4.png'
        );

Hope this helps to explain.

thx again for your time!
Avatar of ST3VO

ASKER

when = where .... sorry about the spelling mistake :o)
I have added scandir() for the templates as well. Make sure you read the comments.

Olly.
<?php
// Check for posted data
if(isset($_POST['copy']) and isset($_POST['copyto']) and $_POST['copyto'] != '' and isset($_POST['directory'])) // Add a check for the directory parameter
{
	if(copy($_POST['copy'], "../{$_POST['directory']}/{$_POST['copyto']}.htm")) // Add the directory component to the destination path
	{
		echo "<p>Template copied OK. <a href=".">Click here to return</a>.</p>";
	}
	else
	{
		echo "<p>Copy failed. <a href=".">Click here to return</a>.</p>";
	}
}
else
{
	// Display form
	echo "\n<form method=\"post\">\n<p>Enter the name for the new page <input name=\"copyto\" value=\"newpage\">.htm</p>\n<p>Select a directory <select name=\"directory\">"; // Add a select control
	foreach(scandir('..') as $filename) // Get a list of directory names for ../
	{
		if(is_dir('../'.$filename) and $filename !== '.' and $filename !== '..' and $filename !== 'templates') // this should exclude any directories you don't want selected (including the templates directory)
			echo "\n\t<option value=\"$filename\">$filename</option>";
	}
	echo "\n</select></p>\n<p>then click a template.</p>";
	// Searches current directory for templates, ignoring this file. Thumbnails should be in a subdirectory called "tmb/", and should be named the same as the template with ".png" appended (eg. "template1.htm.png")
	foreach(scandir('.') as $template)
	{
		if(!is_dir($template) and $template !== 'index.php')
			echo "\n\t<input type=\"image\" src=\"tmb/$template.png\" name=\"copy\" value=\"$template\">";
	}
	echo "\n</form>";
}
?>

Open in new window

Oh. Right. Stand by...
Avatar of ST3VO

ASKER

thanks!!!
I had a bit of a mad idea... you name the template files as the path that you want to save them under, but delimited with "_"s that you then swap out to get the path. Code attached.
Alternatively, if you want to use an array, you will have to make it an array of arrays like this (or a multi-dimensional array as some might call it):

$templates = array(
  array('name'=>'temp1', 'thumbnail' => 'tmb/1.png', 'path' => '../somedir1',
  array('name'=>'temp2', 'thumbnail' => 'tmb/2.png', 'path' => '../somedir/sub',
  array('name'=>'temp3', 'thumbnail' => 'tmb/3.png', 'path' => '../anotherdir',
  array('name'=>'temp4', 'thumbnail' => 'tmb/4.png', 'path' => '../'
  );

Olly.
<?php
// Check for posted data
if(isset($_POST['copy']) and isset($_POST['copyto']) and $_POST['copyto'] != '')
{
	if(copy($_POST['copy'], '../'.dirname(str_replace('_', '/', $_POST['copy'])).'/'.$_POST['copyto'].'.htm')) // Add the directory component to the destination path
	{
		echo "<p>Template copied OK. <a href=".">Click here to return</a>.</p>";
	}
	else
	{
		echo "<p>Copy failed. <a href=".">Click here to return</a>.</p>";
	}
}
else
{
	// Display form
	echo "\n<form method=\"post\">\n<p>Enter the name for the new page <input name=\"copyto\" value=\"newpage\">.htm</p>\n<p>then click a template.</p>";
	// Searches current directory for templates, ignoring this file. Thumbnails should be in a subdirectory called "tmb/", and should be named the same as the template with ".png" appended (eg. "template1.htm.png")
	foreach(scandir('.') as $template)
	{
		if(!is_dir($template) and $template !== 'index.php')
			echo "\n\t<input type=\"image\" src=\"tmb/$template.png\" name=\"copy\" value=\"$template\">";
	}
	echo "\n</form>";
}
?>

Open in new window

Sorry, missed a closing bracket off that array.

Olly.
Make that 4 closing brackets.

Olly.
$templates = array(
  array('name'=>'temp1', 'thumbnail' => 'tmb/1.png', 'path' => '../somedir1'),
  array('name'=>'temp2', 'thumbnail' => 'tmb/2.png', 'path' => '../somedir/sub'),
  array('name'=>'temp3', 'thumbnail' => 'tmb/3.png', 'path' => '../anotherdir'),
  array('name'=>'temp4', 'thumbnail' => 'tmb/4.png', 'path' => '../')
  );

Open in new window

Avatar of ST3VO

ASKER

Getting errors again with the Directory Does not exist :o/

Avatar of ST3VO

ASKER

Hmmm...this doesn't seem right...

See edit source output from the browser

If there's an easier way to make it work without using an array it's fine...I don't mind about the coding method ... just need it to work :o)

Thx
<form method="post">
<p>Enter the name for the new page <input name="copyto" value="newpage">.htm</p>
<p>then click a template.</p>
	<input type="image" src="tmb/add_new_page.php.png" name="copy" value="add_new_page.php">
	<input type="image" src="tmb/add_new_page_tst.php.png" name="copy" value="add_new_page_tst.php">
	<input type="image" src="tmb/newpage.htm.png" name="copy" value="newpage.htm">
	<input type="image" src="tmb/template1.php.png" name="copy" value="template1.php">
	<input type="image" src="tmb/template2.php.png" name="copy" value="template2.php">
 
	<input type="image" src="tmb/template3.php.png" name="copy" value="template3.php">
	<input type="image" src="tmb/template4.php.png" name="copy" value="template4.php">
	<input type="image" src="tmb/template5.php.png" name="copy" value="template5.php">
</form>

Open in new window

Avatar of ST3VO

ASKER

Need to get this finished so I'm going to open another question in order to get a solution...I guess you are busy.

Thanks for all your help and time :o)
Avatar of ST3VO

ASKER

Thanks :o)
Sorry, I had to go home at some point. Thanks for the points. If you are still looking, here is the final code.

Olly.
<?php
$templates = array(
	'temp1' => array('thumbnail' => 'tmb/1.png', 'path' => '../somedir1'),
	'temp2' => array('thumbnail' => 'tmb/2.png', 'path' => '../somedir/sub'),
	'temp3' => array('thumbnail' => 'tmb/3.png', 'path' => '../anotherdir'),
	'temp4' => array('thumbnail' => 'tmb/4.png', 'path' => '../'));
// Check for posted data
if(isset($_POST['copy']) and isset($_POST['copyto']) and $_POST['copyto'] != '')
{
	if(copy($_POST['copy'], $templates[$_POST['copy']]['path'].'/'.$_POST['copyto'].'.htm')) // Add the directory component to the destination path
	{
		echo "<p>Template copied OK. <a href=".">Click here to return</a>.</p>";
	}
	else
	{
		echo "<p>Copy failed. <a href=".">Click here to return</a>.</p>";
	}
}
else
{
	// Display form
	echo "\n<form method=\"post\">\n<p>Enter the name for the new page <input name=\"copyto\" value=\"newpage\">.htm</p>\n<p>then click a template.</p>";
	foreach($templates as $templatename => $template)
	{
		echo "\n\t<input type=\"image\" src=\"{$template['thumbnail']}\" name=\"copy\" value=\"{$templatename}\">";
	}
	echo "\n</form>";
}
?>

Open in new window

Avatar of ST3VO

ASKER

That worked great! thanks a million. Can I ask you a last question pls...(I can open a new thread if you want) ... If I wanted more than 4 images....but 4 in each row...is it very hard to do?