Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Simple file uploader

max_file_size

  • Please log in to reply
3 replies to this topic

#1 Jacko

Jacko

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 78 posts
  • Learning:C, Java, C++, PHP

Posted 24 October 2009 - 01:12 PM

Hello, today I'm going to explain you how you can make a simple file uploader.

First you have to know that the maximum upload size is definited in the php.ini (upload_max_filesize) of your host, so if you want to upload bigger files you have to change the upload_max_filesize value.

Ok, we're ready to start to write our file uploader:
The most important thing is the form with wich we will send our files to the php code:
<form method=\"post\" action=\"upload.php\" enctype=\"multipart/form-data\">
File to upload: <input type=\"file\" name=\"file\"> <input type=\"submit\" name=\"submit\" value=\"Upload\">
</form>

Now we have to define the maximum size of the file to be uploaded:
define("MAX_FILE_SIZE", 10485760); // Maximum file size is 10 mb, 10485760 is in bytes

The php files variable that we need are:
	
$file_name = $_FILES['file']['name']; // File name
$file_size = $_FILES['file']['size']; // File size
$file_temp = $_FILES['file']['tmp_name']; // Our temporaney file
$file_dest = "files/".basename($file_name); // File destination

but these will be also useful:
$file_type	= $_FILES['file']['type']; // The file type, if we want to upload a determinated type of file
$file_error = $_FILES['file']['error']; // The files errors

Now we need the main function with wich we will upload the file (move_uploaded_file):
	if ($file_size <= MAX_FILE_SIZE) {
// Uploading file
if(move_uploaded_file($file_temp, $file_dest))
echo 'File uploaded: '.$file_dest;
else
echo 'Error: '.$file_error;
} else {
echo "This file is too big!";
}

This is the full code:
<?php
define("MAX_FILE_SIZE", 10485760); // Maximum file size is 10 mb, 10485760 is in bytes

if(isset($_POST['submit'])) {
// Files variables
$file_name = $_FILES['file']['name']; // File name
$file_size = $_FILES['file']['size']; // File size
$file_type = $_FILES['file']['type']; // The file type, if we want to upload a determinated type of file
$file_temp = $_FILES['file']['tmp_name']; // Our temporaney file
$file_error = $_FILES['file']['error']; // The files errors
$file_dest = "files/".basename($file_name); // File destination

if ($file_size <= MAX_FILE_SIZE) {
// Uploading file
if(move_uploaded_file($file_temp, $file_dest))
echo 'File uploaded: '.$file_dest;
else
echo 'Error: '.$file_error;
} else {
echo "This file is too big!";
}

} else {
// Uploading form
echo "<form method=\"post\" action=\"upload.php\" enctype=\"multipart/form-data\">
File to upload: <input type=\"file\" name=\"file\"> <input type=\"submit\" name=\"submit\" value=\"Upload\">
</form>";
}
?>

I hope as always it will be useful.. Have fun!

Bye! :thumbup1:
  • 0

#2 Jacko

Jacko

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 78 posts
  • Learning:C, Java, C++, PHP

Posted 28 October 2009 - 02:58 AM

Do you have any opinion or suggestion?
  • 0

#3 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 28 October 2009 - 03:14 AM

Well done! +rep.
I recommend not using isset to test $_POST values though. I wrote a blog post explaining why here: http://forum.codecal...sing-isset.html
  • 0

#4 curiousplayer2003

curiousplayer2003

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 27 June 2010 - 10:14 AM

Yet another one
ยป Simple file upload script using php Tutorials, Scripts, Technology and Interview Tips
pretty simple :)
  • 0





Also tagged with one or more of these keywords: max_file_size

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download