phpifisset$_POSTsubmit $id = $_POSTid; $big_version = $_POSTbig_version; $version = $_POSTversion; $edition = $_POSTedition; $size = $_POSTsize; $link = $_POSTlink; $img = $_POSTi
The given code is a PHP script that handles data submission from a form. It connects to a MySQL database and inserts the submitted data into a table called mc_pe_apps.
To add a security measure called encoding to the file upload process, you can modify the code as follows:
- Add the
enctypeattribute to the<form>tag with the value"multipart/form-data". This is necessary for uploading files. - Change the input field for the image to
<input type="file" name="img"/>. This allows users to select a file from their local system for upload. - Modify the PHP script to handle the file upload. You can use the
move_uploaded_file()function to save the uploaded file to a specific directory on the server. - Add validation and security checks to ensure that the uploaded file is an image and has the correct file extension (e.g., jpg, png, etc.). You can use the
$_FILESsuperglobal to access information about the uploaded file. - Generate a unique filename for the uploaded image to prevent conflicts and overwrite existing files. You can use functions like
uniqid()ortime()combined with the original filename to create a unique name. - Save the unique filename in the database instead of the original filename.
Here's an example of the modified code:
<?php
if(isset($_POST['submit'])){
$id = $_POST['id'];
$big_version = $_POST['big_version'];
$version = $_POST['version'];
$edition = $_POST['edition'];
$size = $_POST['size'];
$link = $_POST['link'];
$upload_time = $_POST['upload_time'];
$con = mysqli_connect("localhost:3306","sertgjkaxpit","t1nbhzvw0qhq");
if (!$con)
{
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con, "sertgjkaxpit");
// Handle file upload
$img = $_FILES['img']['name'];
$img_tmp = $_FILES['img']['tmp_name'];
$img_extension = strtolower(pathinfo($img, PATHINFO_EXTENSION));
// Validate and save the uploaded image
if (in_array($img_extension, ['jpg', 'jpeg', 'png'])) {
$new_img_filename = uniqid('', true) . '.' . $img_extension;
$img_directory = 'uploads/';
$img_path = $img_directory . $new_img_filename;
if (move_uploaded_file($img_tmp, $img_path)) {
mysqli_query($con, "INSERT INTO `mc_pe_apps` (`id`, `big_version`, `version`, `edition`, `size`, `link`, `img`, `upload_time`)
VALUES ('$id', '$big_version', '$version', '$edition', '$size', '$link', '$new_img_filename', '$upload_time');");
} else {
echo 'Error uploading image.';
}
} else {
echo 'Invalid image format. Please upload a JPG, JPEG, or PNG file.';
}
mysqli_close($con);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
ID:<input type="text" name="id"/>
大概版本:<input type="text" name="big_version"/>
版本:<input type="text" name="version"/>
测试版/正式版:<input type="text" name="edition"/>
大小:<input type="text" name="size"/>
链接:<input type="text" name="link"/>
图像:<input type="file" name="img"/>
上传时间:<input type="text" name="upload_time"/>
<input type="submit" name="submit" value="提交"/>
</form>
</body>
</html>
In this modified code, the uploaded image file is saved in a directory called "uploads" (which you need to create in your project directory). The image filename is stored in the database as a unique identifier to prevent conflicts
原文地址: http://www.cveoy.top/t/topic/iyVu 著作权归作者所有。请勿转载和采集!