Fetching product details from commission junction using php and uploads csv files to uploads folder.This code fetches only the files created yesterday.
<?php
//FETCH FILES FROM CJ USING PHP AND UNZIP THAT GZIP FILE USING PHP FUNCTION AND CREATE CSV FILE FROM IT.
//WE FETCH ONLY FILES CREATED ON YESTERDAY,NOT ALL FILES
//SET MEMORY LIMIT AND ALSO EXECUTION TIME IF NEEDED
ini_set ( "memory_limit" , "200M" );
//Set here Memory size to 200MB becuase we have to download file size of large MB
//IF NOT SET IT CAUSES MEMORY ALLOCATION PROBLEM
ini_set ( "max_execution_time" , "3600" ); //SET TO 1 HOUR
$ftp_server = "datatransfer.cj.com" ;
$ftp_user_name = "username" ; //Give FTP username for cj
$ftp_user_pass = "password" ; //Give FTP password for cj
$local_path = '/opt/lampp/htdocs/cj/' ; //LOCAL FILE PATH
$server_path = 'outgoing/productcatalog/26997/' ; //CJ FILE PATH
function parse_rawlist ( $array )
{
$i = 0 ;
foreach( $array as $curraw )
{
$struc = array();
$current = preg_split ( "/[\s]+/" , $curraw , 9 );
$struc [ 'perms' ] = $current [ 0 ];
$struc [ 'number' ] = $current [ 1 ];
$struc [ 'owner' ] = $current [ 2 ];
$struc [ 'group' ] = $current [ 3 ];
$struc [ 'size' ] = $current [ 4 ];
$struc [ 'month' ] = $current [ 5 ];
$struc [ 'day' ] = $current [ 6 ];
$struc [ 'time' ] = $current [ 7 ];
$struc [ 'zfile_name' ] = $current [ 8 ];
$structure [ $i ] = $struc ;
$i ++;
}
return $structure ;
}
$conn_id = ftp_connect ( $ftp_server , 21 );
if( $conn_id )
{
echo "Successfully connected" ;
}
$login_result = ftp_login ( $conn_id , $ftp_user_name , $ftp_user_pass );
ftp_chdir ( $conn_id , $server_path ); //Change Current Working Directory
$file_list = ftp_rawlist ( $conn_id , "." );
$zip_details = parse_rawlist ( $file_list );
$yest_date1 = strtotime ( "-1 day" );
$yest_date = strtotime ( date ( 'M/d/Y' , $yest_date1 ));
for( $i = 0 ; $i < count ( $zip_details ); $i ++)
{
$fname = $zip_details [ $i ][ 'zfile_name' ];
$mod_date1 = $zip_details [ $i ][ 'month' ]. "/" .( $zip_details [ $i ][ 'day' ]). "/" . date ( "Y" ); //Changes here
$mod_date = strtotime ( $mod_date1 ); //File Modified Date
if( $mod_date == $yest_date ) //If modified date equals yesterdays date
{
$local_file = $fname ;
$server_file = $fname ;
if ( ftp_get ( $conn_id , $local_file , $server_file , FTP_BINARY ))
{
echo "\n<br/>Successfully written to $local_file \n" ;
}
else
{
echo "\n<br/>There was a problem\n" ;
}
$contents = '' ;
$size = 52428800 ; //=50MB
if( file_exists ( $local_file ))
{
//To Get the size of the uncompressed file
$FileRead = $local_file ;
$FileOpen = fopen ( $FileRead , "rb" );
fseek ( $FileOpen , - 4 , SEEK_END );
$buf = fread ( $FileOpen , 4 );
$GZFileSize = end ( unpack ( "V" , $buf ));
fclose ( $FileOpen );
//To Get the size of the zip file
$fzip_size = $GZFileSize ;
$filename = $local_file ;
$ext = pathinfo ( $filename , PATHINFO_EXTENSION );
$file_name = pathinfo ( $filename , PATHINFO_FILENAME );
$file_name = $file_name . ".csv" ;
$zd = gzopen ( $filename , "rb" );
//If file size is greated than 50MB Cut that file otherwise causes memory allocation problem
if( $fzip_size > $size )
{
$fzip_size = $size ;
}
$contents = gzread ( $zd , $fzip_size );
gzclose ( $zd );
$file_name = "uploads/" . $file_name ;
$fp = fopen ( $file_name , "wb" );
if( strlen ( $contents ) == 52428800 )
{
$cval = explode ( "\n" , $contents );
$i = count ( $cval ) - 1 ;
unset( $cval [ $i ]);
$contents = implode ( "\n" , $cval );
fwrite ( $fp , $contents );
}
else
{
fwrite ( $fp , $contents ); //write contents of feed to cache file
}
fclose ( $fp );
if( file_exists ( $file_name ))
{
chmod ( $file_name , 0755 );
unlink ( $local_file );
}
}
}
}
ftp_close ( $conn_id );
?>
Remote Archive (Zip, Tar, Gzip) downloader with FTP and local extration support Categories : PHP , FTP , Filesystem , PHP Classes , Compression A PHP Script that shows how to use FTP to run a shell script, read two local files and update data in a database. Categories : PHP , Filesystem , FTP , Date Time , Databases 3 lines of Code to extract Tar, Zip, Gzip etc.. Categories : PHP , Filesystem , PHP Classes , Compression PHP ZIP file creation on a linux box using exec. Create ZIP files containing images Categories : PHP , Compression , Filesystem Using PHP to Delete a directory with all sub directories and files using FTP Categories : PHP , FTP , Directories , Filesystem Directory TreeView - File Manager & Explorer - FTP - Utility - PHP/HTML - Categories : PHP , Directories , FTP , Filesystem , HTML and PHP Upload function using PHP's FTP abilities. Categories : PHP , Filesystem , FTP Moving folder hierarchy b/w server Categories : PHP , FTP , Filesystem Link Submition - Allow your visitors to submit links to the site. Categories : PHP , Arrays , Filesystem , Beginner Guides Unix Disk Information with graphs Categories : PHP , Shell Scripting , Filesystem Upload Via FTP - an alternative to move_uploaded_file Categories : PHP , FTP , Beginner Guides Finds files on your site, uses UNIX find command. Categories : Complete Programs , Filesystem , PHP Show Source with Line Numbers Categories : PHP , Regexps , Filesystem file class , uploade file , download file already uploaded on another website Categories : PHP , PHP Classes , Filesystem , Web Services Image Browser Categories : Filesystem , GD image library , Content Management , PHP