PHP – How to read a file from another server using FTP credentials?

I had a task to read file from another server. Obvious you would suggest file_get_contents or cURL for same. But those files was not accessible through URL. I have to read it using FTP server credentials, Initially I was trying with ftp_get and some other FTP functions. But it was download all files to my local server.

But finally I got the solution, here its….

<?php
    $filename = 'ftp://username:password@hostname/files/path/and/name.txt';
    $handle = fopen($filename, "r");
    $contents = fread($handle, filesize($filename));
    fclose($handle);
    echo $contents;
 ?>

I hope this would help you….

Advertisements

Magento – Blank white screen frontend issue

Magento shows blank/empty page, I was scared when this happened on one of the live site but after some exercises with source code and using internet I come to find out the solution. Most probably it could have memory issue or the compilation tool.

In order to troubleshoot the issue,  your first step is to edit index.php, find out bellow line and uncomment

ini_set('display_errors', 1);

If its not available, insert this line somewhere at the top of the file and try out bellow solutions.

Try – 1 : Disable Complier

Go to System -> Tools -> Compilation and Disable you Complier and clear/flush magento cache data. In most of the cases this would solve the issue.

Try – 2 : Improve PHP Memory Limit

If you can access php.ini OR using .htaccess in the root OR edit index.php add bellow line right after ini_set(‘display_errors’, 1);

ini_set('memory_limit', '256M');

Try – 3 : Design changes

If you have recently installed new theme try to switch interface to a previously used one. Flush Magento cache after each operation. Magento will switch to default theme if it can not find a custom one.

I hope these would resolve  your problem.

Git – File Rename

We have seen the basic commands in Git – Setup repository and basic operation

Let add something more in to that, how to rename file with git svn. Here is the command for that,

git mv oldname newname

i.e.

git mv -f event.php Event.php

here -f stand for force, Force renaming file even if the target exists.

Basically above command line will just do,

git add newname
git rm oldname

git mv command is also use to move file from one to another location. As bellow,

git mv <source> <destination>

Uploads file to FTP server programmatically using PHP script

PHP it self provide us the FTP functions library which allow to implement client access to files servers (FTP). This library providing a wide range of control to executing script including read or write file to FTP server. This post will particularly post show you how to Send Files to send file using php script. Here is the  script which allow you to upload file to FTP server,

<?php
    // Connect to server
    $conn = ftp_connect("ftp.yourserver.com") or die("Could not connect");

    ftp_login($conn,"username","password") or die("invelid username or password");

    // move to path where you need to upload file
    ftp_chdir($conn, "/upload/") or die("could not find dir");

    // upload file to particular path
    $upload = ftp_put($conn,"allproduct.xml","allproduct.xml",FTP_BINARY);

    if (!$upload) { echo 'FTP upload failed!'; }

    ftp_close($conn);
?>

Some time you need to create dir dynamically on server, in that case you can use bellow code instead of change dir,

if(!ftp_chdir($conn, $folder_path))
{
    $create_folder = ftp_mkdir($conn, $folder_path);
    ftp_chmod($conn, 0777, $folder_path);
    ftp_chdir($conn, $folder_path);
}

Above part of script will check for particular dir. If it could not find, it will create that dir and moved to particular dir path. Then you can continue to upload part.

Git – Setup repository and basic operation

Since few days I am working on GIT version system. I have mostly worked on SVN system for windows, and its easy to work with as its providing GUI. Its bit difficult to work with GIT (only for initial level).

Today I would like to share some basic knowledge which can help you to start with Git. Some basic commands like Setup repository, push code, add new files, fetch new changes etc…

Setup repository,

ssh git@example.com
mkdir my_project.git
cd my_project.git
git init --bare
git update-server-info # If planning to serve via HTTP

From local machine, add code / initial push

cd my_project
git init
git add *
git commit -m "My initial commit message"
git remote add origin git@example.com:my_project.git
git push -u origin master

Its done! repository is created.

Now project contributor can clone and track remote repository (as we do checkout in SVN)

git clone git@example.com:my_project.git
cd my_project

Adds your new or modified files to the queue to be committed later. Files are not committed yet

git add

Delete file which are not more required in directory

git rm file.php

Commits the files that have been added and creates a new revision with a log.

git commit

If you do not add any files, git will not commit anything. You can combine both actions with git

commit -a

Push your changes to the remote repository.

git push

Update your local copy of a remote branch. This operation never changes any of your own branches and is safe to do without changing your working copy.

git fetch

Pull is context sensitive, so Git will merge any pulled commits into the branch you are currently working in. pull automatically merges the commits without letting you review them first. If you don’t closely manage your branches you may run into frequent conflicts.

git pull