• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Logging and managing downloads with PHP

vetteguy

Diamond Member
I am developing a site which allows users to upload files (specifically Word documents) to the webserver, documents in an MySQL database who uploaded the file, the filename, the date, etc. Then, there is a page for admins to log in and download these files for review. I would like to make it so that when one person downloads a file, it flags it in the database as being downloaded and by whom. I have a script written which lists all of the files currently uploaded, and now I'm trying to make a system to perform the second part. Does anyone have any suggestions? I thought about having like a radio button next to each of the files, and then you pick one and click "Submit" which would take you to a php script which would basically do a redirect to that file and force a download, but I can't seem to figure out how to determine which file has been selected. This is part of the script that displays the files:

while ($row = mysql_fetch_array($result)) {

$user_id = $row['user_id'];
$username = $row['username'];
$filename = $row['filename'];
$submit_date = $row['submit_date'];
$downloaded = $row['downloaded'];
$download_date = $row['download_date'];
$download_id = $row['download_id'];

if ($downloaded == "N") {
$download_date = "NA";
}
if ($counter %2 == 0) {
echo "<tr bgcolor=#FFFFFF onMouseOver=this.className='highlight'; onMouseOut=this.className='normal';>";
}else{
echo "<tr bgcolor=#DDDDDD onMouseOver=this.className='highlight'; onMouseOut=this.className='normal2';>";
}
td_print($username);
td_print($filename);
td_print($submit_date);
td_print($downloaded);
td_print($download_date);
echo "<td><input type=radio name=download value=Y></td>";
tr_print();

$counter++;
}

So basically it's a while loop which lists all of the rows of uploaded files in the database. Does anyone have any suggestions for how I can take the next step (downloading)? I need to flag the file as downloaded so someone doesn't download a file that's already been viewed. Thanks!
 
Pass the filename as a parameter in the URL and redirect it as you said.

<a href="download.php?file=<? echo $filename; ?>"><? echo $filename; ?></a>

In download.php, you can flag $filename in your database or wherever.
 
concur on the url...simplest.

Hey did u write a php script to accept multi-part form posts for your uploads?

I tried doing that and said f*** it and just found a cgi to work...too big a hassle w/ php IMO.
 
Originally posted by: calpha
concur on the url...simplest.

Hey did u write a php script to accept multi-part form posts for your uploads?

I tried doing that and said f*** it and just found a cgi to work...too big a hassle w/ php IMO.

What's the big hassle with PHP? Upload script with multi-part form is pretty simple with PHP IMHO.
 
Originally posted by: igowerf
I think PHP handles HTML forms beautifully.

HMTL Forms beautifully I agree....as long as it's not multi-part.

but when uploading binary data (read: files) I found that a general CGI script to verify that to check for existing file name, and delete it if exists....and then write it out....was much easier. Maybe that too was because I was lazy.

PHP is by far the best I've ever used in terms of Total Ease of Use. even beats ASP IMO.

I guess I just didn't like doing file operations in PHP....i ran a couple of tests, and said....well, i know there' s tons of cgis that work already written....and the php scirpts I was playing with for the uploads weren't working EXACTLY right.....so I just went with what worked.

 
Originally posted by: calpha
Originally posted by: igowerf
I think PHP handles HTML forms beautifully.

HMTL Forms beautifully I agree....as long as it's not multi-part.

but when uploading binary data (read: files) I found that a general CGI script to verify that to check for existing file name, and delete it if exists....and then write it out....was much easier. Maybe that too was because I was lazy.

PHP is by far the best I've ever used in terms of Total Ease of Use. even beats ASP IMO.

I guess I just didn't like doing file operations in PHP....i ran a couple of tests, and said....well, i know there' s tons of cgis that work already written....and the php scirpts I was playing with for the uploads weren't working EXACTLY right.....so I just went with what worked.

You're talking about two different animals here. File handling and HTML form handling. Obviously, nothing will beat Perl/CGI for file handling. But for HTML forms (including multi-part/form-data), I think PHP handles it really well.
 
I got the redirect thing to work if I pass it a hard-coded filename, but now I need to have a way to select which file I want to download. Whether it's a radio button or whatever doesn't really matter. Does anyone have any suggestions about this? I would make each row have a link to download, but that wouldn't allow me to flag the database.

calpha-I have a php upload script if you're interested.
 
OK....what's it called when you upload a file using a form post? I thought the Mime type of the form was different, but that it was still a multi-part form?

Or did I just jump offf the short bus?
 
Originally posted by: calpha
OK....what's it called when you upload a file using a form post? I thought the Mime type of the form was different, but that it was still a multi-part form?

Or did I just jump offf the short bus?
Here's the script which does the actual uploading...ignore the database stuff, since I'm logging the uploads in MySQL:

<?php
require "includes/dbsetup.php";

$table_name = "files";
$db = @mysql_select_db($db_name, $connection) or die ("Couldn't select database");

if (@is_uploaded_file($_FILES["doc1"]["tmp_name"])) {
copy($_FILES["doc1"]["tmp_name"], "uploads/" . $_FILES["doc1"]["name"]);

$filename = $_FILES['doc1']['tmp_name'];
$realname = $_FILES['doc1']['name'];

$sql = "
INSERT INTO $table_name
(user_id, filename, submit_date, downloaded, download_id)
VALUES
(\"20\", \"$realname\", \"$today\", \"N\", \"0\")
";

$result = @mysql_query($sql,$connection) or die("Couldn't execute query");
if ($connection) {
$msg = "success";
}


header( "Location: http://test2k/owl/success.php?realname=$realname");
}

?>

Here's the HTML form that lets someone pick a file for upload:

<html>
<head>
<title>Submit a Document</title>
</head>

<body>

<p>Use the form on this page to submit a document to the Writing Center
Staff for review. If you have not yet read the policies and statement of
services, please do do <a href="/services.htm">here</a>. Also, if you have not
yet logged into the site, please do so before proceeding. </p>

<form enctype="multipart/form-data" action="do_upload.php" method="post">
<input name="doc1" type="file" size="32" /><br />
<input type="submit" value="Upload" />
</form>

</body></html>

HTH
 
Back
Top