CodeCharge Studio
search Register Login  

Web Reports

Visually create Web Reports in PHP, ASP, .NET, Java, Perl and ColdFusion.
CodeCharge.com

YesSoftware Forums -> CodeCharge Studio -> PHP

 Downloading uploaded files from a secure directory

Print topic Send  topic

Author Message
jacem

Posts: 87
Posted: 05/20/2008, 12:15 AM

I am using CCS 3.2 MySQL on Linux server. I am uploading files successfully using CCS's fileupload. Most upload applications in the past have been focused on uploading files that are ok for the rest of the world to view, usually via {link}. However this latest application involves uploading client files for an accountant. Obviously the rest of the world should not be seeing them, only authorised 'users' on my CCS database application (they login using standard CCS login forms and each page is restricted to valid users).

If my filefolder attributes are set to 770 (ie: no 'World' access) then my {link}'s to the uploaded files naturally enough fail with Forbidden: You don't have permission to access <filepath/filename> on this server, which is to be expected.

The files are successfully being uploaded and stored in the directory but I can’t get my head around how to satisfy the need to then provide those files to authorised uses of my database application. It’s like we need a filedownload component as well as the fileupload component so the server can grab a file and serve it back to the user while still in our secure ccs protected environment.

Do I need to have a routine written to copy a requested file back to the temp folder before serving the link to the authorised user? How would the server know that it is time to delete the file from the temp folder – how would it know the user is finished downloading it? Also wouldn’t this mean the file is open to the world for a period of time?

Is there a better way? I have looked at a lot of the fourm entries for uploads problems like mine but almost all seem to be path issues and rights in getting file upload working which is not an issue for me.

What am I missing (be gentle with me!)

View profile  Send private message
andy


Posts: 183
Posted: 05/21/2008, 3:47 AM

Hi

If you point your web domain to a subfolder of your root folder and then create your download/upload document folder as a folder in your root then the "world and his dog" shouldn't be able to access it via your domain.

e.g.

Root
|_mydomain.com-folder = www.mydomain.com

Root
|_mydomain.com-folder/myCCSwebapp = www.mydomain.com/CCSwebapp/login.php

Root
|_securedocuments-folder (can't be accessed via the url)

Then in the upload file path you would need to specify "../../securedocuments-folder"

I hope this makes sense.
_________________
Andy

RAD tools for rich UI controls:
http://www.koolphptools.com
View profile  Send private message
wkempees


Posts: 1679
Posted: 05/21/2008, 4:21 AM

Further to Andy's post...

As you are uploading through a CCS FileUpload, the owner would be you webservers account.
Downloading would be no problem and security seems catered for.
Only addition could be using non obvious naming patterns or even renaming the files after upload, storing the new name in your database table, so that upon download the selection made from the database would lead to a filename that differs from the uploaded one.

Walter
JM2ct
_________________
Origin: NL, T:GMT+1 (Forumtime +9)
CCS3/4.01.006 PhP, MySQL .Net/InMotion(Vista/XP, XAMPP)

if you liked this info PAYPAL me: http://donate.consultair.eu
View profile  Send private message
mentecky

Posts: 321
Posted: 05/21/2008, 2:17 PM

I've used code to do this. This is just an example and you'll have to edit the heck out of it to match your system.

First, create a blank CCS page, in this example I'll call it "getmyfile.php"

Set your security options like you would for any page on your site.

In your new page's OnBeforeShow event add something like this:

	   // Edit this to point at your document/image path  
      $path = dirname(__FILE__)."/documentdir/";  
        
      // This is for example only. I look my filenames up in a DB. If you use this you had better check your filenames!!!!! They could point  
      // anywhere on your server !  
	   $file_name = CCGetParam("filename","");  
        
      $file_parts = explode(".", $file_name);  
      $ext = strtoupper($file_parts[count($file_parts) - 1]);  
  
      // Set header information  
      header('pragma: no-cache');  
      header('expires: 0');  
      header('Cache-Control: no-cache');  
      header('Content-Length: '.filesize($path.$file_name));  
      header('Content-Disposition: attachment; filename="'.$file_name.'"');  
  
	   // do some MIME stuff  
	   if ($ext == "DOC")  
	   {  
         $content_type = "application/msword";  
	   }  
	   else if ($ext == "PDF")  
	   {  
         $content_type = "application/pdf";	    
      }  
	   else if ($ext == "RTF")  
	   {  
         $content_type = "application/rtf";	    
      }  
	   else if (($ext == "MPG2") || ($ext == "MP3") || ($ext == "MP2"))  
	   {  
         $content_type = "audio/mpeg";	    
      }  
	   else if ($ext == "WAV")  
	   {  
         $content_type = "audio/x-wav";	    
      }  
	   else if ($ext == "GIF")  
	   {  
         $content_type = "image/gif";	    
      }  
	   else if ($ext == "JPG")  
	   {  
         $content_type = "image/jpeg";	    
      }  
	   else if ($ext == "PNG")  
	   {  
         $content_type = "image/png";	    
      }  
	   else if (($ext == "TIF") || ($ext == "TIFF"))  
	   {  
         $content_type = "image/tiff";	    
      }  
	   else  
	   {  
         $content_type = "text/plain";	    
      }  
  
      header('Content-type: '.$content_type);  
  
	   // output the file  
      readfile($path.$file_name);  
  
      exit;  

PLEASE do not depend on passing the filename on the URL. This is just an example. I usually pass something like "doc_id=1" and look the file up in a table. But for this example and so you can test easily I just pass it as a parameter.

Also note I don't error check much because if it's in the database I'm sure the file exists etc.

Now you should be able to DL any file in the given folder with a link like:

<a src="getmyfile.php?filename=thedocument.doc">Download</a>

Again, don't use this code exactly as shown!!!! Make sure you get your filename from a known safe source or test the heck out of the input to make sure only files you want seen can be accessed.

Rick
_________________
http://www.ccselite.com
View profile  Send private message
jacem

Posts: 87
Posted: 05/21/2008, 5:12 PM

Firstly thanks wkempees,andy & mentecky for your helpful responses.

Agree with wkempees that the owner of uploaded files is the webservers account and Peterr from Yes Software has said that as long as directory browsing for my destination folder is disabled then the fileupload components habit of adding timestamp to the uploaded file name should ensure that the filenames will not have 'obvious naming patterns' - a requirement alluded to by wkempees.

The CCS is in a folder below the root for the website as andy has suggested and the destination of the uploaded files is in a folder below that.

So it seems I shouldn't have worried: I just need to ensure naming is obscure and directory browsing turned off.

I am still to fathom the suggestion from mentecky as it is beyond my current knowledge & experience but seems to be about getting the server to deliver back the files to the user from html generated by php at the server backend - however this suggestion has reinforced the approach to store the filenames in the database.

Thanks for all suggestions and I am now on a path to a solution for my circumstances.
View profile  Send private message
mentecky

Posts: 321
Posted: 05/21/2008, 7:40 PM

Thanks Jacem,

My example, although maybe more complex than you need, allows the server to send files not browsable normally, and even from folders not under the web root. I use it to force a login even if a user sends a link to a file to another user.

For example... I could send you a link to a document and it would force you to login before allowing the download.

The others are correct, if just making the link hard to guess is what you are after, CCS does a good job of that.

Good luck with your project!

Rick
_________________
http://www.ccselite.com
View profile  Send private message

Add new topic Subscribe to topic   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

MS Access to Web

Convert MS Access to Web.
Join thousands of Web developers who build Web applications with minimal coding.

CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright © 2003-2004 by UltraApps.com  and YesSoftware, Inc.