PDA

View Full Version : How to loop to make it work?


student101
04-08-2008, 07:49 PM
How to loop through the script to make it work better?

<?php
$host = 'www.example.com'; // host (website) that contains the folder you want to change
$user = '******';
$password = '******';
$folder = '/folder/nestedfolder/';
//Set the next folder to set permissions on
$folder2 = '/folder/nestedfolder2/';
$mod = '0755';
// connect to FTP site
$conn = ftp_connect("$host");
if (!$conn)
{
echo 'Error: Could not connect to ftp server';
exit;
}
// log in to FTP site
@ $result = ftp_login($conn, $user, $password);
if (!$result)
{
echo "Error: Could not log on as $user";
ftp_quit($conn);
exit;
}

//HOW TO LOOP FROM HERE FOR $folder2 or any other variables?

// try to change the permissions on the directory
if (ftp_site($conn, 'CHMOD '.$mod.' '.$folder)) {
echo "Successfully changed permissions";
}
else {
echo "There was a problem changing the permissions";
ftp_quit($conn);
exit;
}
// close the connection
ftp_close($conn);
?>

pete
04-08-2008, 09:51 PM
You can set your folders in an array like below, run this and then add it to your script:


<?php
$aCmds = array();
array_push($aCmds,
array(
'mod'=>'0755',
'folder'=>'/folder/nestedfolder/'
),
array(
'mod'=>'0755',
'folder'=>'/folder/nestedfolder2/'
),
array(
'mod'=>'0755',
'folder'=>'/folder/nestedfolder3/'
)
);
foreach($aCmds as $cmd){
echo $cmd['mod'].' '.$cmd['folder'].'<br />';
}
?>
You can also run unix commands like this using exec($cmd,$ret); where $cmd is the command to run and $ret the return value.

Personally I wouldn't use PHP to run FTP commands, I would opt for using a shell script instead, like this:


#!/bin/sh
ftphost='ftp.yoursite.com'
ftpuser='username'
ftppassword='password'
ftp -n $ftphost <<END_SCRIPT
quote USER $ftpuser
quote PASS $ftppassword
quote PASV
cd /home/yoursite/
mkdir hello
chmod -R 777 hello
quit
END_SCRIPT
That makes a directory called hello and chmods to 777, obviously you would add your own commands.
I hope that helps.

student101
04-09-2008, 05:57 AM
Thanks for that one, made it a bit simpler.

These arrays are something else.

$folders = array('/path/to/dir1','path/to/dir2','path/to/dir3');
foreach ( $folders as $folder )
{
// use $folder here
}


I have no clue on shell scripts, can they be used on ASP as well?

Cheers

pete
04-09-2008, 07:31 AM
Thanks for that one, made it a bit simpler.

These arrays are something else.

$folders = array('/path/to/dir1','path/to/dir2','path/to/dir3');
foreach ( $folders as $folder )
{
// use $folder here
}
I have no clue on shell scripts, can they be used on ASP as well?

Cheers

Your array is fine but wouldn't be if you wanted to chmod to different values, which is why I added a multi dim array. Later you might want to use imagemagick to convert images and create an array of commands that you run through to do that for example. In that case a multi-dim array is often the only way to go, it is also very readable once you get used to them. This is important when you come to make changes a year later.

I don't use ASP, only PHP, I'm not sure the same exists for ASP, that is beyond my knowledge. If you are a PHP'er, in time you will end up using more unix on the server and you tend to naturally learn the basics. For cron jobs and more server orientated tasks shell scripts are the way to go. It might look confusing but as with all languages you just start with the basics and play around and go from there.

Ideally you need a host that doesn't restrict SSH on the server or if you have a Mac practice on that in the 'Terminal'.

student101
04-09-2008, 07:38 AM
Thanks,
Some hosts(understanding people driven) give you all and all, others(lazy people driven) are slack bitches that know nothing.

You have to deal with what you get dealt, I guess(or move).

Cheers