Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating Directories on a schedule

Status
Not open for further replies.

JCrou82

Programmer
Joined
Aug 23, 2002
Messages
265
Location
US
I want to know if there is a way to create a directory with subdirectories on an Apache Red Hat Linux Webserver using php? The directories would be created on the last day of the month or the first, either will do. Right now, I have to manually create these folders and the naming of these directories and subdirectories. These directories have a standard/structure so its pretty standard. Is this something I can or should do with PHP or should I create a different type of script in Red Hat using another language? I want it timed so that I don't want to go and run a script myself, I want it automated. But if it's do-able that way too, that would be ok, at least I'm not creating every directory or subdirectory.

Thanks
 
You need to fire off your script using cron.

If you have the PHP interpreter available as a stand-alone application (not just as an Apache module), then you can use mkdir() ( function in your script.

If you do not have the stand-alone interpreter, then you may have to switch to perl or a shell script. Want the best answers? Ask the best questions: TANSTAAFL!
 
what do I need so I can do it as a shell script? Or can I have it go off by clicking on a link or running a particular page?
 
you set it up with cron- so that it fires off a request to that script to run at the specified time. Once you set it up it should be automatic from then on. Sometimes, when my code just won't behave, I take it outside and make it listen to britney spears music, and when it comes back it's really well behaved. I wonder if it's suffering from post tramatic stress syndrome now..
 
It's two separate problems.

The first is getting a script written in some language outside the context of a web server.

The second is getting that script run at the appropriate time.


The first problem is solved by your having either PHP as a standalone interpreter, perl, bash, ruby, python, or some other language available that can be invoked from the command line. And having a script available to run in that language.

The second is to use some scheduling system to get the script run at the appropriate time. On a *nix system, the cron daemon can be configured to invoke the script at the appropriate time. Want the best answers? Ask the best questions: TANSTAAFL!
 
Thanks sleipnir214, but now i return it back to you and ask, how do I find out what languages I have running and/or whether PHP is ran as a standalone interpreter? I have seen a script or two created by the guy before me in which he was running back up jobs and saving them as .gz files. I don't remember what language it's in, but i'll double check

2. How do i set up or find out if I have cron or something like it running on my server?


Thanks to all,
 
If you are running a *nix operating system, you have cron.

If you can access the machine via telnet or ssh, you can issue:
Code:
echo &quot;<?php phpinfo(); ?>&quot; | php
at the command prompt. If you get data returned, then php is available as a command-line app. Want the best answers? Ask the best questions: TANSTAAFL!
 
thank you sleipnir214,

in the world of computer issues, you are a hero. I am running Red Hat Linux 7.0 so from your statement, I should have cron. And I telnetted in to my box and typed in exactly what you did, and i received a display of a number of html tags which in a browser would translate into the phpinfo() page that one uses to test a php installation, which should mean that I now have command-line (stand alone) PHP.

I need to figure out a way to create a php script that will create my directories. I then need to create a cron job that will run this file whenever the time is right (first of the month). Am I correct in these two statements?

The questions now are:

1. are their any sample scripts for creating directories and data structure?

2. Where do i save this file to? In other words, what should the path be where this particular script is located, if it even matters?

3. how do I create a cron job? Are there tutorials? and where do i save this cron job?

Thanks all
 
1. Probably not. The script would have to match your directory layout.

2. I would create a directory off my home directory on the server and store it there.

3. You create/edit/manage your crontab by issuing the command crontab -e . When you save the file, your crontab will be updated. Issue the command: man 5 crontab to see the format of a crontab. You will probably end up using the text editor &quot;vi&quot; when you invoke crontab -e. Check around the internet for tutorials on using vi. Want the best answers? Ask the best questions: TANSTAAFL!
 
ok, i'm back

Now how do I make a directory in php? i've looked on the web and on phpbuilder.com and found nothing on how to make/create a directory. Isn't there a pre-built function?

Thanks
 
Ok, i'm able to create the directories as wanted, but unfortunately the permissions that I set are not being applied. Can anyone help me with this?

mkdir(&quot;dirname&quot;,755);

and what ends up creating is a directory named &quot;dirname&quot; with the following permissions: 341

How come and how do I change it?
 
JCrou82,

You need to make sure that the user that runs the cronjob has the required credentials to not only create the folder (which is already happening) but also change the permissions to 755.

Check who owns the cronjob. You might want to create a user that has all the permissions necessary just in the specific area in which you create the directories.

 
ok guys,

I figured out why it wasn't working. PHP's mkdir needs to have the mode paramater entered as a an octal number. once i changed my code from:
mkdir(&quot;mydirectory&quot;,755);
to
mkdir(&quot;mydirectory&quot;,0755);
it worked like a charm.

Now my only issue is regarding cron. I've read up a little and cron has default job schedules that are set for hourly, daily, weekly and monthly. Well the monthly setting works for me since the &quot;create directories&quot; PHP file needs to run on the first of the month before business hours and 4 am is early enough. The only problem is, I'm not sure that if I run my php script (which is located in the htdocs directory) the directories created will not be owned by the right user. I was able to test the script by running it from a browser. By doing that, the httpd runs the php script creating the directories giving ownership to the user it's running as which in my case would be webuser. But if I were to run a cron job from the default settings, I'm not sure who would be given ownership? But then again, I can run the script in crontab under the webuser so that should create the directories with the webuser as owner? If I'm wrong in any way, please point it out to me.

Which way is it best and has a higher success rate, cron or crontab? also Should i move the php script out of htdocs or can I just leave it there? how do I create a link to the script so I can place it in the cron or crontab?


thank you, pardon me if i have asked alot of questions, it's just that this is beneficial for the success of our site.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top