এই lesson এ আমারা দেখব কিভাবে Form থেকে PHP তে Image বা অন্য কোন ফাইলে upload করতে হয়
form.php
<!DOCTYPE html>
<html>
<head>
<title> File upload </title>
</head>
<body>
<form method="POST" action="action.php" enctype="multipart/form-data">
Name: <input type="text" name="name"> <br/><br/>
Image: <input type="file" name="image"> <br/><br/>
<input type="submit" value="Save">
</form>
</body>
</html>
action.php
echo "<br/>";
echo $_FILES['image']['name'];
echo "<br/>";
echo $_FILES['image']['size'];
echo "<br/>";
$extension = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);
if ($extension != 'jpg') {
echo 'Please upload jpg file';
die;
}
$tmp = $_FILES['image']['tmp_name'];
// $target = 'image/' . $_FILES['image']['name'];
// $target = 'image/' . rand(1, 99999999). $_FILES['image']['name'];
// $target = 'image/' . rand(1, 99999999). '.' . $extension;
// $target = 'image/' . rand(0, 9999) . '-' . time(). '.' . $extension;
$target = 'image/' . rand(0, 99999999) . $_POST['name'] . '.' . $extension;
if( move_uploaded_file($tmp, $target) ) {
echo "File Uploaded Successfully";
} else {
echo 'Failed to upload';
}