In this tutorial you are going to learn , How to upload files using PHP and moves uploaded files into the specified files folder, Using some PHP global variable like $_FILES['controller_name'] , move_uploaded_files() , you can move uploaded file into the folder it's easy, for that we have to create html form and embed few lines of PHP code into the single file, let's have a look.
1 . file_uploads = On
2 . upload_max_filesize = 50M
upload_max_filesize helps you to maximize file uploading size, by default it is 2, you can maximize as your need.
Read also : File uploading and View Script with PHP & MySQL
Make Sure : 1 . Method="POST" : it should be POST. 2 . enctype="multipart/form-data" : it's specify content-type to be uploaded.
3 . type="file" : attribute of the
here in this following script javascript was just used for message that file was upload or not.
put this script just above
PHP Script explained :
1 . $folder : Directory where files are uploaded.
2 . move_uploaded_files : php function that moves selected file to the specified folder.
3 . rand() : this is the awesome function that enables you to upload same files multiple times.
index.php
copy-paste the above script and try to upload any file, You can use this for Uploading image's ,PDF's,MP3,Video, Doc any file types make sure you change the necessary parts in the script.
that's it
Configure The "php.ini" File
open your php.ini file and search for following keys and make some changes as i change like below :1 . file_uploads = On
2 . upload_max_filesize = 50M
upload_max_filesize helps you to maximize file uploading size, by default it is 2, you can maximize as your need.
Read also : File uploading and View Script with PHP & MySQL
The html form :
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>File Uploading With PHP and MySql</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="pic" />
<button type="submit" name="btn-upload">upload</button>
</form>
</body>
</html>
Make Sure : 1 . Method="POST" : it should be POST. 2 . enctype="multipart/form-data" : it's specify content-type to be uploaded.
3 . type="file" : attribute of the
input
tag shows input field as a file selector with a "Browse" Button.The PHP
We will use isset($_FILES['name_of_input']) to make sure some file is selected.here in this following script javascript was just used for message that file was upload or not.
put this script just above
<!DOCTYPE html>
tag
<?php
if(isset($_POST['btn-upload']))
{
$pic = rand(1000,100000)."-".$_FILES['pic']['name'];
$pic_loc = $_FILES['pic']['tmp_name'];
$folder="uploaded_files/";
if(move_uploaded_file($pic_loc,$folder.$pic))
{
?><script>alert('successfully uploaded');</script><?php
}
else
{
?><script>alert('error while uploading file');</script><?php
}
}
?>
PHP Script explained :
1 . $folder : Directory where files are uploaded.
2 . move_uploaded_files : php function that moves selected file to the specified folder.
3 . rand() : this is the awesome function that enables you to upload same files multiple times.
Complete Script
index.php
<?php
if(isset($_POST['btn-upload']))
{
$pic = rand(1000,100000)."-".$_FILES['pic']['name'];
$pic_loc = $_FILES['pic']['tmp_name'];
$folder="uploaded_files/";
if(move_uploaded_file($pic_loc,$folder.$pic))
{
?><script>alert('successfully uploaded');</script><?php
}
else
{
?><script>alert('error while uploading file');</script><?php
}
}
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>File Uploading With PHP and MySql</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="pic" />
<button type="submit" name="btn-upload">upload</button>
</form>
</body>
</html>
copy-paste the above script and try to upload any file, You can use this for Uploading image's ,PDF's,MP3,Video, Doc any file types make sure you change the necessary parts in the script.
that's it
please help i cant even upload an MP3 or video files. please help
ReplyDeletesearch for 'upload_max_filesize' in 'php.in' file and change it's value as you'r need, by default it's 2M
Deletefor video set the value around 100M.
download the file and try to upload mp3 and video files
can i ask the php.in file ? i cant find it, i click the download script to download inside got this few file only
Deletedbtuts.sql
uploads
dbconfig.php
index.php
style.css
upload.php
view.php
can i which is php.in ? or no in inside?
hi,can i ask where the php.lni file? i download the script from this link
Deletehttps://codingcage.com/2014/12/file-upload-and-view-with-php-and-mysql.html
and i open only see this few file
dbtuts.sql
uploads
dbconfig.php
index.php
style.css
upload.php
view.php
icannot download >.<
Deleteit shows error while uploading files greater than 1M.
ReplyDeletei have tried changing the 'upload_max_filesize' in 'php.in' tp 50M
still its not working. plz help.
thanks in advance
check your upload limit increased or not, after increasing click on restart all services of wamp server.
DeleteHi. How stop alert when page with this script is loaded?
ReplyDeleteI need reload (location.reload();) after upload but if work with it like loop ;/
All time page is loaded I see "successfully uploaded"
Just remove the JavaScript Code...
DeleteHi, i have a form that adds data to the database and i also want the user to upload an image of themselves. So my question, how do i combine this tutorial with other input types like Text and Date. And thanks for these tutorial, PHP PDO CRUD Tutorial using OOP with Bootstrap. That is what i am using to add data to the database. its a great resource, really appreciate it. Thanks again
ReplyDeletecheck the following tutorial :
Deletehttps://codingcage.com/2014/12/file-upload-and-view-with-php-and-mysql.html
in this tutorial i have stored file type and size, same you can insert users image with their name(TEXT) and date just change sql insert query and insert image with text and date as i showed in CRUD tutorial just insert image ...
Code works great but I cannot get it to upload PDF documents. TXT files work fine but PDF documents will not insert into the database. They upload to the folder but not the database.
ReplyDeleteCode works great but I cannot get the files to add PDF documents. The PDF file gets uploaded to the uploads folder but the record does not get added to the database.
ReplyDeleteHello thomas, this was just for uploading file to the directory/folder
Deletecheck out this tutorial : Simple Image Upload, Insert, Update, Delete using PHP MySQL
ok. I'll check it out. For this script here when it creates the reference in the List page none of the PDF documents show. I am trying have an Insert Record Page where the user can add text to 4 fields and add a PDF that will display in the list page where users can click on the link to view it.
ReplyDeleteThanks for this! I have combined this with another tutorial on here and it's working great. One question, how can I upload multiple files at once?
ReplyDeleteI already change the upload_max_filesize in php.in. I got this error "Warning: POST Content-Length of 19837945 bytes exceeds the limit of 8388608 bytes in Unknown on line 0" when I upload the mp4
ReplyDeletePlease help me
how can i upload multiple images like i have 4 file types in one form through obeject oriented
ReplyDelete