I have the following code which timestamps all files uploaded to the server. As a result each file is unique. I’m trying to download each file but can’t use the _FILES[file][‘name’] global since that won’t work. How should I go about downloading my files?
Uploading code:
[php]
$upload_dir = ‘repository/’;
if (isset($_POST[‘addfile’])){
$tempFile = $_FILES['file']['tmp_name'];
$FileName = $_FILES['file']['name'];
$file_type = $_FILES['file']['type'];
// using DIRECTORY_SEPARATOR constant is a good practice, it makes your code portable.
$targetPath = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . $upload_dir . DIRECTORY_SEPARATOR;
// Adding timestamp with image's name so that files with same name can be uploaded easily.
$mainFile = $targetPath.date("Y-m-d H-i-s").'-'. $_FILES['file']['name'];
//get variables for database
$projectname=$_POST['projectname'];
$version= $_POST['version'];
$comments=$_POST['comments'];
if(move_uploaded_file($tempFile,$mainFile)){
include 'db.php';
$add = mysql_query("INSERT INTO `myversions`(filename, filetype, projectname, version, comments, created)
VALUES ('$FileName', '$file_type','$projectname','$version','$comments', NOW())");
echo "Your file “. basename( $_FILES[‘file’][‘name’]). " has been successfully uploaded and recorded in the database!”;
}
else {
echo “Your file failed to upload.”;
}
}
[/php]
View code:
[php]
include(“db.php”);
//Show all files
$result = mysql_query("SELECT * FROM myversions ORDER by projectname ASC, created DESC");
echo "<div class='table-responsive'>";
echo "<table class='table table-striped table-bordered'>";
echo "<thead>";
echo "<tr>";
echo "<th>File Name</th><th>Project Name</th><th>Version</th><th>Comments</th><th>Created</th>";
echo "</tr></thead>";
echo "<tbody>";
while($row = mysql_fetch_array($result)){
$id = $row['id'];
echo "
<tr>
<td>$row[filename]</td>
<td>$row[projectname]</td>
<td>$row[version]</td>
<td>$row[comments]</td>
<td>$row[created]</td>
<td><a href='#'>Download File</a></td>
</tr>";
}
echo "</tbody>";
echo "</table>";
echo "</div>";
//close connection
mysql_close($conn)
[/php]