File upload to external server?

ShiftThis - March 10, 2005 - 03:54

When I attach a file to a post I would like to have the file uploaded to a host on another server. I just can't seem to put in an address for a directory outside of my drupal installation to another directory on a different server.

Anyone have any ideas on how to tweak the php to handle this? I'm pretty weak with my php, so it'll probably take me weeks to get it working on my own. Any help would be appreciated! Thanks!

help?

ShiftThis - March 13, 2005 - 06:26

Is there maybe a module that can handle this?

These are the modules I've tried to use unsuccessfully:
Attachment.module
Filemanager.module
Filestore.module
FSCache.module

All I need is to store my uploaded file attachements on a seperate server, can it be that hard?

Here's a non-drupal-specific solution..

javanaut - March 13, 2005 - 06:53

If you were wanting to host your "files" directory on another server (call it the "static files" server), here's one way to do it (assuming $base_url is the top level of your web site and $static_url serves the content for the $base_url/files directory):

  1. Map the $base_url/files directory to the static files server using NFS or rsync.
  2. Create a mod_rewrite rule to redirect requests for anything under $base_url/files/ to go to $static_url

rsync might have side effects when files are edited/added and directories haven't been synchronized yet, though rsync has the least impact on server performance.

An alternative to using mod_rewrite to redirect traffic might be a drupal filter that rewrites occurrences of "$base_url/files" into "$static_url". This would only really handle node content and not really anything that your theme uses.

I think your best bet would be to have $base_url/files mapped to $static_url using NFS or Samba or some other network file sharing utility. This way, changes to the actual files are seen immediately. The performance of these tools shouldn't be too much of a problem unless you're constantly adding new files (like adding multiple files per second). You'll definitely want to turn the "public files" setting on in your admin/settings, too, that way the actual file url is displayed and drupal isn't reading the file then serving it dynamically.

Good luck.

thanks

ShiftThis - March 13, 2005 - 18:53

Thanks for the suggestion, unfortunately I have no idea what NFS or rsync is. Also, I should point out that my site is run on a linux machine, but I have no access to it besides FTP.

rsync is a tool that lets

javanaut - March 13, 2005 - 19:27

rsync is a tool that lets you mirror directories. It can mirror a local directory with a remote one and is very efficient about only transfering the files that have been added or changed. You would typically run this from a cron job.

NFS lets you mount a remote directory as though it were a local one. Any files written to a locally mapped NFS share are instantly written to the remote server. NFS is typically available on *nix systems. Samba does pretty much the same thing, but using a different protocol. It is compatible with MS Windows file sharing mechanisms.

If you only have FTP access, then you'll probably need to modify the upload module to write to the remote server. You might be able to use something like "ftp://user:pwd@static_server/files" as the value for "files directory" in the drupal config, but that would probably involve some code tweaks to get it going. Specifically, there's a call to move_uploaded_file() that you'll need to update since that has security restrictions on what can be moved and where. The problem with this strategy, however, is when files are edited or deleted, the changes would also need to be sent to the remote server. This approach would also involve using mod_rewrite to redirect links to the local files dir to go to the static file server.

This sounds like a useful feature for large sites with lots of file uploads/downloads. Maybe a feature request is in order.

 
 

Drupal is a registered trademark of Dries Buytaert.