Jump to content

[SOLVED] Can I use an array to shorted this script


almightyegg

Recommended Posts

I have a file that when executed it re-writes a group of files to a list of directorys like so:

 

<?
$openfile = fopen("/home/lordofth/public_html/areas/panel.php", "w");
fwrite($openfile, file_get_contents("/home/lordofth/public_html/layout/panel.php"));
fclose($openfile);
$openfile = fopen("/home/lordofth/public_html/areas/links.php", "w");
fwrite($openfile, file_get_contents("/home/lordofth/public_html/layout/links.php"));
fclose($openfile);
$openfile = fopen("/home/lordofth/public_html/areas/links2.php", "w");
fwrite($openfile, file_get_contents("/home/lordofth/public_html/layout/links2.php"));
fclose($openfile);
$openfile = fopen("/home/lordofth/public_html/areas/icons.php", "w");
fwrite($openfile, file_get_contents("/home/lordofth/public_html/layout/icons.php"));
fclose($openfile);
$openfile = fopen("/home/lordofth/public_html/areas/default.css", "w");
fwrite($openfile, file_get_contents("/home/lordofth/public_html/layout/default.css"));
fclose($openfile);
$openfile = fopen("/home/lordofth/public_html/areas/default2.css", "w");
fwrite($openfile, file_get_contents("/home/lordofth/public_html/layout/default2.css"));
fclose($openfile);
$openfile = fopen("/home/lordofth/public_html/areas/copyright.php", "w");
fwrite($openfile, file_get_contents("/home/lordofth/public_html/layout/copyright.php"));
fclose($openfile);
$openfile = fopen("/home/lordofth/public_html/areas/bbcodes.php", "w");
fwrite($openfile, file_get_contents("/home/lordofth/public_html/layout/bbcodes.php"));
fclose($openfile);

$openfile = fopen("/home/lordofth/public_html/account/panel.php", "w");
fwrite($openfile, file_get_contents("/home/lordofth/public_html/layout/panel.php"));
fclose($openfile);
$openfile = fopen("/home/lordofth/public_html/account/links.php", "w");
fwrite($openfile, file_get_contents("/home/lordofth/public_html/layout/links.php"));
fclose($openfile);
$openfile = fopen("/home/lordofth/public_html/account/links2.php", "w");
fwrite($openfile, file_get_contents("/home/lordofth/public_html/layout/links2.php"));
fclose($openfile);
$openfile = fopen("/home/lordofth/public_html/account/icons.php", "w");
fwrite($openfile, file_get_contents("/home/lordofth/public_html/layout/icons.php"));
fclose($openfile);
$openfile = fopen("/home/lordofth/public_html/account/default.css", "w");
fwrite($openfile, file_get_contents("/home/lordofth/public_html/layout/default.css"));
fclose($openfile);
$openfile = fopen("/home/lordofth/public_html/account/default2.css", "w");
fwrite($openfile, file_get_contents("/home/lordofth/public_html/layout/default2.css"));
fclose($openfile);
$openfile = fopen("/home/lordofth/public_html/account/copyright.php", "w");
fwrite($openfile, file_get_contents("/home/lordofth/public_html/layout/copyright.php"));
fclose($openfile);
$openfile = fopen("/home/lordofth/public_html/account/bbcodes.php", "w");
fwrite($openfile, file_get_contents("/home/lordofth/public_html/layout/bbcodes.php"));
fclose($openfile);

 

 

Problem is, there's about 20 or so directories to fwrite() 8 files into, which from the above code, makes a large and long winded file.

 

So I was wondering, for ease of use, if it's possible to put the directories into an array of something and then it would fwrite() to all directories in there??

 

If it is possible, it'd be great if you could point me in the right direction, thanks

Try this...

<?php
$source='/home/lordofth/public_html/layout/';
$target='/home/lordofth/public_html/areas/';
$arrFiles=array('panel.php','links.php','links2.php','icons.php','default.css','default2.css','copyright.css','bbcodes.php');
copyFiles($source,$target,$arrFiles);
$source='/home/lordofth/public_html/layout/';
$target='/home/lordofth/public_html/account/';
$arrFiles=array('panel.php','links.php','links2.php','icons.phpdefault.css','default2.css','copyright.php','bbcodes.php');
copyFiles($source,$target,$arrFiles);

function copyFiles($source,$target,$arrFiles) {
  foreach ($arrFiles as $file) {
    copy($source.$file,$target.$file);
  }
}
?>

 

EDIT: Improved code and swapped source and target round as I had them wrong.

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.