For the love of god, I cannot get this work. I am new to php/mysqli(I don't even know anymore) and I am trying to get this to work for the non-profit I am with. I am never going to do this again. I have tried for 3 days and now I have no Idea where I am anymore.
I am trying to upload and download a file then delete it when we don't need it anymore.
Upload and Download works.
I can not get the delete function to work. If my life deepened on it...I would die....
Here is my data base connection:
<?php
$conn = new PDO( 'mysql:host=localhost;dbname=db_pdo_download', 'root', '');
if(!$conn){
die("Fatal Error: Connection Failed!");
}
?>
Here is the upload/download page:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
</head>
<body>
<nav class="navbar navbar-default">
<div class="container-fluid">
</div>
</nav>
<div class="col-md-3"></div>
<div class="col-md-6 well">
<h3 class="text-primary">Files</h3>
<hr style="border-top:1px dotted #ccc;"/>
<center>
<form method="POST" action="upload.php" enctype="multipart/form-data">
<div class="form-inline">
<input type="file" class="form-control" name="file" required="required"/>
<button class="btn btn-primary" name="upload"><span class="glyphicon glyphicon-upload"></span> Upload</button>
</div>
</form>
</center>
<br />
<table class="table table-bordered">
<thead class="alert-info">
<tr>
<th>File</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
require 'conn.php';
$query = $conn->prepare("SELECT * FROM `file`");
$query->execute();
while($fetch = $query->fetch()){
?>
<tr>
<td><?php echo $fetch['file']?></td>
<td>
<a href="download.php?file_id=<?php echo $fetch['file_id']?>" class="btn btn-primary">Download</a>
<!--Not Working-->
<a href="delete.php?file_id=<?php echo $fetch['file']?>" class="btn btn-primary">Delete</a>
<!--Not Working-->
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</body>
</html>
Here is my upload function:
<?php
require_once 'conn.php';
if(ISSET($_POST['upload'])){
$file_name = $_FILES['file']['name'];
$file_temp = $_FILES['file']['tmp_name'];
$file_size = $_FILES['file']['size'];
$exp = explode(".", $file_name);
$ext = end($exp);
$time = date("d-m-Y");
$name = $time."-".$file_name;
$path = "uploads/".$name;
if($file_size > 5242880){
echo "<script>alert('File too large')</script>";
echo "<script>window.location='index.php'</script>";
}else{
try{
if(move_uploaded_file($file_temp, $path)){
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO `file` (`file`, `location`) VALUES ('$name', '$path')";
$conn->exec($sql);
}
}catch(PDOException $e){
echo $e->getMessage();
}
$conn = null;
header('location:index.php');
}
}
?>
Here is my delete file…..not working:
<?php
//connect to data
require 'conn.php';
//what am I doing wrong here
//Trying to get the file to delete
$fileToDelete = $_GET['file_id'];
//Deleting that row using a prepared statement?
$sql = "DELETE FROM `file` WHERE `file_id` = :file";
//Prepare our DELETE statement.
$statement = $pdo->prepare($sql);
//The make that we want to delete from our cars table.
$makeToDelete = $fileToDelete;
//Bind our $makeToDelete variable to the paramater :make.
$statement->bindValue(':file', $makeToDelete);
//Execute our DELETE statement.
$delete = $statement->execute();
?>