Method 2: Converting a Website into an Android App Using Android Studio
Java |
2025-02-21 11:24:00
Here is a simple and complete example of PHP and HTML source code to upload an image to your server. It includes:
File type and size validation
Upload to a specific folder (uploads/
)
Feedback messages
/upload-demo/
── upload.php
── index.html
── uploads/
← Make sure this folder is writable (chmod 755 or 777)
index.html
(HTML Form)<!DOCTYPE html>
<html>
<head>
<title>Image Upload</title>
</head>
<body>
<h2>Upload an Image</h2>
<form action="upload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="image" accept="image/*" required><br><br>
<button type="submit" name="upload">Upload Image</button>
</form>
</body>
</html>
upload.php
(PHP Handler)<?php
if (isset($_POST['upload'])) {
$targetDir = "uploads/";
$file = $_FILES['image'];
// File info
$fileName = basename($file['name']);
$targetFile = $targetDir . $fileName;
$fileType = strtolower(pathinfo($targetFile, PATHINFO_EXTENSION));
$allowedTypes = ['jpg', 'jpeg', 'png', 'gif'];
// Validate image
if (!in_array($fileType, $allowedTypes)) {
echo " Only JPG, JPEG, PNG & GIF files are allowed.";
exit;
}
if ($file['size'] > 2 * 1024 * 1024) { // 2MB limit
echo " File is too large. Max 2MB allowed.";
exit;
}
if (move_uploaded_file($file["tmp_name"], $targetFile)) {
echo " Image uploaded successfully: <a href='$targetFile'>$fileName</a>";
} else {
echo " Failed to upload image.";
}
} else {
echo "No file uploaded.";
}
?>
Make sure the uploads/
folder has write permissions (chmod 755
or 777
).
Always sanitize the filename in production to avoid malicious file names.
Add further security by checking MIME type (using mime_content_type()
or getimagesize()
).
Would you like this bundled in a .zip
file ready to download and use?