Archive

Author Archive

Absolute and relative paths with FTPWebRequest

March 7th, 2010 Alvos 2 comments

After persuading my client to use explicit SSL :) I ran into another very curious issue that I would like to share with you and maybe it will help you solve a similar problem.

When testing the solution I had to download the files inside a specific path of an FTP location. After downloading the files, I told my client that the solution was working fine but he replied telling me that the files were still in the path. I checked the path using my solution and the files weren’t there! All the files were downloaded to the destination folder and I could see them clearly.

Always blaming the user – as we engineers usually do – I asked my client to share his desktop so that I could see the folder he was looking at and make sure he was looking at the right path. What was my surprise when I saw the “same” folder I was looking at in my computer with a bunch of files pending to be downloaded.

After a couple of minutes scratching my head I realized that we were looking at two different folders using a similar path structure. But how was this possible? I hope I can make it clear enough for you.

Actually, my FTP solution was downloading the files from a relative path. As an example, I was told that after logging to the FTP location, I needed to go to the path “example1/download” to download the files. When I logged in using using my solution I didn’t realize that I was actually being redirected to a folder created for the credentials I was using when logging to the FTP location. As a result, I thought I was accessing the path

ftp://ftplocation.com/example1/download

when I was actually accessing the path

ftp://ftplocation.com/user1/example1/download

because of the automatic redirection to the user folder depending on the credentials used to access the FTP location.

I was having the problem because unfortunately, the FTP location happened to have both paths available – go figure! – and my client was looking at the absolute path – in the example above ftp://ftplocation.com/example1/download.

If you’re having the same problem you need to tell the FTPWebRequest instance that you will be using and absolute path. As the FTWebRequest class has its foundations in the HTTPWebRequest class you are not capable of controlling your FTP session, so you won’t be able to redirect to the root folder – in the example above ftp://ftplocation.com- and redirect to your specific path from there. Fortunately, the geniuses at Microsoft thought of a workaround to tell the FTPWebRequest class you will be using and absolute path instead of your default redirection or relative path.

The following code shows how to do that:

1
2
Uri target = new Uri ("ftp://ftplocation.com/%2fexample1/download");
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(target);

I know “%2f” is an escaped “/” but that’s how the class can tell if it will be using an absolute path instead of a relative one.

It’s simple right? Just add “%2f” before the specific path and you will make sure you are accessing the absolute path. Sometimes you will need  to use a relative path so it’s better if you make that option configurable – maybe a checkbox where you can choose between relative or absolute paths.

Ok, another workaround for the FTPWebRequest class that could save you some trouble.

Cheers!

FTPWebRequest with implicit SSL: NOT POSSIBLE

March 7th, 2010 Alvos 2 comments

Some time ago I had to develop a web FTP solution for a client and as a client’s choice, I had to use ASP.NET 3.5 to do it. I never liked Visual Basic.NET that much so I always use C# when I need to develop .NET applications.

I thought of using the FTPWebRequest class to develop the solution and everything was going fine until I had to use the solution to create a connection to an FTP location that was secured using implicit SSL.

I googled and googled about it and finally I found out that our friends at Microsoft decided NOT to support implicit SSL for the FTPWebRequest class. This is another reason that makes me want to meet the guy who designed this class and slap him in the face. This and the fact that the FTPWebRequest class is based on the HTTPWebRequest class without taking in account that FTP and HTTP are protocols that work in a completely different way, hence, the FTPWebRequest class lacks of a lot of features that could make it a powerful FTP tool.

Having said that, using FTPWebRequest makes it easy to create FTP solutions once you make sure you WON’T need to connect to FTP locations that are secured by implicit SSL. If you are one of the unfortunate souls that need to support this type of security, I’m sorry to say it but you will need to use – as primitive as it sounds – sockets so don’t even bother using FTPWebRequest since you would be doing the work twice.

FTPWebRequest does support explicit SSL so if you  can persuade your client to use explicit SSL in his FTP locations, go for FTPWebRequest since it’s very easy to use.

Cheers!

Using Zend_Db_Select without specifying a database adapter

March 7th, 2010 Alvos No comments

I’ve been working with the Zend_Db_Table object when I want to get one or multiple records from a database table by using fetchAll() or find() methods. The latest versions of Zend Framework – I’m currently using 1.10.2 – come with the following warning: “The API for fetch operations has been superseded to allow a Zend_Db_Table_Select object to modify the query. However, the deprecated usage of the fetchRow() and fetchAll() methods will continue to work without modification.”

The warning says that the deprecated usage of both methods will continue to work without modification but as I’ve had bad times when I’ve trusted these “promises” because deprecated features usually become unmaintained and eventually disappear, I strongly recommend to modify your code to use the latest features offered.

If you follow this link you’ll find how to use the Zend_Db_Select object and in all the examples you need to create and instance of the Zend_Db object in order to use the select object, as in the following example:

1
2
$db = Zend_Db::factory( ...options... );
$select = new Zend_Db_Select($db);

The problem is that most of us never actually create an instance of the Zend_Db object and I don’t recommend it since it’s not configurable at all. As powerful as Zend Framework is, the zf tool it provides, allows us to create database connections that can be used globally and are configurable through the application.ini file. In case you don’t know how to do that here’s an example of how to create a connection to a MySQL database:

How to create a database connection using zf tool

How to create a database connection using zf tool

If you didn’t understand the code in the image you might need to go through the quick start again.

After you have created a database connection using the zf tool, you will be able to use it globally so here’s an example of how to use Zend_Db_Select without having to create an instance of the Zend_Db object:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public function testDBSelect($value)
{
 $select = $this->getDbTable()->select()->from('tbl_example')->where('field = ?', $value);
  
 $stmt = $select->query();
 $result = $stmt->fetchAll();
  
 if (0 == count($result))
 {
  return;
 }
  
 $row = $result[0];
  
 echo($row['Field1']);
 echo($row['Field2']);
}

Basically, you get the instance of the Zend_Db_Select from a Zend_Db_Table object.  The results from the select come in the form of a matrix where the rows are  the records gotten from the table and the columns are the field values  per row.

You would only need to go through the results variable using a basic cycle and access the field values like a normal array $row['field1'], $row['field2'], etc.

Well, for all the ones not being able to solve this problem, there you go.

Cheers!

Why do we have a category for the “geek stuff”?

March 7th, 2010 Alvos No comments

Hello all,

There are thousands of sites that talk about issues or share tutorials that teach you about different technologies and accessing to our site could make you think that it’s just about the same stuff. Trust me, I thought about creating this section a lot of times before actually having the guts to do it.

Actually, I created this section because I’m a guy that spends about 16 hours a day working on something that has to do with sofware development or technology in general and during that time, I have to deal with issues or subjects that aren’t just that easy to resolve just by googling them. Some of them have given me a really hard time and with some I’ve spent about 2 or 3 days before I find the solution that tipically results in a stupid thing.

I’m really busy most of the time and I hate to wait for someone to answer one of my questions in a popular technology forum. Not that I’m ungrateful, but it’s just that it could be much easier if someone had already published what I need. I know most of you agree with me on that :)

After all that being said, I thought about sharing the solution to issues that have given me a hard time so you don’t have to spend more that 10 minutes in something that otherwise could take you hours or days.

I’m not promising detailed tutorials about every topic – I will create them if I have the time – but at least I will publish articles with solutions to questions that are not that easy to find answers for, on the web.

Also, if you have questions or have any issue you’ve spent days on without finding the solution, you are more than welcome to shoot me an email and we can work on it together. Trust me, with all the time I spend in front of the computer, I’ve become very good at solving problems :)

Well, I hope you find something useful inside this section and become a frequent visitor.

Cheers! ;)

Categories: Geek stuff Tags: