Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 526
  • Last Modified:

php script help

I have not done php.
I need a simple php script that will run in Linux or unix envr.


what I have:
10  xml files ( 1.xml, 2.xml ....)
in a folder on the web hosting server
the script will be run by some program everyday same time


what I want:
script that will change the file name  every day in this way

for example
change 1.xml to main.xml today
then next day
change 2.xml to main.xml ( it need to know what it did yesterday)
....
then next day
change 10.xml to main.xml
then next day (back to)
change 1.xml to main.xml
0
MTCCOMP
Asked:
MTCCOMP
  • 5
  • 2
2 Solutions
 
MTCCOMPAuthor Commented:
Some thing like this:


Server will have Three folders

InUse       - Copy of the currently in use file without name change so it remeber which one was changed
Main       - Copy of the Currently in use file with name change to Main.xml
AllFiles - Will have all the files (1.xml,2.xml....)


CODE:

FileName  = Get file name from \inuse\1.xml

InUseVer =  FileName (get file name without the extension)

InUseVer = InUseVar + 1

if( Inusevar > 10 ) then
InUseVer = 1
end if

FileName = InuseVar & ".xml"

Del \inuse\*.*
Copy \AllFiles\FileName   \Inuse\Filename
Copy \AllFiles\FileName   \Inuse\Main.xml
0
 
MTCCOMPAuthor Commented:
I have create some code, as I have never done the php, if someone check and suggest any thing.


<?php

$dirInUse = './InUse';
$dirMmain = './Mmain';
$dirAllFiles = './AllFiles';

$tempfilename = glob('./InUse/*.xml');

$filename  = basename($tempfilename, ".xml");
//echo $filename;
//echo "<br/>";

$Ver = (int)$filename + 1;
//echo $Ver;
//echo "<br/>";

if ($Ver > 10) {
      $Ver = 1;
}
//echo $Ver;
//echo "<br/>";

$newfilename = (string)$Ver . '.xml';
//echo "Output= {$newfilename}";
//echo "<br/>";

unlink('./InUse/*.xml');

copy($dirAllFiles & $newfilename, $dirInUse);
rename ($dirAllFiles & $newfilename, $dirMain & '/Main.xml');


?>
0
 
Ray PaseurCommented:
If you're new to PHP this article will help you find your way to good learning resources and avoid all of the bad code examples that litter the internet.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

I think your design will be easier if you use a data base to hold the XML documents, or at least to hold the file names.  If you want to give us the "high-level" view of the application, and tell us about the business rules you want to implement we may be able to help with a more mainstream design pattern.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
MTCCOMPAuthor Commented:
Thanks for replay, I was reading lot on the internet since i post the Question.

Overview of the problem is the Client has Application, whcih use RSS feed xml file. Which is picked up from the web folder on hosting site (dodday).  Which work Ok.

Problem is that Custom xml Feed changes/rotate every 35 days ( 35 xml file, one for each day, then loop back). These 35 file  do get change once in while but the keep rotating every day.

Current they were changing the file each day manually, I was trying to make is automate.
If they create 35 file, put in one folder and system automatcally pick one file and rename and put in the folder when the system will pickup the file.

I have not use php, but what was used at the web hosting site.

There what i have created from what i have leard today (searching).  And it is WORKING now. I like it if one of the expert polish the code or give any input.


<?php

$dirInUse = './InUse';  // to know what last file was use
$dirMain = './Main/';   // Rename & copy curret file in use, as program will pick from here
$dirAllFiles = './AllFiles/';  // location of all 35 files

$tempfilename = glob('./InUse/*.xml');

$filename  = basename($tempfilename[0], ".xml");

$Ver = (int)$filename + 1;
if ($Ver > 35) {
$Ver = 1;
}

$newfilename = (string)$Ver . '.xml';

unlink($tempfilename[0]);

copy ($dirAllFiles . $newfilename, $dirInUse.'/'.$newfilename);
copy ($dirAllFiles . $newfilename, $dirMain .'Main.xml');

?>
0
 
Ray PaseurCommented:
I wouldn't change a thing if it's working the way you want.  It looks fine to me.  Some day you may find that you want an abstraction layer between the numeric name and some kind of topical information.  When that time comes, you will want to use a data base.  But for now this is exactly what any professional programmer would do.

Best regards, ~Ray
0
 
MTCCOMPAuthor Commented:
Thanks for all the input
0
 
MTCCOMPAuthor Commented:
After 3 post, i got one reply. By that time i have put the some code together which you said was good. How should i rate that "A".

"A" should be the default grade awarded unless the answer is deficient. An A grade means the solution provided is thorough and informative or is a link to information that answered the question. Any links that are posted will be accompanied by a summary of what can be found there and how it helps solve the problem.
 
"B" is the grade given for acceptable solutions, or a link to an acceptable solution. A B grade means the solution given lacked some information or required you to do a good amount of extra work to resolve the problem. When closing the question, the asker should explain why a B grade was awarded.
 
In my case it was accepatable solution"B" as, it did not give me the answer
but was pointing me to to read some php materail, which i have already done as i had post the code before the first reply.

"B" does not mean"uncooperative or unable to help you"

I have appricate you help in atleast respoinding therefore i gave teh B and 200 points.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now