Reputation: 288
I have already use upload & show function in PHP, but if someone press "Rename" button in my view section, it will open a dialog box where existing filename show, it can be changed by user & also store the new name which is input by the user(not only in SQL but also changed file name which is stored directory). file path stored in MySQL, files store in define folder.
I tried to find on the internet but most of its change the name while uploading or change the pre define names, not change by the user
Here is my file uploading code:
$db=connection
if(isset($_POST['submit']))
{
$filename=$_FILES["pdf"]["name"];
$tempname=$_FILES["pdf"]["tmp_name"];
$folder="files/".$filename;
move_uploaded_file($tempname,$folder);
$abc="INSERT INTO tables (pdf) VALUES ('$folder')";
$res=mysqli_query($db,$abc);
if($res)
{
echo "save";
}
}
& this is my view:
<?php
$acb = "SELECT * FROM tables";
$ress = mysqli_query($db, $acb);
while ($test = mysqli_fetch_array($ress)) {
$id = $test['id'];
echo "<tr alian='center'>";
$path = "/files";
$file2 = basename($path, ".php");
echo "<td>".$test['id']."</td>";
echo "<td>".basename($test['pdf'])."</td>";
echo "<td><a href='ren.php?id=$id'>Rename</a></td>";
echo "<td><a href='del.php?id=$id'>Delete</a>
</td>";
}
I understand that this is simple code & not have any related code for renaming, but I already tried many times for check.
Upvotes: 0
Views: 724
Reputation: 6006
You might want to take a different approach here: Maybe upload the files and rename them to something convenient after upload, for example: id of table's row, and store the name of the file in your MySQL table (add a filename column), which the user can change.
Then when the user downloads the file, send the name stored in the database with the Content-disposition
header.
On upload:
$filename = mysqli_real_escape_string($db, basename($_FILES["pdf"]["name"]));
if (mysqli_query($db, "INSERT INTO tables (pdf) VALUES ('$filename')")) {
$id = mysqli_insert_id($db);
$folder = "files/" . $id;
move_uploaded_file($_FILES["pdf"]["tmp_name"], $folder);
}
When updating the filename, you will do something like this:
UPDATE tables SET pdf = '$newName' WHERE id = $id
(Don't forget to escape the input and sanitize the filename)
When serving the file to the user (user downloads the file)
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="' . $row["filename"] . '"');
readfile('files/' . $row["id"]);
Upvotes: 1