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:
Now we have to define the maximum size of the file to be uploaded:HTML 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>
The php files variable that we need are:Code:define("MAX_FILE_SIZE", 10485760); // Maximum file size is 10 mb, 10485760 is in bytes
but these will be also useful:Code:$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
Now we need the main function with wich we will upload the file (move_uploaded_file):Code:$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
This is the full code:Code: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!";
}
I hope as always it will be useful.. Have fun!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>";
}
?>
Bye!![]()
Do you have any opinion or suggestion?
Well done! +rep.
I recommend not using isset to test $_POST values though. I wrote a blog post explaining why here: http://forum.codecall.net/blogs/jord...ing-isset.html
Yet another one
» Simple file upload script using php Tutorials, Scripts, Technology and Interview Tips
pretty simple![]()
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks