Solved

php script help

Posted on 2013-12-03
8
516 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
ID: 39693320
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
ID: 39693859
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
ID: 39694036
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Accepted Solution

by:
MTCCOMP earned 0 total points
ID: 39694587
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
ID: 39695219
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
ID: 39705697
Thanks for all the input
0
 

Author Comment

by:MTCCOMP
ID: 39707752
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article will show, step by step, how to integrate R code into a R Sweave document
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

895 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

19 Experts available now in Live!

Get 1:1 Help Now