最简单的方法是将文件存储在服务器上的文件夹中.然后将文件的URL存储在MySQL数据库中,如果用户不知道文件位置,则拉出文件URL(假设您有登录功能).
例如:
(UPLOAD.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"] . "
";
} 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格式的数据,因此您可以让用户不必重新加载页面.
$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)) {
如果您需要更多帮助,请随时告诉我. 🙂