Replace Funky Characters While Importing CSV

Sometimes uploaded text/csv file may have non-utf8 or other funky characters using the function below. public static function processUploadedBundles($request) { $content = file_get_contents($request->file(’uploadedFile’)->getRealPath());   $lines = explode(PHP_EOL, $content); $array = []; foreach ($lines as $line) { $arrayCsv = str_getcsv($line, ","); $arrayCsv = array_map(function($value){ return preg_replace(’/[\x00-\x1F\x7F-\xFF]/’, ”, $value); }, $arrayCsv); $array[] = $arrayCsv; }   return…

Stream Filter

In php one can use filters with streams.  Sometimes it can become handy.  Let’s say you open a .csv file as a stream, but this file is tab separated.  Your program can can process coma separated csvs, but not tab separated.  This is a good use case for a stream filter, because it can make replacements…

Complex Eloquent Query

A query below selects products that need to be updated in a remote application. It takes quantities of products in host application that are connected to source products application. On top of that it looks at all the orders that are in “Pending” status and reserves quantities for those products. SELECT products.quantity_available, connector_products.stock_id, products.id, connector_products.sku,…

Upload to FTP with PHP

$fp = fopen(’https://www.example.com/pdfdoc’, ‘r’);   $user = "sammy"; $pass = "password"; $ftp_server = "192.168.10.10";   //should be wrapped in try catch to properly handle errors $ftp_conn = ftp_ssl_connect($ftp_server); $login = ftp_login($ftp_conn, $user, $pass);   ftp_chdir($ftp_conn, ‘path/to/folder’); //can also use ftp_pwd   ftp_pasv($ftp_conn, true); //passive mode   ftp_fput($ftp_conn, "mydocument.pdf", $fp, FTP_BINARY);   fclose($fp); ftp_close($ftp_conn);$fp = fopen(‘https://www.example.com/pdfdoc’,…

Using Local and Public Disks

Having vagrant run on Windows host machine may cause some problems with symbolic links. After trying to make symbolic links to work and failing, I decided to use public disk in development and local disk in production. php artisan storage:link makes symbolic link from “public/storage” to “storage/app/public”.  When developing on windows-vagrant this command will not work properly,…