# How to upload images using PHP

**URL:** <https://forum.kirupa.com/t/how-to-upload-images-using-php/294043>\
**Category:** programming\
**Created:** [August 7, 2009, 12:22pm UTC](https://forum.kirupa.com/t/how-to-upload-images-using-php/294043 "2009-08-07T12:22:35Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnfrey](https://avatars.discourse-cdn.com/v4/letter/j/e56c9b/32.png) [@johnfrey](https://forum.kirupa.com/u/johnfrey)\
**Post date:** [August 7, 2009, 12:22pm UTC](https://forum.kirupa.com/t/how-to-upload-images-using-php/294043/1 "2009-08-07T12:22:35Z")

</div>

Uploading a file is a basic requirement of most of the websites. In this post, I will explain in detail about how to upload an image using PHP.

First of all, we will add HTML code to display the browse button to upload an image:

```php
<FORM ENCTYPE="multipart/form-data" ACTION="_URL_" METHOD=POST>
Upload this file: <INPUT NAME="userfile" TYPE="file">
<INPUT TYPE="submit" VALUE="Send File"></FORM>

```

This code will display a text area with a browse button to upload an image. Then I’ll add PHP code for processing of file upload:

```php
if ($userfile_size >250000){$msg=$msg."Your uploaded file size is more than 250KB so please reduce the file size and then upload. Visit the help page to know how to reduce the file size.<BR>";$file_upload="false";}

```

Now, we will check that only jpeg or gif files can be uploaded into our server:

```php
if (!($userfile_type =="image/pjpeg" OR $userfile_type=="image/gif")){$msg=$msg."Your uploaded file must be of JPG or GIF. Other file types are not allowed<BR>";$file_upload="false";}

```

Finally, running this script will add the file to the mentioned directory:

```php
if(move_uploaded_file ($userfile, $add)){
// do your coding here to give a thanks message or any other thing.}else{echo "Failed to upload file Contact Site admin to fix the problem";}

```
