Tuesday, July 19, 2011

How to force a file to download in ASP.NET

Sometime you may want to force the user to download a particular file even though browser can open that file type because of any of your business need. This can be easily achieved by adding the content-disposition header in the response of your page. protected void Page_Load(object sender, EventArgs e) { Response.Clear(); Response.AddHeader("content-disposition", "attachment;filename=.gif"); Response.ContentType = "image/GIF"; Response.WriteFile(Server.MapPath(@"~/computer.gif")); Response.End(); }

Thursday, March 18, 2010

Setting up PHP with IIS 6 on Windows XP

This is a small guide on setting up PHP under IIS 6 on a Windows XP/2003 server. If you have any questions or queries about it please don't hesitate to leave me a comment.

Installing PHP

  • Get the latest php (5.2.6 at present) from PHP.net's download page. Download the zip packager rather than the installer.
  • Extract the zip file to c:\php
  • Copy php.ini-dist file to php.ini
  • Edit the c:\php\php.ini file and
    • Set the session.save_path to a temporary directory on your server, you could create a special one for this (e.g. c:\php\session)
    • Set the upload_tmp_dir to a temporary directory on your server, you could create a special on for this (e.g. c:\php\upload)
    • Set the SMTP setting to the ip of your smtp server. It won't work with authentication so make sure you can send from your web server through that smtp server without a user/pass
    • Set the sendmail_from to the email you want all emails from the server to come from
    • Set the upload_max_filesize setting to something a little large (if necessary). Default is 2M. I think 8M is normally ok.
    • Set your extension_dir=C:\php\ext
    • Set post_max_size to at least the size of your upload_max_filesize (+ a little bit more). Default is 8M. 10M should probably be ok if you increased the upload_max_filesize to 8M.
    • Uncomment (by removing the ; at the start of the line)"cgi.force_redirect" and change the value to 0 instead of 1
    • Add these lines to the end of the file
  • Save and exit the file

Setting up IIS and file/directory permissions

  • Now open the IIS management console by typing Start->Run and typing %SystemRoot%\system32\inetsrv\iis.msc
  • Find out what user we need to give write access to by
    • Edit the properties of the web site you are going to install the php application into
    • On the directory security tab, click the edit button under anonymous access and authentication control
    • Copy down the user name under the anonymous access section (it is allowed right ?). It's usually COMPUTER\IUSR_COMPUTER where COMPUTER is the name of the computer running IIS
  • Now right click on the c:\php\session directory and go to properties, click the security tab, click add and in the box type in the name you copied down before.
  • Click check names and it should underline the name, then click ok.
  • Click modify and it should check all the other necessary boxes for you.
  • Click ok
  • If there is no security tab, then in windows explorer
    • Go to the Tools menu, choose 'Folder Options'.
    • Choose the 'View' tab and find the option called 'Use simple file sharing' (usually the last one) and untick it.
    • Click apply, then ok.
    • Go back to the previous step
  • Repeat this for the c:\php\upload directory and any other directories which need to be writeable by your php application.
  • Now go back to the IIS management console, right click on the website you want to setup php for and go to properties.
  • Click the documents tab and click add and type in index.php and click ok. This tells the webserver to look for index.php files for a default file.
  • Now, still in the iis management console, click the home directory tab, click on the configuration button and then click add.
  • Browse to the c:\php\php-cgi.exe file for the executable and .php for the extension. We want it for All Verbs which is checked by default, check script engine and uncheck "Check that file exists" then click ok.
  • Then click ok until all the dialogs are closed and restart your website in the iis control panel by clicking the restart button in the toolbar or by right click and choosing restart from the tasks menu.
  • Now create a file in your web root called info.php and put in it then browse to that file in your web browser via it's url. It should bring up a phpinfo page which will let you check that mysql is enabled, the session.save_path is right etc.