Solved

Sample code needed

Posted on 2009-05-19
33
230 Views
Last Modified: 2012-05-07
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
0
Comment
Question by:ST3VO
  • 20
  • 13
33 Comments
 
LVL 10

Expert Comment

by:ollyatstithians
ID: 24420435
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.
0
 

Author Comment

by:ST3VO
ID: 24420436
Can anyone help please?
0
 

Author Comment

by:ST3VO
ID: 24420581
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.

 
0
 

Author Comment

by:ST3VO
ID: 24420713
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

0
 
LVL 10

Accepted Solution

by:
ollyatstithians earned 500 total points
ID: 24421010
Here is what I would do (I haven't read your code yet) just to keep it moving.

Olly.
<?php

// Check for posted data

if(isset($_POST['copy']) and isset($_POST['copyto']) and $_POST['copyto'] != '')

{

	if(copy($_POST['copy'], "../{$_POST['copyto']}"))

	{

		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

{

	// Get the list of templates (you could generate this from a listing of the directory)

	$templates = array(

		'temp1' => 'tmb/temp1.png',

		'temp2' => 'tmb/temp2.png',

		'temp3' => 'tmb/temp3.png',

		'temp4' => 'tmb/temp4.png'

	);

	// 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 $template=>$thumbnail)

	{

		echo "\n\t<input type=\"image\" src=\"$thumbnail\" name=\"copy\" value=\"$template\">";

	}

	echo "\n</form>";

}

?>

Open in new window

0
 

Author Comment

by:ST3VO
ID: 24421271
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
0
 
LVL 10

Expert Comment

by:ollyatstithians
ID: 24421340
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.
0
 

Author Comment

by:ST3VO
ID: 24421673
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.
0
 

Author Comment

by:ST3VO
ID: 24421847
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 ?
0
 
LVL 10

Expert Comment

by:ollyatstithians
ID: 24422102
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.
0
 

Author Comment

by:ST3VO
ID: 24422173
Yes, I'm using WAMP on localserver...

This is my structure:

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


0
 

Author Comment

by:ST3VO
ID: 24422248
Ref: remove the "../" from the destination path. ...

Removed it and still getting the same error :o/
0
 
LVL 10

Expert Comment

by:ollyatstithians
ID: 24422326
Have you named the template files "template1" or "template1.php". I didn't bother to put extensions on the files in my example.

Olly.
0
 

Author Comment

by:ST3VO
ID: 24422425
Just template1 ...didn't insert any ext...wonder if that's the problem...will try
0
 

Author Comment

by:ST3VO
ID: 24422501
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
0
 
LVL 10

Expert Comment

by:ollyatstithians
ID: 24422520
Not sure I understand what you mean by "saving url".

Olly.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:ST3VO
ID: 24422599
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.
0
 
LVL 10

Expert Comment

by:ollyatstithians
ID: 24423240
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

0
 

Author Comment

by:ST3VO
ID: 24423372
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!
0
 

Author Comment

by:ST3VO
ID: 24423382
when = where .... sorry about the spelling mistake :o)
0
 
LVL 10

Expert Comment

by:ollyatstithians
ID: 24423413
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

0
 
LVL 10

Expert Comment

by:ollyatstithians
ID: 24423425
Oh. Right. Stand by...
0
 

Author Comment

by:ST3VO
ID: 24423506
thanks!!!
0
 
LVL 10

Expert Comment

by:ollyatstithians
ID: 24423785
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

0
 
LVL 10

Expert Comment

by:ollyatstithians
ID: 24423789
Sorry, missed a closing bracket off that array.

Olly.
0
 
LVL 10

Expert Comment

by:ollyatstithians
ID: 24423800
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

0
 

Author Comment

by:ST3VO
ID: 24423943
Getting errors again with the Directory Does not exist :o/

0
 

Author Comment

by:ST3VO
ID: 24424016
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

0
 

Author Comment

by:ST3VO
ID: 24426876
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)
0
 

Author Closing Comment

by:ST3VO
ID: 31582912
Thanks :o)
0
 
LVL 10

Expert Comment

by:ollyatstithians
ID: 24429420
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

0
 

Author Comment

by:ST3VO
ID: 24429687
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?

0
 

Author Comment

by:ST3VO
ID: 24429781
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now