Doarn
0
Q:

upload file php

<form enctype="multipart/form-data" action="__URL__" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
    Diese Datei hochladen: <input name="userfile" type="file" />
    <input type="submit" value="Send File" />
</form>
4
<?php
	if(isset($_POST['upload_file'])){		
		$target_dir = "uploaded_files/";
		$target_file = $target_dir . basename($_FILES["select_file"]["name"]);
		$uploadOk = 1;
		$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
		
		$new_name = $target_dir."test_data.".$imageFileType;
		
		if($imageFileType != "jpg"){
			echo "File type is not supported";
		}
		else{
			if(file_exists($target_file)){
				echo "Sorry this file is already exists";
				// for file deletion
				// unlink($target_file);
				// echo "file is deleted";
			}
			else{
				if(move_uploaded_file($_FILES["select_file"]["tmp_name"], $new_name)){
					echo "your file has been successfully uploaded";
				}
				else{
					echo "please check your file";
				}
			}
		}
	}
	else{
		echo "kindly select the file for upload";
	}
?>
2
//This is the minimal code for an image upload for first time learners
//html portion
<!DOCTYPE html>
<html>
<head>
	<title>ImageUpload</title>
</head>
<body>
	<form action="upload.php" method="post" enctype="multipart/form-data">
		<label>Username</label>
		<input type="text" name="username">
		<br>
		<label>UploadImage</label>
		<input type="file" name='myfile'>
		<br/>
		<input type="submit" value="upload">
	</form>
</body>
</html>
  
 //php portion
  <?php
	$user=$_POST['username'];
	$image=$_FILES['myfile'];
	echo "Hello $user <br/>";
	echo "File Name<b>::</b> ".$image['name'];

	move_uploaded_file($image['tmp_name'],"photos/".$image['name']);
	//here the "photos" folder is in same folder as the upload.php, 
	//otherwise complete url has to be mentioned
	?>
0

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = 
  strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
  $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
  if($check !== false) {
    echo "File is an image - " . $check["mime"] . ".";

      $uploadOk = 1;
  } else {
    echo "File is not an image.";

      $uploadOk = 0;
  }
}

// Check if file already exists
if (file_exists($target_file)) {

    echo "Sorry, file already exists.";
  $uploadOk = 0;
}


 // Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
  echo "Sorry, your file is too large.";
  $uploadOk = 0;

 }

// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
  echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
  $uploadOk = 0;
}

// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
  echo "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)) {

      echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). 
  " has been uploaded.";

    } else {
    echo "Sorry, there was an error uploading your file.";

    }
}
?>
 
0
<?php
   if(isset($_FILES['image'])){
      $errors= array();
      $file_name = $_FILES['image']['name'];
      $file_size =$_FILES['image']['size'];
      $file_tmp =$_FILES['image']['tmp_name'];
      $file_type=$_FILES['image']['type'];
      $file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
      
      $extensions= array("jpeg","jpg","png");
      
      if(in_array($file_ext,$extensions)=== false){
         $errors[]="extension not allowed, please choose a JPEG or PNG file.";
      }
      
      if($file_size > 2097152){
         $errors[]='File size must be excately 2 MB';
      }
      
      if(empty($errors)==true){
         move_uploaded_file($file_tmp,"images/".$file_name);
         echo "Success";
      }else{
         print_r($errors);
      }
   }
?>
-1
// To change: FILENAME, array with allowed extensions, Max Filesite, Filepath
if(upload("FILENAME", array("jpeg","jpg","png"), 209715, "C:/xampp/htdocs/")){
        echo "Success";
    }


function upload($f_name, $f_ext_allowed, $f_maxsize, $f_path){

      $f_name_2 = $_FILES[$f_name]['name'];
      $f_size  =  $_FILES[$f_name]['size'];
      $f_tmp   =  $_FILES[$f_name]['tmp_name'];
      $f_error =  $_FILES[$f_name]['error'];
      $f_ext   = strtolower(end(explode('.',$f_name_2)));
      $f_rename = $_SESSION['uid'] . "." . $f_ext;

        if($f_error == 0 && in_array($f_ext, $f_ext_allowed) 
        && $f_size < $f_maxsize && mb_strlen($f_name_2, "UTF-8") < 225 
        && preg_match("`^[-0-9A-Z_\.]+$`i", $f_name_2)){
            if(move_uploaded_file($f_tmp, $f_path . $f_name_2){
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
}
0

 <!DOCTYPE html><html><body><form action="upload.php" method="post"
enctype="multipart/form-data">    Select image to upload:    <input type="file" name="fileToUpload" id="fileToUpload">    <input type="submit" value="Upload Image" name="submit">
</form></body></html>
 
-1

New to Communities?

Join the community