Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $folder = "upload/";
- if (is_uploaded_file($HTTP_POST_FILES['filename']['tmp_name'])) {
- if (move_uploaded_file($HTTP_POST_FILES['filename']['tmp_name'], $folder.$HTTP_POST_FILES['filename']['name'])) {
- echo "File uploaded";
- } else {
- echo "File not moved to destination folder. Check permissions";
- };
- } else {s
- echo "File is not uploaded";
- };
- ?>
- <?php
- $target_dir = "upload/";
- $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
- $uploadOk = 1;
- $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
- // Check if image file is a actual image or fake image
- if(isset($_POST["submit"])) {
- if ($target_file == "upload/") {
- $msg = "cannot be empty";
- $uploadOk = 0;
- }
- // Check if file already exists
- else if (file_exists($target_file)) {
- $msg = "Sorry, file already exists.";
- $uploadOk = 0;
- }
- // Check file size
- else if ($_FILES["fileToUpload"]["size"] > 5000000) {
- $msg = "Sorry, your file is too large.";
- $uploadOk = 0;
- }
- // Check if $uploadOk is set to 0 by an error
- else if ($uploadOk == 0) {
- $msg = "Sorry, your file was not uploaded.";
- // if everything is ok, try to upload file
- } else {
- if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
- $msg = "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
- }
- }
- }
- ?>
- <form action="upload.php" method="post" id="myForm" enctype="multipart/form-data">
- Select file to upload:
- <input type="file" name="fileToUpload" id="fileToUpload">
- <button name="submit" class="btn btn-primary" type="submit" value="submit">Upload File</button>
- </form>
- public static function uploadFile($filepath="upload",$existCheck=0,$uniq=0){
- global $_FILES;
- try {
- // Undefined | Multiple Files | $_FILES Corruption Attack
- // If this request falls under any of them, treat it invalid.
- if (
- !isset($_FILES['uploaded_file']['error']) ||
- is_array($_FILES['uploaded_file']['error'])
- ) {
- $result["status"]="fail";$result["errors"]=('Invalid parameters.');return $result;
- }
- // Check $_FILES['uploaded_file']['error'] value.
- switch ($_FILES['uploaded_file']['error']) {
- case UPLOAD_ERR_OK:
- break;
- case UPLOAD_ERR_NO_FILE:
- $result["status"]="fail";$result["errors"]=('No file sent.');return $result;
- case UPLOAD_ERR_INI_SIZE:
- case UPLOAD_ERR_FORM_SIZE:
- $result["status"]="fail";$result["errors"]=('Exceeded filesize limit.');return $result;
- default:
- $result["status"]="fail";$result["errors"]=('Unknown errors.');return $result;
- }
- // You should also check filesize here.
- if ($_FILES['uploaded_file']['size'] > 1000000) {
- $result["status"]="fail";$result["errors"]=('Exceeded filesize limit.');return $result;
- }
- // DO NOT TRUST $_FILES['uploaded_file']['mime'] VALUE !!
- // Check MIME Type by yourself.
- $finfo = new finfo(FILEINFO_MIME_TYPE);
- if (false === $ext = array_search(
- $finfo->file($_FILES['uploaded_file']['tmp_name']),
- array(
- 'jpg' => 'image/jpeg',
- 'png' => 'image/png',
- 'gif' => 'image/gif',
- ),
- true
- )) {
- $result["status"]="fail";$result["errors"]=('Invalid file format.');return $result;
- }
- if($uniq==0){
- $temp=$filepath;
- }
- else{
- $temp=$filepath."/".uniqid()."_".$_FILES['uploaded_file']['name'];
- }
- if ($existCheck==1 && file_exists($temp)) {
- $result["status"]="fail";$result["errors"]=('Unknown errors.');return $result;
- }
- if(@copy($_FILES['uploaded_file']['tmp_name'], $temp)) {
- return $result["status"]="success";
- }
- $result["status"]="fail";$result["errors"]=('Unknown errors.');return $result;
- } catch (Exception $e) {
- $result["status"]="fail";$result["errors"]= $e->getMessage();return $result;
- }
- }
- first, your write html code like this don't forget to write enctype="multipart/form-data" and then crerte new file name is upload.php
- <form action="upload.php" method="post" enctype="multipart/form-data">
- <input type="file" name="fileToUpload" id="fileToUpload">
- <input type="submit" value="Upload Image" name="submit">
- </form>
- $path = "form/";
- $target_file = $path.basename($_FILES["fileToUpload"]["name"]);
- $file=$_FILES['fileToUpload']['name'];
- $result = move_uploaded_file($_FILES['fileToUpload']['tmp_name'],$target_file.$file);
- if ($result) {
- echo "file successfully uploaded";
- }
- else {
- echo "please select your file";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement