How to upload files in PHP?
Following example shows you how to upload files using php.
When you are working with web applications, you may want to upload files to your web server. In php you can upload files very easily. But there are some configuration settings you have to check before you upload a file. Remember that you have to set the form attribute "enctype" to multipart/form-data in your html file. It is a good practice that use separate folder for keep uploaded file in your web server. Also you can create sub folders for keep images, csv files, audio file, etc. Before you upload file check that folder exist in the server. Also make sure that the folder permission is 0777.
Remember that what are the file types that allow user to upload. Never allow users to upload files containing scripts such as file with .php, .asp, .aspx, .js, etc. extensions. If a user upload such a file it will be a big security risk to your web site. So make sure what are the file types allow to upload. First you have to filter files by extension to avoid this security risk. Also you have to check the files that already exists in the server with the same name. Otherwise previous file will be overwrite from the new one.
Remember that what are the file types that allow user to upload. Never allow users to upload files containing scripts such as file with .php, .asp, .aspx, .js, etc. extensions. If a user upload such a file it will be a big security risk to your web site. So make sure what are the file types allow to upload. First you have to filter files by extension to avoid this security risk. Also you have to check the files that already exists in the server with the same name. Otherwise previous file will be overwrite from the new one.
Demo- File uploading using PHP.
Try It!
Configuration settings associated with file upload in PHPThere are some configuration settings in the php.ini file associated with file uploading.
| |||||||||||||||||||||
$_FILES['my_file']['x'] is an associative array in php. It contains details of the uploaded file such as file name, file size and file type. First file is uploaded to the temporary directory in the server machine as a temporary file. such as "C:\temp PHP move_uploaded_file() uploaded function copied the uploaded temporary file to the save location. Temporary file is deleted after the upload. | |||||||||||||||||||||
|
1. Example code for upload files using PHP
HTML Code<html>
<head>
<title>File Upload in PHP </title>
</head>
<body>
<head>
<title>File Upload in PHP </title>
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data" name="file_upload_form" id="file_upload_form">
<table border="0">
<tr>
<td width="94">Select File </td>
<td width="96"><input type="file" id="my_file" name="my_file" /></td>
</tr>
<tr>
<td> </td>
<td>
<input name="btn_upload_file" type="submit" id="btn_upload_file" value="Upload" />
</td>
</tr>
<table border="0">
<tr>
<td width="94">Select File </td>
<td width="96"><input type="file" id="my_file" name="my_file" /></td>
</tr>
<tr>
<td> </td>
<td>
<input name="btn_upload_file" type="submit" id="btn_upload_file" value="Upload" />
</td>
</tr>
</table>
</form>
</body>
</html>
copy above code into a notepad and save it as upload.html and copy following code into another notepad and save it as upload.php </body>
</html>
PHP code
<?php
if($_FILES['my_file']['error']==0){
$file_name = $_FILES['my_file']['name'];
$file_type = $_FILES['my_file']['type'];
$file_size = $_FILES['my_file']['size']/1024;
$temp_path = $_FILES['my_file']['tmp_name'];
$file_save_path="uploaded_files/".$file_name;
$uploaded = move_uploaded_file($temp_path,$file_save_path);
if($uploaded==true){
print("The file ".$file_name." has been uploaded successfully");
print("<br/> File Type : ".$file_type);
print("<br/> File Size : ".$file_size. "Kb");
}else{
}
}else{
print("Error while uploading the file ".$_FILES['my_file']['error']);
}
?>
if($_FILES['my_file']['error']==0){
$file_name = $_FILES['my_file']['name'];
$file_type = $_FILES['my_file']['type'];
$file_size = $_FILES['my_file']['size']/1024;
$temp_path = $_FILES['my_file']['tmp_name'];
$file_save_path="uploaded_files/".$file_name;
$uploaded = move_uploaded_file($temp_path,$file_save_path);
if($uploaded==true){
print("The file ".$file_name." has been uploaded successfully");
print("<br/> File Type : ".$file_type);
print("<br/> File Size : ".$file_size. "Kb");
}else{
}
}else{
print("Error while uploading the file ".$_FILES['my_file']['error']);
}
?>
©-Copyright By Duminda Chamara
JavaScript Validation
JavaScript Validation
0 comments:
Post a Comment