Skip to content

Instantly share code, notes, and snippets.

@sapegin
Created February 22, 2011 15:41
Show Gist options
  • Save sapegin/838848 to your computer and use it in GitHub Desktop.
Save sapegin/838848 to your computer and use it in GitHub Desktop.
Microstock FTP Uploader
<?php
$stocks = array (
'shutterstock' => array (
'name' => 'Shutterstock',
'host' => 'ftp.shutterstock.com',
'login' => '',
'password' => '',
'dir' => '/',
),
'fotolia' => array (
'name' => 'Fotolia',
'host' => 'submit.fotolia.com',
'login' => '',
'password' => '',
'dir' => '/',
),
'dreamstime' => array (
'name' => 'Dreamstime',
'host' => 'upload.dreamstime.com',
'login' => '',
'password' => '',
'dir' => '/',
),
'bigstock' => array (
'name' => 'BigStockPhoto',
'host' => 'bigstockphoto.com',
'login' => '',
'password' => '',
'dir' => '/',
),
'123rf' => array (
'name' => '123RF',
'host' => 'submit.123rf.com',
'login' => '',
'password' => '',
'dir' => '/',
),
'canstock' => array (
'name' => 'CanStock',
'host' => 'ftp.canstockphoto.com',
'login' => '',
'password' => '',
'dir' => '/',
),
'crestock' => array (
'name' => 'Crestock',
'host' => 'ftp.crestock.com',
'login' => '',
'password' => '',
'dir' => '/',
),
'yaymicro' => array (
'name' => 'YAY Micro',
'host' => 'ftp.yaymicro.com',
'login' => '',
'password' => '',
'dir' => '/',
),
'lori' => array (
'name' => 'Lori',
'host' => 'ftp.lori.ru',
'login' => '',
'password' => '',
'dir' => '/',
),
);
?>
<?php
/**
* Microstock FTP Uploader
* © 2008 Artem Sapegin (github.com/sapegin)
*/
/**
* TODO:
* Check metadata:
* 1. title, description, keywords shoundn't be empty
* 2. minimum 7 words in description (???)
* 3. minimum 7 keywords (shutterstock)
> IMPORTANT: Your titles must not be longer then 30 Characters (Dreamstime rules) and your description must contain
> at least 7 words and not just be a duplicate of the title (Bigstockphoto rules).
> http://www.arcurs.com/easy-keywording
*/
require 'config.php';
define( 'DIR', dirname( dirname( __FILE__ ) ) );
/**
* Confing
*/
define( 'ENGINE_DIR', DIR . '/stock' ); // Folder with PHP files
define( 'UPLOAD_DIR', DIR . '/upload' ); // Folder with files to upload
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
echo "\nMicrostock FTP Uploader\n(C) 2008 Artem Sapegin\n\n";
$start = microtime();
$stocks_time = array ();
$stocks_to_process = false;
if ( ( 2 == $argc ) || ( 3 == $argc ) ) {
$stocks_to_process = ( 'all' == $argv[ 1 ] ) ? array_keys( $stocks ) : explode( ',', $argv[ 1 ] );
}
else {
echo "Available sites:\n\n";
foreach ( $stocks as $id => $params ) {
echo str_pad( $id, 15 ), $params['name'], "\n";
}
echo "\nall all\none,two,three several\n\n";
exit;
}
chdir( UPLOAD_DIR );
$day_of_week = date( 'N' );
$files_to_upload = glob( '*.jpg' );
if ( 0 == count( $files_to_upload ) ) {
echo "Nothing to upload.";
}
else {
foreach ( $stocks_to_process as $id ) {
uploadStock( $id );
}
}
echo "\nTotal working time: ", dt_microtime_diff( $start ), " с\n";
foreach ( $stocks_time as $name => $time ) {
echo str_pad( "$name:", 20 ), "$time с\n";
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
function uploadStock( $id ) {
global $stocks, $files_to_upload, $stocks_time, $day_of_week;
if ( !$stocks[ $id ] ) {
echo "Unknown site: $id.\n";
return false;
}
$params = $stocks[ $id ];
$name = $params[ 'name' ];
echo "\nConnectiong to $name... ";
$ftp = @ftp_connect( $params[ 'host' ] );
if ( !$ftp ) {
echo "Error!\n";
return false;
}
$login = @ftp_login( $ftp, $params[ 'login' ], $params[ 'password' ] );
if ( !$login ) {
echo "Wrong login and/or password.\n";
return false;
}
echo "OK\n";
$stocks_time[ $name ] = microtime();
$result = true;
foreach ( $files_to_upload as $filename )
{
$r = uploadFile( $ftp, $filename, $params[ 'dir' ] );
if ( !$r ) {
$result = false;
}
}
$stocks_time[ $name ] = $result ? dt_microtime_diff( $stocks_time[ $name ] ) : 'Failed!';
ftp_close( $ftp );
return true;
}
function uploadFile( $ftp, $filepath, $dir ) {
$filename = basename( $filepath );
echo "Uploading $filename... ";
$remote_filename = rtrim( $dir, '/' ) . "/$filename";
$size = filesize( $filepath );
$remote_size = ftp_size( $ftp, $remote_filename );
if ( $size == $remote_size ) {
echo "Skipped\n";
return true;
}
$upload = ftp_put( $ftp, $remote_filename, $filepath, FTP_BINARY );
if (!$upload) {
echo "Error!\n";
return false;
}
echo "OK\n";
return true;
}
function dt_microtime_diff($start)
{
list ($usec_s, $sec_s) = explode(' ', $start);
list ($usec_n, $sec_n) = explode(' ', microtime());
return round(((float)$sec_n + (float)$usec_n) - ((float)$sec_s + (float)$usec_s));
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment