Hello Friends, Today I am gonna share PHP code to upload file. I’ve created a function named as upload by which you can upload files on server. The function signature is upload($file, $filesize, $directory, $overwrite).
Here,
$file: name of input element whose type is file
$filesize: maxium file size to be uploaded
$directory: directory name where file to be uploaded
$overwrite: pass Y to overwrite existing file if already exists or N to generate new file name
To use this function create a file named as upload.php and paste the code, then include upload.php into your code and then use upload function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
<?php function upload($file, $filesize, $directory, $overwrite) { $filename = trim($_FILES[$file]["name"]); $response['filename'] = ""; $response['error'] = true; //assuming there is an error $response['error_msg'] = ""; if (empty($filename)) { $response['error_msg'] = "File not selected"; return $response; } if ($_FILES[$file]["error"] > 0) { $response['error_msg'] = 'There is some problem please try again later.'; return $response; } //allowed file extensions which gonna be uploaded $allowedExts = array("pdf", "doc", "docx", "jpg", "png", "gif"); //allowed file type which gonna be uploaded $allowedMimeTypes = array('application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/pdf', 'image/gif', 'image/jpeg', 'image/png'); //extracting uploaded file extension $extension = strtolower(end(explode(".", $_FILES[$file]["name"]))); //file size limit checking if (($filesize * 1024) < $_FILES[$file]["size"]) { $response['error_msg'] = 'File size should less than ' . $filesize . ' kb.'; return $response; } //checking file extension if (!( in_array($extension, $allowedExts) )) { $response['error_msg'] = 'Please provide another file type.'; return $response; } //checking file type if (!in_array($_FILES[$file]["type"], $allowedMimeTypes)) { $response['error_msg'] = 'Please provide another file type.'; return $response; } //if everything alright if ($directory != null && $directory <> '') { $directory = $directory . "/"; } else { $directory = ""; } //if overwrite parameter is not Y then new file name needs to be generated if ($overwrite != 'Y') { $i = 1; while (file_exists($directory . $filename)) { $filename = basename($filename, "." . $extension) . "_" . $i . "." . $extension; $i++; } } if (move_uploaded_file($_FILES[$file]["tmp_name"], $directory . $filename)) { $response['filename'] = basename($filename); $response['error'] = false; return $response; } else { $response['error_msg'] = "File could not moved"; return $response; } } ?> |
Using upload function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php include_once ('upload.php'); if (isset($_POST['uploadfile'])) { $response = upload("testfile", 2000, null, 'N'); //file not overitten and file uploaded in current directory where current file exists if ($response['error']) { echo $response['error_msg']; } else { echo "Uploaded file name is " . $response['filename']."<br>"; } } ?> <form method="post" enctype="multipart/form-data"> <input type="file" name="testfile"/> <input type="submit" name="uploadfile" value="upload"/> </form> |
Output in Browser
Thanks
Please share if you like it
Comments