我有一个简单的文件共享页面,允许用户上传图像并在列表中显示目录内容。我的数据库中有 2 个用户,我想让它只有一个可以删除文件。我的数据库有一个用户类型列,这些值为 1 或 2。这是我的代码:
<body>
<?php include "view/header.php" ?>
<div id="main">
<h2>Image to be uploaded</h2>
<form id="upload_form" action="." method="POST" enctype="multipart/form-data">
<input type="hidden" name="action" value="upload">
<input type="file" name="file1"><br />
<input id="upload_button" type="submit" value="Upload">
</form>
<h2>Images in the directory</h2>
<?php if (count($files) == 0) : ?>
<p>No images have been uploaded.</p>
<?php else: ?>
<ul>
<?php foreach($files as $filename) :
$file_url= $image_dir . '/' . $filename;
$delete_url= '.?action=delete&filename=' . urlencode($filename);
?>
<li>
<a href= "<?php echo $delete_url;?>">
<img src= "images/delete.png" alt= "Delete"></a>
<a href= "<?php echo $file_url;?>">
<?php echo $filename;?></a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
<div id="footerholder">
<div id="footer">
<?php include "view/footer.php" ?>
</div>
这是我的登录页面所指的:
<?php
$dsn= 'mysql:host=localhost; dbname=file_share';
$username= 'root';
$password= '';
try {
$db= new PDO($dsn, $username, $password); }
catch (PDOException $e) {
$error_message= $e->getMessage();
echo "<p>An error occured while connecting to the database: $error_message </p>"; }
//Only run code below if form has been posted
if($_SERVER['REQUEST_METHOD'] == 'POST') {
if( isset($_POST['username'], $_POST['password'])
&& !empty($_POST['username']) && !empty($_POST['password']) ) {
$username = $_POST['username'];
$password = $_POST['password'];
$sql= 'SELECT *
FROM `users`
WHERE `username` = :username
AND `password` = :password
LIMIT 1'; //SQL query with named placeholders
$stmt = $db->prepare($sql); //Returns a PDOStatement class object
$stmt->bindParam(':username',$username,PDO::PARAM_STR,16);
$stmt->bindParam(':password',$password,PDO::PARAM_STR,16);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
$error = $stmt->errorInfo();//Debug
if($error[0] != '00000')
printf('SQL Error: <pre>%s</pre>', print_r($error, true));
if($stmt->rowCount() > 0) {
$_SESSION['loggedIn']= "true";
header("Location: uploader.php");
}
else {
echo 'Sorry username/password wrong';
echo '<br><a href="login.php">Try Again</a></br>';
}