• Home
  • Programming
    • API
      • Google
    • Javascript
    • Php
    • Server
  • CMS
    • Magento
    • Yahoo! Store

Save remote image to server with PHP

Monday, 28 October 2013 20:12
Tim Ramsey
0 Comments
I was working on a project where I was using an API to connect to Amazon to pull book details via ISBN. I needed to store the remote images on my server to be associated with the product that was being created and I put together these few lines of code to do so. It obviously will need to be reworked for your own needs, but the logic is below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//Remove Image Url
$imageUrl = "http://www.removeimageurl.com/some/remote/image.jpg";
//Get File Extension
$extension = substr(strrchr($imageUrl,'.'),1);
//This is the name you will give the image
$name = $wholedata["name"];
//Remove all the junk from the $name
$match = array("/\s+/","/[^a-zA-Z0-9\-]/","/-+/","/^-+/","/-+$/");
$replace = array("-","","-","","");
$newName = preg_replace($match,$replace, $name);
$newFilename = strtolower($newName.'.'.$extension);
//New File name with new Extension
$extension = substr(strrchr($filename,'.'),1);
$new_file_name = strtolower('main_'.$newName.'.'.$extension);
//Put the remove file in current directory of server.
file_put_contents($new_file_name, file_get_contents($imageUrl));

———————————————————————-
If you wanted to use directories on server you could (remove the last line above and) do something like

1
2
3
4
5
6
7
8
9
10
// Set server Path correctly
$saveDir = $_ENV["DOCUMENT_ROOT"]."/images/";
//Assign Write Privileges to Directory
chmod($saveDir,0777);
//If chmod worked, and it is writable then write else fail
if(is_writable($saveDir)) {
    file_put_contents($saveDir . $new_file_nam, file_get_contents($imageUrl));
} else {
     exit("Failed to write to directory ".$saveDir);
}


This WordPress code tag BS is pissing me off and I will be finding a plugin to allow all this to be copied and pasted easily 🙂

Magento + Get Backend Variables = getStoreConfig

Saturday, 26 October 2013 22:41
Tim Ramsey
0 Comments
Well I had finished a custom PayPal payout module for a client in my test environment, and had to incorporate it into the Magento interface. After research, I found the code to grab the information I needed was

Mage::getStoreConfig('sectionName/groupName/fieldName');

I thought the next logical step for me was to go into the XML file in the CORE PayPal model to find the ‘sectionName/groupName/fieldName’ hierarchy in the config file. After being frustrated with how nested the config file was, I turned to the database core_config_data, which quickly gave me the information I was looking for.

core_config_data

Now I can use getStoreConfig to extract the data

1
2
3
4
5
$sandbox = Mage::getStoreConfig('paypal/wpp/sandbox_flag');
$usernameUn = Mage::getStoreConfig('paypal/wpp/api_username');
$passwordUn = Mage::getStoreConfig('paypal/wpp/api_password');
$sigUn = Mage::getStoreConfig('paypal/wpp/api_signature');
$environment = ($sandbox == 0 ? 'live' : 'sandbox');

Page 15 of 15

  • Previous
  • 1
  • …
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

Categories

  • CMS
    • Magento
    • Shopify
    • Yahoo! Store
  • Programming
    • API
      • Google
    • Javascript
    • Php
    • Server
    • SVN
  • VOIP

Recent Posts

  • How to write to Google Sheet with API in PHP
  • How to Migrate FreePBX server to a new server
  • Unifi Controller Java CPU 100%
  • Update Unifi Controller on Linux
  • Code Signing Desktop App