PHP Limit upload file size

This upload form can limit upload file size.


In this tutorial, you have to create 2 files for testing code.
1. limit_upload.php
2. limit_upload_ac.php

1. Create file limit_upload.php
2. Create file limit_upload_ac.php
3. Create folder "upload" for store uploaded files.
4. CHMOD your upload folder to "777" by using your ftp software(change permission).

STEP1: Create file limit_upload.php

############### Code

<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<form action="limit_upload_ac.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<td><strong>File Upload (Limit file size 50 K)</strong></td>
<td align="center">Select file
<input name="ufile" type="file" id="ufile" size="35" /></td>
<td align="center"><input type="submit" name="Submit" value="Upload" /></td>

STEP2: Create file limit_upload_ac.php

############### Code


// Define file size limit

//set where you want to store files
//in this example we keep file in folder upload
//$HTTP_POST_FILES['ufile']['name']; = upload file name
//for example upload file name cartoon.gif . $path will be upload/cartoon.gif

$path= "upload/".$HTTP_POST_FILES['ufile']['name'];

if($ufile !=none)

// Store upload file size in $file_size

if($file_size >= $limit_size){
echo "Your file size is over limit<BR>";
echo "Your file size = ".$file_size;
echo " K";
echo "<BR>File size limit = 50000 k";
else {

//copy file to where you want to store the file
if(copy($HTTP_POST_FILES['ufile']['tmp_name'], $path))
echo "Successful<BR/>";
echo "<img src=\"$path\" width=\"150\" height=\"150\">";
echo "Copy Error";

STEP3: CHMOD upload folder to 777 (change permission)

This step, do it when you upload to real server. This example, I use WS-FTP, right click at upload folder > FTP Commands > CHMOD(Unix)

Similar Post

Making number format like 0001, 0002

In this tutorial we'll make number format like 0001, 0002, 0003 in stead of 1, 2 ,3. It's very easy to do this

PHP User online tutorial

This tutorial show you php script that count how many users are active on your site.