Page 1 of 1
an inquiry regarding file attachment for forums.
Posted: Fri Sep 09, 2011 9:11 am
by urboyfriend
do most forums use the move_uploaded_file() to make attachments?
Also, may i know any good links on how to create file attachment/s to posts? (for bbs like this)
thank you for the quick reply!
Re: an inquiry regarding file attachment for forums.
Posted: Fri Sep 09, 2011 9:43 am
by ok
The PHP manual has nice tutorial about file uploading:
http://www.php.net/manual/en/features.file-upload.php
Re: an inquiry regarding file attachment for forums.
Posted: Sun Sep 11, 2011 9:58 am
by urboyfriend
may i get more information about this lines?
Code: Select all
<?php
// place this code inside a php file and call it f.e. "download.php"
$path = $_SERVER['DOCUMENT_ROOT']."/path2file/"; // change the path to fit your websites document structure
$fullPath = $path.$_GET['download_file'];
if ($fd = fopen ($fullPath, "r")) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
switch ($ext) {
case "pdf":
header("Content-type: application/pdf"); // add here more headers for diff. extensions //this line
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download //this line
break;
default;
header("Content-type: application/octet-stream"); //this line
header("Content-Disposition: filename=\"".$path_parts["basename"]."\""); //this line
}
while(!feof($fd)) {
$buffer = fread($fd, 2048); header("Content-length: $fsize"); //this line
header("Cache-control: private"); //this line //use this to open files directly
echo $buffer;
}
}
fclose ($fd);
exit;
// example: place this kind of link into the document where the file download is offered:
// <a href="download.php?download_file=some_file.pdf">Download here</a>
?>
header
("Content-type: application/pdf");
header
("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");
header
("Content-type: application/octet-stream");
header("
Content-Disposition: filename=\"".$path_parts["basename"]."\"");
header("
Content-length: $fsize");
header("
Cache-control: private");
i have used the header before but only to redirect people to another page...
reading links are appreciated

Re: an inquiry regarding file attachment for forums.
Posted: Sun Sep 11, 2011 10:21 am
by ok
The file you attached above is used for handling file downloading through PHP. It is heavily used for preventing access from unauthorized users (access control).
If you don't mind everybody downloading your files, you can simply link to the file itself (Wordpress does this, PHPBB has an option for this).
Headers are defined by Wikipedia as:
HTTP header fields are components of the message header of requests and responses in the Hypertext Transfer Protocol (HTTP). They define the operating parameters of an HTTP transaction.
So, basically, the headers tell the browser what type of file you are about to send (Content-Type), how to handle the file download (Content-Disposition), what is the file size (Content-length) for displaying the user a progress of the download, and how to cache the file (Cache-Control). In the file you attached, PHP is telling the browser not to cache the file (Cache-Control: private).
All of the above is automatically handled for you by the web server if you choose to link directly to the file, instead of using a PHP script to handle the download.
Wikipedia (and Google) is very helpful regarding HTTP. You can read more on the different headers here:
http://en.wikipedia.org/wiki/MIME#Content-Type
http://en.wikipedia.org/wiki/MIME#Content-Disposition
http://en.wikipedia.org/wiki/HTTP_header