Sunday, May 24, 2009

Built-in DOS Command-Line FTP client and DOS scripts

Did you know that there are two ways that you can use the FTP client (ftp.exe in the c:\windows\system32 directory) that comes packaged with Windows? (Yes, Vista included) This dates back to XP, Win98, and even Win95. From the DOS prompt you can type ftp to get the ftp> prompt, and from that environment you can sequentially type in the ftp commands, OR you can write a batch file to automate the FTP of multiple files using the single DOS command:

c:\>ftp -s:script.txt ftp.your_url.com

The batch file can WRITE the ftp> commands to a text file script.txt, invoke the commands in the file and erase the file to clean up, and can be run from the c:\> prompt by just calling the batch file name (without the trailing .bat). A couple of different syntaxes below gets the job done:

echo userid> script.txt
echo password>> script.txt
echo binary>> script.txt
echo put filetoupload.pdf>> script.txt
echo close>> script.txt
echo bye>> script.txt
ftp -s:script.txt ftp.insertyoururl.com
del script.txt

or

set /p FTPADDRESS=Enter URL or IP Address:
set /p SITEBACKUPFILE=Enter File to Download:
set /p FTPUSERNAME=Enter FTP User Name:
set /p FTPPASSWORD=Enter FTP Password:
>script.ftp ECHO %FTPUSERNAME%
>>script.ftp ECHO %FTPPASSWORD%
>>script.ftp ECHO binary
>>script.ftp ECHO get %SITEBACKUPFILE%
>>script.ftp ECHO close
>>script.ftp ECHO bye
FTP -s:script.ftp %FTPADDRESS%
TYPE NUL >script.ftp
DEL script.ftp

And now for the BUG that took me pretty much an entire day to solve. It is so stupid that no one else has ever done this, therefore I could not find it on the internet. I found that both typing ftp at the dos prompt or running the batch files from the dos prompt caused dos to "hang". As in, the cursor blinks on a new line but nothing happens and no keys allow escape. The final answer was I had inadvertently named my batch file ftp.bat -- HUGE mistake because that name overrides the path to ftp.exe in the windows system directory and essentially causes the cmd utility to crash. Oh boy.

Monday, May 11, 2009

Avoiding Microscopic Web Text on iPhone

You need a meta tag inside your html head tag as follows:

[head][meta name="viewport" content="width=240; user-scalable=false" /][/head]

The tradeoff is: with this meta tag you lose the "pinch" zoom function, but you can control the size of the text really easily with the width parameter. And you can still scroll with your finger.

Embedding JavaScript within PHP Script

Actually the title of this post should be "Embedding PHP variables within JavaScript which is embedded within echoed HTML within PHP."

Consider this block of code:

echo '[tag]body background="../files/backgroundfade1.jpg"
onLoad="document.getElementById(\'search\').focus();
document.getElementById(\'search\').value=\' '.$_POST[search].'\';"
[tag]';

This little block of code solves a really difficult problem, which is how to have a PHP-generated page of code automatically put the cursor (focus) into the search field at the END of the text in the field (not the beginning). Notice that the echoed HTML within PHP has to be enclosed in single quotes, and the JavaScript embedded in the HTML has to be enclosed in double-quotes, and within that JavaScript one needs additional embedded single quotes to enclose names and data. But if I just use the single quotes I inadvertently escape out of my original HTML statement. THE SECRET IS TO USE BACK_SLASHES BEFORE THE EMBEDDED SINGLE QUOTES to preserve JavaScript environment, then you can immediatedly use another single quote to embed a PHP variable surounded by dots, hence the \' '. $PHP_VARIABLE .'\'

It's really obvious once you look at it this way. It took me weeks to find this solution, and it does not seem to be anywhere on the internet, as far as I know.

Here is another crazy JavaScript Gem:

onKeyup="var t=setTimeout(\'document.list.submit()\',0);"

[Again, note the back slashes because this is embedded within HTML embedded within PHP again, I just left all that out this time.] The setTimeout() to zero milliseconds before running the submit() method gets around a BUG in iPhone Safari Browsers which submits POST requests with the submit() method without capturing the contents of the text field. With the timeout function it somehow forces the BUGGY iPhone browser to submit the data in field with the POST.