最简单的方法是将文件存储在服务器上的文件夹中。然后将文件的 URL 存储在 MySQL 数据库中,如果用户不知道文件位置,则拉取文件 URL(假设您有登录功能)。
例如:
(UPLOAD.PHP 或您用来将文件上传到服务器的脚本)
<?php
$connect_to_db = mysqli_connect('localhost', 'user', 'pass', 'db');
$user = $_POST['user'];
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& in_array($extension, $allowedExts)) {
if ($_FILES["file"]["error"] > 0) {
echo "Error: " . $_FILES["file"]["error"] . "<br>";
} else {
//Move the file to the uploads folder
move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/" . $_FILES["file"]["name"]);
//Get the File Location
$filelocation = 'http://yourdomain.com/uploads/'.$_FILES["file"]["name"];
//Get the File Size
$size = ($_FILES["file"]["size"]/1024).' kB';
//Save to your Database
mysqli_query($connect_to_db, "INSERT INTO images (user, filelocation, size) VALUES ('$user', '$filelocation', '$size')");
//Redirect to the confirmation page, and include the file location in the URL
header('Location: confirm.php?location='.$filelocation);
}
} else {
//File type was invalid, so throw up a red flag!
echo "Invalid File Type";
}
?>
现在您可以做的是在页面中创建一个表格,并让它根据登录者列出所有上传的文件(再次假设您使用此功能)。这将使人们知道必须保留他们上传的所有文件的日志。
以下示例是如果您使用 Ajax 发布数据,并且它返回 JSON 格式的数据,因此您可以让用户不必重新加载页面。
<?php
$connect_to_db = mysqli_connect('localhost', 'user', 'pass', 'db');
$user = $_POST['user'];
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& in_array($extension, $allowedExts)) {
if ($_FILES["file"]["error"] > 0) {
echo json_encode(array('status' => 'error', 'msg' => 'File could not be uploaded.'));
} else {
//Move the file to the uploads folder
move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/" . $_FILES["file"]["name"]);
//Get the File Location
$filelocation = 'http://yourdomain.com/uploads/'.$_FILES["file"]["name"];
//Get the File Size
$size = ($_FILES["file"]["size"]/1024).' kB';
//Save to your Database
mysqli_query($connect_to_db, "INSERT INTO images (user, filelocation, size) VALUES ('$user', '$filelocation', '$size')");
//Return the data in JSON format
echo json_encode(array('status' => 'success', 'data' => array('filelocation' => $filelocation, 'size' => $size)));
}
} else {
//File type was invalid, so throw up a red flag!
echo json_encode(array('status' => 'error', 'msg' => 'Invalid File Format'));
}
?>
如果您还想限制文件大小,可以添加一行简单的代码来检查文件大小,如果文件太大,则不会让它通过:
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000) //Must be smaller than 20KB
&& in_array($extension, $allowedExts)) {
如果您需要更多帮助,请随时告诉我。 :)