How to upload files in PHP?
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.
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.
<title>File Upload in PHP </title>
<td width="94">Select File </td>
<td width="96"><input type="file" id="my_file" name="my_file" /></td>
<input name="btn_upload_file" type="submit" id="btn_upload_file" value="Upload" />
$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'];
$uploaded = move_uploaded_file($temp_path,$file_save_path);
print("The file ".$file_name." has been uploaded successfully");
print("<br/> File Type : ".$file_type);
print("<br/> File Size : ".$file_size. "Kb");
print("Error while uploading the file ".$_FILES['my_file']['error']);