
In this php and mysql tutorial we will learn how to upload file in php to the server, file can be of any type for example jpg, png, mp4, avi,mkv or etc.
First of all we will create a Html form.Html form will contain of a form tag which contain attribute “method” equal to post and “enctype” equal to multipart/form-data which specifies which content type to use when submitting the form. Without this attributes the form will not work to upload file to server.
Next in the form is input type file which contain browse button which can be used to open up a dialog box which allow user to select file to upload.Next is the submit button when user click on the submit button the form will submit data to the server.
Learn how to upload image in php and store in database
https://www.bloggingfist.com/how-to-upload-image-in-php-and-sote-in-database/
Learn how to upload image in php and store in database
https://www.bloggingfist.com/how-to-upload-image-in-php-and-sote-in-database/
Html code to Create a form.
Learn how to create custom html contact form in php <form action="" method="post" enctype="multipart/form-data">
<table class="table table-bordered">
<tbody>
<tr align="center">
<td style="padding-top:30px"><strong>Upload Report For Test :</strong>
<td>
<div class="form-group"><span id="file_error"></span>
<input type="file" name="file" id="file" class="form-control span12">
</div>
</td>
</tr>
<tr align="center">
<td colspan="2"><input type="submit" value="Upload File" name="submit" class="btn btnSubmit btn-primary btn-lg"></td>
</tr>
</tbody>
</table>
Now processing the form with php. Create a php file with extension .php. Now start php script to upload file to the server in php. if condition is used when a submit button is clicked by the user. Define directory “upload” to save the uploaded file there. $dir varible contain the the “upload” directory where we upload file to save. After that create a superglobal variable to assign value of file filed. $_FILE[] is an associative Array.
Now check for the upload file that user upload a file a empty. If upload file is not empty then we check that file is already uploaded or not.if yes then we rename the file by adding time with time() function. Now create a $fdir variable which is the full directory of uploading file. . temp folder is contain inside the www folder. when we upload a file it temporarily upload to temp file and then we move the file form temp directory to the directory we define.
Now move file from temp folder to directory we difine by using the php function move_uploaded_file. At last execute the query to save the file name to database.
Php Code to upload a file to serve
if(isset($_POST['submit'])
{
$dir="uplaod/";
$file=$_FILES["file"]["name"];
$tmpname=$_FILES["file"]["tmp_name"];
if(empty($file)
{</br>
$error='Upload Report';
}else{
if($file!="")
{
if(file_exists($dir.$file))
{
$file=time().'_'.$file;
}
}
$fdir=$dir.$file;
move_uploaded_file($tmpname,$fdir) or die(mysql_error());
mysql_query("insert into `tests` values('$file')") or die(mysql_error());
I hope you enjoyed learning about how to upload file in php to server.
Yanbu Gypsum Board Decor Yanbu Gypsum Board Decor Yanbu Gypsum Board Decor Yanbu Gypsum Board Decor Yanbu Gypsum Board Decor Yanbu Gypsum Board Decor Yanbu Gypsum Board Decor Yanbu Gypsum Board Decor Yanbu Gypsum Board Decor Yanbu Gypsum Board Decor Yanbu Gypsum Board Decor Yanbu Gypsum Board Decor Yanbu Gypsum Board Decor Yanbu Gypsum Board Decor Yanbu Gypsum Board Decor Yanbu Gypsum Board Decor Yanbu Gypsum Board Decor Yanbu Gypsum Board Decor Yanbu Gypsum Board Decor Yanbu Gypsum Board Decor Yanbu Gypsum Board Decor Yanbu Gypsum Board Decor Yanbu Gypsum Board Decor Yanbu Gypsum Board Decor Yanbu Gypsum Board Decor Yanbu Gypsum Board Decor
Blogger Comment
Facebook Comment