image uploading using php

image uploading using php



 <?php

$n = 10;

function getRandomString($n)

{

  $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

  $randomString = '';


  for ($i = 0; $i < $n; $i++) {

    $index = rand(0, strlen($characters) - 1);

    $randomString .= $characters[$index];

  }


  return $randomString;

}





$filename= $_FILES['userfile']['name'];


$source= $_FILES['userfile']['tmp_name'];


$destination="photo/".getRandomString($n).$filename;


echo $destination,"<br>";

$type=pathinfo(basename($destination),PATHINFO_EXTENSION);

if($type==="jpg" ||  $type==="png")

{

echo"allowed";

move_uploaded_file($source, $destination);

else

{

echo"not allowed";

}



?>


<html>


<head>




</head>


<body>


<form method="post" enctype="multipart/form-data">


<input type="file" name="userfile"  required>


<br><br>


<input type="submit" name="save">


</form>




</body>




</html>


0 Comments