Answers for "upload images php"

PHP
2

php image upload write to server

<?php error_reporting(0);

$msg = "";

if ( isset( $_POST['upload']) ) {

	$filename = $_FILES["uploadfile"]["name"];
	$tempname = $_FILES["uploadfile"]["tmp_name"];
    $folder   = $filename;

    if ( move_uploaded_file( $tempname, $folder ) ) {
        $msg = "Image uploaded successfully";
    } else{
        $msg = "Failed to upload image";
	}
}
?>

<!DOCTYPE html>
<html>
    <head>
    <title>Image Upload</title>
    </head>
    <body>

    <h2><?php echo $msg; ?></h2>
    <form method="POST" action="" enctype="multipart/form-data">
        <input type="file" name="uploadfile"/>
        <button type="submit" name="upload">UPLOAD</button>
    </form>
    </body>
</html>
Posted by: Guest on June-17-2021
1

basic code for file upload in php

//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
	?>
Posted by: Guest on July-03-2020
2

img upload in php

if(isset($_FILES['image']))
                {
                    $img_name = $_FILES['image']['name'];      //getting user uploaded name
                    $img_type = $_FILES['image']['type'];       //getting user uploaded img type
                    $tmp_name = $_FILES['image']['tmp_name'];   //this temporary name is used to save/move file in our folder.
                    
                    // let's explode image and get the last name(extension) like jpg, png
                    $img_explode = explode(".",$img_name);
                    $img_ext = end($img_explode);   //here we get the extension of an user uploaded img file

                    $extension= ['png','jpeg','jpg','gif']; //these are some valid img extension and we are store them in array.
Posted by: Guest on August-25-2021

Browse Popular Code Answers by Language