Enhancing Websites with PHP

File Uploads

Allowing file uploads is the focus of this unit, which is a common feature in social networking and file sharing websites. There are several caveats you need to be aware of when allowing file uploads. First, you need to restrict the type(s) of file formats you allow. Second, you need to set a maximum size of each file. For a photo gallery you are advised to limit the file types to JPEG or PNG and the size to no more than 512KB. Lastly, you need to carefully consider what file naming convention you should use. Since you have no control over how guests name their photos and you want to avoid overwriting existing photos, your upload process needs to rename each file in a way that relates the file to the guest who uploaded it.

Knowing how to program a photo upload feature is a great skill to have, though it is not enough. You also need to learn how display the photos on your website. An ideal way to do so is by adding a bit of JavaScript to your application. JavaScript is a popular client-side language that runs natively in the browser. It allows you to do things that neither HTML nor PHP can, though it integrates seamlessly with both. You can write JavaScript with PHP in the same way you’ve been writing HTML with PHP. For this unit, you will learn how to create an external JavaScript file containing a single function that retrieves the name of the image along with its width and height. It will then create a pop-up window accordingly and display the image.