I am new to PHP and have not yet learned all the syntax and commends but was wondering if an easy script can be made to copy files of the same name from one directory over to another directory thru a PHP script

in other words if I have 2 folders

1 named Folder1
the other named Folder2

and I wanted to copy files that have the same name from folder1 to folder 2 and have it replave the files currently in folder2 with the ones it copies from folder1 could this be done

EX

Folder1 has files names

help.txt
card.txt
user.txt

and folder 2 has the same files but older non updated versions ... what script could I use to copy the ones in Folder1 and replace the existing ones in folder2

Any help would be most appreeciated

I am trying to learn from all this and use this for a project at the same time ...

Thank you so much anyone that spends a few minutes of thier time to help with this

Midnite

Recommended Answers

All 38 Replies

Well, since you

trying to learn from all this and use this for a project at the same time

... than take a look at this copy().

TY amd_k8

OK I can work out that part now my only question is this ...

If I have say 24 files in a folder .... ex

12am,1am,2am,3am ETC ETC 11am, 12pm,1pm,2pm etc etc till 11pm

how would I make an array that would loop and give me each seperate file name ? in the variable that I would be using ? Any help ?

TY amd_k8

OK I can work out that part now my only question is this ...

If I have say 24 files in a folder .... ex

12am,1am,2am,3am ETC ETC 11am, 12pm,1pm,2pm etc etc till 11pm

how would I make an array that would loop and give me each seperate file name ? in the variable that I would be using ? Any help ?

Maybe you should experiment with it and see what you come up with?

I was kinda hoping someone would be kind enough to make a working script that I could implement and learn from at the same time ... I am crunched for time and really dont have the time right now to fully learn all I would need to in order to get this done...

Again if anyone can help I would be most greatful

Thanks
Midnite

Member Avatar for diafol

I was kinda hoping someone would be kind enough to make a working script that I could implement and learn from at the same time ... I am crunched for time and really dont have the time right now to fully learn all I would need to in order to get this done...

Again if anyone can help I would be most greatful

Thanks
Midnite

Slavery has been abolished in most civilised countries and online forums. You don't have the time, but you want members to give up their precious time to do your work for you. Perhaps you want to resubmit your request? There again, you may find some sap or young buck on this forum who'll do it out of sheer ego.

commented: I think it's a pretty apt comment personally. +10
commented: I think being rude to someone new asking for help is very unfair to someone who is NEW +0

Take a look at scandir().

Can someone please help with this ... I thought thats what we are all here for to help one another ... not to be nasty and make rude comments to someone that is not as knowledgeable as others in certain programming languages ... I asked for help ... didint ask to be ridiculed

Thanks for those that do help

those that pass judgement ... thats not cool ... I am an inexperienced PHP programmer asking for assistance

Thanks again
Midnite

Did you look at scandir() in addition to copy() as previously mentioned? Ardav is merely pointing out that you seem to want people here to write scripts for you, but you haven't demonstrated any effort on your own at all. If you believe that his comments are unfair, show that you're trying to make some headway with this yourself when you ask for assistance.

Ezzaral

I have tried this on my own and have had no luck with it ... that is why I am here asking for help ... if someone was kind enough to do so I would learn from what they did and figure out how to do future scripts on my own -

I am simply asking for help to do something I need to and will play with the script and learn from it on my own ... just lost as to where to start ...

Yes I read those posts and have had no luck with it

Thanks
Midnite

Member Avatar for diafol

Can someone please help with this ... I thought thats what we are all here for to help one another ... not to be nasty and make rude comments to someone that is not as knowledgeable as others in certain programming languages ... I asked for help ... didint ask to be ridiculed

Thanks for those that do help

those that pass judgement ... thats not cool ... I am an inexperienced PHP programmer asking for assistance

Thanks again
Midnite

Not trying being nasty - just giving you a heads up. Asking for help is as you say the whole point of this type of forum. However, it is generally accepted that you make some sort of effort yourself.

I apologise if my post was a bit strong. On reflection, I should not have been so sarcastic.

I am still prepared to help if you post some code of your own as a starting point for discussion. The whole point of posting your own code is that you learn more from your own successes and failures than from dissecting somebody else' script - which may itself be erroneous. It also offers members many access points for support and thereby generates a busy thread.

Can someone please help with this ... I thought thats what we are all here for to help one another ... not to be nasty and make rude comments to someone that is not as knowledgeable as others in certain programming languages ... I asked for help ... didint ask to be ridiculed

Thanks for those that do help

those that pass judgement ... thats not cool ... I am an inexperienced PHP programmer asking for assistance

Thanks again
Midnite

Experienced PHP programmer?
Here's what you say in your first post in this thread:
"I am new to PHP and have not yet learned all the syntax and commends but was wondering if an easy script can be made to copy files of the same name from one directory over to another directory thru a PHP script"

You have been given hints, but you still have no code to show.
That is why we suspect that the deadline you are up against is your homework is due, but you would rather play beer pong while some other shmuck does your work for you.

The very premise of what your are trying to do smacks of an academic exercise, not anything that one would normally use PHP for.
I would think a shell script would be a better choice.

I have tried that is the POINT I dont know where to start i am new to this so i dont understand the syntax ? i can do this in shell script yes but what I dont understand in PHP is how to setup a loop like say how do I create a counter that would do the following

I am trying to start at 12am then go to 1am 2am etc like the hours in a day till 11pm then switch to 12pm and then 1pm and then continue till 11pm ....

I have setup and gotten the copy part to work I just dont know how to set a variable to the names of the files that I want to copy ...

see this is my code but what I need is to set a changing variable that will do each hour instead of hard coding each one as in my example

<?php
$file = 'generic/11pm.txt';
$newfile = 'playlists/11pm.txt';

if (!copy($file, $newfile)) {
    echo "failed to copy $file...\n";
}
?>

First of all I said that I am an

INEXPERINCED PHP PROGRAMMER

Not an experienced one and yes I have done this in shell which I do know

DAY=`date +%a`
DAY1="$DAY".txt
DIR0="playlists"
DIR1="dailyplaylists"
DIR2="generic"

rm "$DIR1"/Playlist_"$DAY1"

for i in `cat ./list`
do
cat "$DIR0"/"$i" | uniq  >> "$DIR1"/Playlist_"$DAY1"
done


for i in `cat ./list | grep -v 12amold.txt`
do
cp "$DIR2"/"$i" "$DIR0"/"$i"
done


exit

The point is I am not exp in PHP and therefore I am trying to learn how to do so ... and asking for somone to write something for me to learn from ... I dont understand the constant need to be put down told I am not trying ETC ETC accused of doing so for some academic credits at some school ... thats all just nonsense .. all I did was state that I am not an EXP php programmer and asked if someone could make a script to do what I was asking and then I would take it apart disect it and learn from it .... its that simple really

<?php
$file = 'generic/11pm.txt';
$newfile = 'playlists/11pm.txt';
// invoke the function before you test for success!
 copy($file, $newfile);
// now do the test
if (!copy($file, $newfile)) {
    echo "failed to copy". $file."...\n"; //fixed this up...
}
else
{
echo "File".$file." written or original overwritten";
}
?>

Now of coarse, the way you have it, all of those file names must exist in the current directory. It would probably be better to use full path names.

Member Avatar for diafol

Have a look at the glob() function. This is an example for extracting filesize info from all 'txt' files. I'm sure that you could do something with this. See filemtime().

<?php
foreach (glob("*.txt") as $filename) {
    echo "$filename size " . filesize($filename) . "\n";
}
?>

...all I did was state that I am not an EXP php programmer and asked if someone could make a script to do what I was asking...

Stop bitchin' - you're getting help - just not the meal ticket you were asking for.

ardav - Who is bitchin ? And I never asked for a meal ticket - where do you see me asking for that ? I asked for someone to create a script that does what I am trying to do so that I can test it and learn from it ... I never said HE MAKE HTIS FOR ME and THANKS ALOT ... OK second where did you get the idea that I needed the filesize of the txt files ? where did I ask about that ? I dont remember doing that and I see nowhere in my message that states that ...

What I said was I wanted to create a loop that would go thru the files in the folder and that they were labeled ...

12am, 1am, 2am, 3am ETC till 11am then 12pm,1pm,2pm till 11pm ...

That is all I asked about

JRM - Thank you for your assistance so far ... I see the difference in what you are doing as opposed to what I first created ... and I understand your reason behind copying first then testing ...

What I am trying to do is make the file titles in these variables change to each hour without having to write 24 files one for each hour

$file = 'generic/11pm.txt';
$newfile = 'playlists/11pm.txt';

so it would be something like this

$f1 = (( variable from 12am - 11am / then Variable from 12pm - 11pm ))
$file = 'generic/'.$f1;
$newfile = 'playlists/'.$f1;

or something like that - the problem I am having is making the counter to change the filename starting with 12am ( which may need to be seperate ) and then 1am-11am and then another seperate one again for 12pm and then a counter for the hours 1pm to 11pm ...

I do not know how to create the changing variable ... that would become the file name

in effect I am trying to copy the files from one directory to another ... is there a way to just copy a whole DIR from one to another maybe that would make it easier for me to figure out

Thanks
Midnite

ardav - Who is bitchin ? And I never asked for a meal ticket - where do you see me asking for that ? I asked for someone to create a script that does what I am trying to do so that I can test it and learn from it ... I never said HE MAKE HTIS FOR ME and THANKS ALOT ... OK second where did you get the idea that I needed the filesize of the txt files ? where did I ask about that ? I dont remember doing that and I see nowhere in my message that states that ...

What I said was I wanted to create a loop that would go thru the files in the folder and that they were labeled ...

12am, 1am, 2am, 3am ETC till 11am then 12pm,1pm,2pm till 11pm ...

That is all I asked about

JRM - Thank you for your assistance so far ... I see the difference in what you are doing as opposed to what I first created ... and I understand your reason behind copying first then testing ...

What I am trying to do is make the file titles in these variables change to each hour without having to write 24 files one for each hour

$file = 'generic/11pm.txt';
$newfile = 'playlists/11pm.txt';

so it would be something like this

$f1 = (( variable from 12am - 11am / then Variable from 12pm - 11pm ))
$file = 'generic/'.$f1;
$newfile = 'playlists/'.$f1;

or something like that - the problem I am having is making the counter to change the filename starting with 12am ( which may need to be seperate ) and then 1am-11am and then another seperate one again for 12pm and then a counter for the hours 1pm to 11pm ...

I do not know how to create the changing variable ... that would become the file name

in effect I am trying to copy the files from one directory to another ... is there a way to just copy a whole DIR from one to another maybe that would make it easier for me to figure out

Thanks
Midnite

Midnite,
Me thinks you protest too much! You really need to put forth more effort
Anyway, if you want the hours to correlate to the local time .
look here us3.php.net/manual/en/function.localtime.php
The PHP site is an excellent resource.
probably the localtime() function should be of interest. You can extract the hour of local time from the array. It is, however a 24 hour clock. If you want to do the Am pm thing, futher conversion will be necessary.

The way I read the files from a directory was similar to:

$filhnd = @opendir($dirname);
    if (!$filhnd)
    {
        printf ("<b>Directory [%s] not opened </b><br />", $dirname);
    }
    else
    {
        while ($fil = readdir($filhnd))
        {
##        now $fil contains a filename from the directory.
##        It could be anything, even a subdirectory name
##        so you have some testing to do.  
##        Then if the filename is worth saving:
             $filelist[] = trim($fil);
        }
        closedir($filhnd);
    }
Member Avatar for diafol

OK second where did you get the idea that I needed the filesize of the txt files ? where did I ask about that ? I dont remember doing that and I see nowhere in my message that states that ...

I used that as an example of how to use glob(). Substitute filesize for filemtype.

Last post from me. Will not bother you again.

Ardav,
You've got a lot more patience than me. I'd have chucked this threat ages ago.

Midnite,
The posters on here have been trying to help you learn. And learning isn't easy it takes _your_ effort. Through out this post you've asked for a script so you can test it and examine it to see how it works. Write your own and you'll know how it works.
You might wish to change your login name too, I'll be very reluctant to answer your posts.

OK for the LAST TIME - I am not a student ... I am not here for homework ... and I am not asking for HAND OUTS ... I am simply a new person to PHP programming and I am asking for a little help with creating a script that will do what I am trying to do ... why all the attitude and negativity ... look if you dont want to help then just keep your feelings thoughts and opinions to yourself cause honestly I am not interested in someone that knows a programming code that I am not familiar with RUNNING off at the mouth cause they know more then I do ... SIMPLE you dont want to help then dont but passing judgement on somone or closing threads that they are opening to ask for help from those that are willing to do so is just totally ignorant and wrong ....

I am working on MY personal website and that is what I want this script for and to learn from ... its very simple and instead of running off with the drama why wouldnt you offer to help somone ...

I dont just cut and paste code I change it modify it and learn from it as I do ... so why the UNNEEDED DRAMA ...

OK for the LAST TIME - I am not a student ... I am not here for homework ... and I am not asking for HAND OUTS ... I am simply a new person to PHP programming and I am asking for a little help with creating a script that will do what I am trying to do ... why all the attitude and negativity ... look if you dont want to help then just keep your feelings thoughts and opinions to yourself cause honestly I am not interested in someone that knows a programming code that I am not familiar with RUNNING off at the mouth cause they know more then I do ... SIMPLE you dont want to help then dont but passing judgement on somone or closing threads that they are opening to ask for help from those that are willing to do so is just totally ignorant and wrong ....

I am working on MY personal website and that is what I want this script for and to learn from ... its very simple and instead of running off with the drama why wouldnt you offer to help somone ...

I dont just cut and paste code I change it modify it and learn from it as I do ... so why the UNNEEDED DRAMA ...

If you are not a young person, you certainly have a healthy sense of entitlement like many "20 somethings" do.
Why don't you study this stuff, write bad programs ,and hone your skills like the rest of did?
What makes YOU above all of that effort?
If you don't have an interest in writing programs, but want a personal website, hire someone who knows what they are doing to design it for you...or better yet ,have your parents pay for it like they always do.

commented: Exactly! +6

JRM = first of all I am probably older then you are ... secondly I have no problem learning and writing bad code like all the rest but what I was asking for was a starting point so that I had an idea on what I was doing and a direction to head in

JRM its simple like I told all the others if you dont want to offer to help then dont no need to throw around your uncalled for comments or attitude ... its that simple ... its real easy to be aman with a big mouth behind a PC ... ISNT IT !!!

But how many of you are MEN ? not code kiddies ( I mean hell you whine like your 5 WAHHHHH you want help WAHHHHH you dont want to learn like the rest of us did WAHHHHH ) oh just stop already here is your pacifier baby ... now as I said if you have nothing but BS to throw around save your breathe cause I really dont care to hear what you or any of the other whiners have to say

Midnite

do actually still wonder why nobody will assist you

read your posts, as one of those who could, if so inclined, assist you reads.
be better off getting a new login and beginning again

Member Avatar for diafol

I really dont care to hear what you or any of the other whiners have to say

Go to another forum. You've obviously ruffled the feathers of everybody here - some of whom would go the extra mile to ensure a solution for you (myself included). This isn't a dig, just a fact. OR do as AB suggests, get a new login and calm the hell down.

JRM its simple like I told all the others if you dont want to offer to help then dont no need to throw around your uncalled for comments or attitude ... its that simple ... its real easy to be aman with a big mouth behind a PC ... ISNT IT !!!

I guess that works pretty well for you too..eh?
Believe me, if I met you in person you would have heard the same.
I fear no man...especially one who thinks the world owes him something.
Now, a guy who takes charge of his destiny might be more of a challenge...but that ain't you.
Just stop all the nasty rhetoric already!
We told you what this board's culture is like, but you don't want to fit in.
Since nobody is going to write code for you, you might as well move on to "greener pastures" if you can find them.
Keep it up, and you will probably get banned anyway.

I have tried that is the POINT I dont know where to start i am new to this so i dont understand the syntax ?

As far as I know, one cannot learn a language before learning the basics. So before you tr to do anything, grab the basics

Then go back to scripts given before and make whatever you want out of them. If you fail somewhere, come back and ask.

commented: Bingo! +6
Member Avatar for diafol

EV - this is what he said:

I was kinda hoping someone would be kind enough to make a working script that I could implement and learn from at the same time ... I am crunched for time and really dont have the time right now to fully learn all I would need to in order to get this done...

He doesn't have the time, but thinks that we do. He even gave me a neg rep for pointing out this erroneous thinking. Well, that's the end of any help I'm prepared to offer. Huh, I can imagine that he'll get himself a new login with the name Midnite003!:icon_lol:

<?php
$i=1;
$a='am.txt';
$dir1 = 'generic/';
$dir2 = 'playlists/';

while($i<=12)
  {
  $name=$i.$a;
  $file = $dir1.$name;
  $newfile = $dir2.$name;
  if (!copy($file, $newfile)) {
    echo "failed to copy $file...\n";
  }
  $i++;
  }

$i=1;
$b='pm.txt';

while($i<=12)
  {
  $name=$i.$b;
  $file = $dir1.$name;
  $newfile = $dir2.$name;
  if (!copy($file, $newfile)) {
    echo "failed to copy $file...\n";
  }
  $i++;
  }

?>

this is what i have so far can anyone help now kindly - Please ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.