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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.