PHP Push File via FTP

There are plenty of situations where we need to push MLS files to a particular clients servers and most of the time we use SSH Bash commands to achieve this but certain instances may also be doable using PHP. Such is the case when working with some client servers we may not have access to the servers root. So, a PHP solution was created.

Rather than go into an elaborate or lengthy process we came up with this simple PHP snippet that has thus far worked in most of the instances where the PHP method is required. The process is rather simple. We add a php file to a particular directory where the files reside and then set the script to run on a CRON job to automate the periodical updates. The files within the array are then pushed or uploaded via FTP to the clients domain.

Now although we could have put in conditionals to check first to see if the file exists, this particular script isn’t overtly complex as the script would not run on our end if files did not exist to begin with. However, I do plan on updating this script in no immediate haste to include a file check and even email notification features. You have no idea how many times we have clients that want to know if their daily process worked so adding in an MySQL update and notification feature are certainly forthcoming.

[php]
$host = ‘clickoncehosting.com’; // without http://, with www.
$username = ”;
$password = ’84#1!cYtg94Th’;
// Usually: /public_html/your_dirs/ or even /httpdocs/your_dirs/
$path = ‘/public_html/example/files/’;

$filelist = array(‘filename.csv’, ‘filename2.csv’, ‘filename3.csv’, ‘filename4.csv’, ‘zipfile.zip’, ‘zipfile2.zip’);
$conn_id = @ftp_connect($host);

// login with username and password
$login_result = @ftp_login($conn_id, $username, $password);

// check connection
if ((!$conn_id) || (!$login_result)) {
echo "<p align=’center’>FTP connection has failed!<br/>";
echo "Attempted to connect to $host for user $username</p>";
exit;
} else {
echo "<p align=’center’>Connected to $host, for user $username</p>";
}
if(!@ftp_chdir($conn_id, $path)){
echo "<p align=’center’>Incorrect directory path!</p>";
exit;
};

foreach($filelist as $file){
$upload = @ftp_put($conn_id, $file, $file, FTP_BINARY);
if (!$upload) {
echo "<p align=’center’>FTP upload has failed!</p>";
} else {
echo "<p align=’center’>Uploaded $file to $host as $file</p>";
}
}
// close the FTP stream
ftp_close($conn_id);
[/php]

Configuring the script for FTP upload of files.

[fancy_list style=”circle_arrow” variation=”olive”]

  • Set your FTP Configration Params
  • Add list of files to the array
  • Upload to a directory where files reside.
  • Run the script either from browser or shell command.
  • I highly suggest you password protect the directory and or keep it out of the publically accessable path.

[/fancy_list]
[button link=”http://clickoncehosting.com/accounts/downloads.php” size=”medium” variation=”olive”]Download Zip File[/button]

PHP Directory Generator

This little snippet of code was created for clients that needed to create 100 or 1000 folders for use in the hosting of MLS images. The idea is to create folders that images would be stored in based on the MLS number. So for example if you have photos with MLS ID’s like R9057842.jpg, you could store images in folders based on the last two or three digets of the MLS Photo ID. Since Linux machines have a physical limitation of 4GB per folder its a good idea to break up your images into a manageable hierarchy. If your MLS has only a few thousand listings then a two place folder structure would suffice. If however you are in a market with say 50K listings, a thousand folders or even 10,000 folders may be necessary.

Having had years and years of experience in managing images for RETS and IDX, it is likely you will want to stick with 1,000 folders as the ideal. Click Once does have scripts for managing images in the snippet area.

Here is a look at the actual code.

[php]
$foldercount = 1000; // Set either 100 or 1000
$thumbnail = "thumbs"; // Name your thumbnail folder
$removefolders = 0; // Remove all folders 1 = true

for($i=0;$i<$foldercount;$i++){
if($foldercount == 100){
if($i<=9){
$dir = ‘0’.$i;
}else{
$dir = $i;
}}
elseif($foldercount == 1000){
if($i<=9){
$dir = ’00’.$i;
}elseif($i<=99){
$dir = ‘0’.$i;
}else{
$dir = $i;
}
}

if(is_dir($dir))
echo "<br />Directory ".$i." exists!";
else
mkdir($dir, 0777);

if(is_dir($dir."/".$thumbnail))
echo "<br />Directory ".$thumbnail." exists!";
else
mkdir($dir."/".$thumbnail, 0777);

// Lastly we simply copy a blank index.php file into each folder.
// We could create the file but have discovered on some hosting accounts
// we can’t so, to be safe, lets just use an index.php file
copy("index.php", $dir."/index.php");
copy("index.php", $dir."/".$thumbnail."/index.php");
echo "<br />Directory ".$i." created";
}
[/php]

Comes down to a few simple steps.

[fancy_list style=”circle_arrow” variation=”olive”]

  • Create an image directory on your server for image folders such as /listing_photos/ or /mls-photos/
  • Change settings in directorygenerator.php file to reflect number of folders, 100 or 1000, then set the name of the thumbnail folder.
  • Upload both files, directorygenerator.php and the blank index.php file to your new folder.
  • Run the script either from browser or shell command.
  • Delete the directorygenerator.php file.

[/fancy_list]
[button link=”http://clickoncehosting.com/accounts/downloads.php” size=”medium” variation=”olive”]Download Zip File[/button]

testing

Aenean eu tellus nulla, non luctus tellus. Curabitur odio quam, congue in euismod eget, bibendum in dui. Vestibulum id purus id enim rhoncus volutpat. Etiam adipiscing gravida faucibus. Nulla mattis consectetur rhoncus. Sed dignissim rhoncus nisi, sed fringilla justo commodo nec. Nam adipiscing, nisl ut facilisis facilisis, dui leo porttitor sapien, nec venenatis tellus magna ac ligula. Cras nec semper velit. Nam vel lacus at nisl luctus varius ut non massa. Proin congue venenatis felis vitae sodales. Aliquam erat volutpat. Integer at arcu sit amet arcu ornare ornare. Curabitur dictum purus non odio malesuada eleifend. Ut convallis risus et nibh auctor eget sodales erat dapibus. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.

Ut augue justo, tempor lacinia pellentesque vitae, fermentum at massa. Nullam ultricies feugiat lacus, eu porttitor magna accumsan sed. Integer pulvinar tellus ut velit accumsan lobortis. In hac habitasse platea dictumst. Donec venenatis vestibulum orci, sed porttitor quam laoreet vitae. Integer luctus, dolor at vestibulum commodo, neque augue malesuada ligula, vitae consequat ligula est eget dolor. Fusce facilisis, nulla eget posuere pharetra, purus metus cursus magna, vitae pellentesque libero sem id metus. Ut tempus blandit ligula quis pretium.

In quam justo, tincidunt non scelerisque a, sollicitudin a justo. Aliquam vitae tellus ipsum, sit amet ultricies enim. Donec id eros nulla. Curabitur gravida tellus ac turpis vestibulum ut laoreet est bibendum. Nullam suscipit ligula ut metus tristique egestas. Aliquam ut est odio, et egestas dui. Praesent sed mi gravida est luctus malesuada vulputate quis tellus. Mauris porta lobortis vehicula. Donec blandit ullamcorper eros, ac mattis arcu aliquet id. Pellentesque a ornare arcu. Suspendisse sit amet justo purus, id egestas erat. Fusce est ipsum, bibendum id porta in, adipiscing ac mi. Aenean sem nibh, pretium ac vulputate non, elementum ac magna.

This is a post with post format of type Link

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim.

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus.

This is a standard post format with preview Picture

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.

Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi.

Read more

Post Formats is a theme feature introduced with Version 3.1. Post Formats can be used by a theme to customize its presentation of a post.

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus – more on WordPress.org: Post Formats