Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Anarchy99

macrumors 65816
Original poster
Dec 13, 2003
1,041
1,034
CA
dont know if the title explains it well not sure how to put it
anyway ive got a table in my database that holds files and the file name column lists the name of the file
is there a way i could auto generate a page that is the same name as the filename eg file1.pdf generates file1.html

ive got the basic code of the page planned out
on the page
i want a link to download the file and a comment area that i have working similar to what you would see on a blog
 
One approach; on links to these files you can add a query string e.g., page.php?id=filename Then in the PHP it grabs that id for the file name and uses it to pull the actual file and either have it set to download it or present it on a HTML page. This could be all done on a single PHP page. If no query string is present it could just list a bunch of links to the various files.
 
One approach; on links to these files you can add a query string e.g., page.php?id=filename Then in the PHP it grabs that id for the file name and uses it to pull the actual file and either have it set to download it or present it on a HTML page. This could be all done on a single PHP page. If no query string is present it could just list a bunch of links to the various files.

anyone know of any tutorials or examples that demonstrate it or something similar so i can get a idea how to do it
 
Hold up, please re-explain. How does the file get added to the database, is this via an upload from a form presented to a user, or is the data already there? I can post some code on PHP file uploads and how to process them and then display a download page showing the uploaded file as a link or whatever you want.

-jim
 
Hold up, please re-explain. How does the file get added to the database, is this via an upload from a form presented to a user, or is the data already there? I can post some code on PHP file uploads and how to process them and then display a download page showing the uploaded file as a link or whatever you want.

-jim

its a upload form for the user

the upload php im using:


<?php

require ('dbconnect.php'); // connect to database

if(isset($_POST['upload'])) // Has a file been uploaded?
{
// See if it is of one of the allowed mime types, change these if you like
if (($_FILES['uploadedfile']['type'] == "application/pdf")||
($_FILES['uploadedfile']['type'] == "image/jpeg"))
{


// This is the temporary place where it got put on the server after it was uploaded
$tempfile = $_FILES['uploadedfile']['tmp_name'] ;

// addslashes so as not to break anything :)
$data = addslashes(fread(fopen($tempfile, "rb"), filesize($tempfile)));

// pull out useful bits in case they are needed.
$filetype = $_FILES['uploadedfile']['type'];
$filesize = $_FILES['uploadedfile']['size'];
$filename = $_FILES['uploadedfile']['name'];

// Stick it into the database.
$query = "INSERT INTO uploads (data,filename,filesize,filetype) VALUES ('$data','$filename','$filesize','$filetype')";
mysql_query($query);
}
// If it wasn't one of the allowed file types do this:
else
{
echo "Invalid File type<br>";
}
}

// Form to upload a new file, make sure you get the "enctype" or it won't work
echo "
<form enctype='multipart/form-data' name='fileupload' action='upload.php' method='POST'>

<input type='file' name='uploadedfile'>
<input type='submit' name='upload' value='Upload File'>

</form>
";

?>
 
Okay, so far all I see is users can upload a file and the upload is inserted into a database. As to that aspect, angelwatt included links to help you if for some reason your code isn't working. If it is working, let's move on to your second request which confused me:

[I want to] auto generate a page that is the same name as the filename eg file1.pdf generates file1.html

You need to explain this better - do you mean 1) you want to generate a page that is stored on the server which can be called later such as filename.html? Or 2) do you mean the moment after the file is uploaded, you want the user to be directed to filename.html with whatever content you need included?

If the former (1), this is NOT suggested - you will waste space with multiple files associated with multiple uploads, if you switch layout or styling you need to change all at once, and you need to deal with deleting files from both the server and database. Bloatware, overly complex. What most people do is write a single script which uses an argument of the URL (as was discussed in the beginning of this topic) to pass the filename to the script which runs a query to fetch the data from the database and do whatever else. Note: You should add an "ID" field in your table and use that as the argument, not the filename. SQL injection issues plus its easier to pass an ID then deal with encoding issues on alphanumeric text.

If the latter (2) what you need to do is similar to #1, just put redirect code to the same script after your condition that checks to see if a file was uploaded. In your redirect, using PHP's header command, include the URL argument as discussed in #1 and include the filename which is already in memory. Then of course add in a condition at the top of the script which checks for the URL argument, and does everything I discussed for #1. This is all done in one single script, which means less code.

-jim
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.