Upload Image With PHP – GIF, JPG and PNG file formats
Example:
<?php
if(isset($_POST['Submit']))
{
$current_image=$_FILES['image']['name'];
$extension = substr(strrchr($current_image, '.'), 1);
if (($extension!= "jpg") && ($extension != "jpeg")
&& ($extension != "gif")
&& ($extension != "png"))
{
die('Unknown extension');
}
$time = date("fYhis");
$new_image = $time . "." . $extension;
$destination="uploads/".$new_image;
$action = copy($_FILES['image']['tmp_name'], $destination);
if (!$action)
{
die('File copy failed');
}else{
echo "File copy successful";
}
}else{
?>
<form method="post" enctype="multipart/form-data" action="index.php">
<input type="file" name="image" ><br>
<input type="submit" name="Submit" value="submit">
</form>
<?php
}
?>
if(isset($_POST['Submit']))
{
$current_image=$_FILES['image']['name'];
$extension = substr(strrchr($current_image, '.'), 1);
if (($extension!= "jpg") && ($extension != "jpeg")
&& ($extension != "gif")
&& ($extension != "png"))
{
die('Unknown extension');
}
$time = date("fYhis");
$new_image = $time . "." . $extension;
$destination="uploads/".$new_image;
$action = copy($_FILES['image']['tmp_name'], $destination);
if (!$action)
{
die('File copy failed');
}else{
echo "File copy successful";
}
}else{
?>
<form method="post" enctype="multipart/form-data" action="index.php">
<input type="file" name="image" ><br>
<input type="submit" name="Submit" value="submit">
</form>
<?php
}
?>
THANKS DUDE! HERE MY CODE!
THIS CODE WILL AUTOMATICLY CHANGE EVERY FILE TO .png
if(isset($_POST['Submit']))
{
$current_image=$_FILES['image']['name'];
$extension = substr(strrchr($current_image, '.'), 1);
$str1 = str_replace("$extension", ".png", "$extension", $count);
$new_image = $myuser[username].$str1;
$destination="images/users/".$new_image;
$action = copy($_FILES['image']['tmp_name'], $destination);
if (!$action)
{
echo "Kon foto niet uploaden!";
}else{
echo "Foto is geupload.";
}
}else{
?>
<form method="post" enctype="multipart/form-data" action="">
<input type="file" name="image" ><br>
<input type="submit" name="Submit" value="submit">
</form>
<?php
}
?>
Thank you very much for this nice peace of code