Solved

php script help

Posted on 2013-12-03
8
515 Views
Last Modified: 2013-12-09
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
Comment
Question by:MTCCOMP
  • 5
  • 2
8 Comments
 

Author Comment

by:MTCCOMP
Comment Utility
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
 

Author Comment

by:MTCCOMP
Comment Utility
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
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Accepted Solution

by:
MTCCOMP earned 0 total points
Comment Utility
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
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 200 total points
Comment Utility
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
 

Author Closing Comment

by:MTCCOMP
Comment Utility
Thanks for all the input
0
 

Author Comment

by:MTCCOMP
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

743 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

7 Experts available now in Live!

Get 1:1 Help Now