0

The application I am developing allows users to upload avatars for their profile. Since the app uses two separate front-facing webservers, we intend to use a separate fileserver to store the image files. The code I use successfully uploads the files to a directory in the webserver. What is the best way to successfully get the files onto a fileserver? It uses an upload code called 'class.upload.php' that I found here https://www.verot.net/php_class_upload.htm

Here is the code:

require_once('class.upload.php');

$foo = new Upload($_FILES['fileToUpload']); 

if ($foo->uploaded) {

   // save uploaded image with a new name,
   // convert to .jpg
   // resized to 50px by 50px

   $foo->file_new_name_body = $tablename;
   $foo->file_auto_rename = true;
   $foo->image_convert = jpg;
   $foo->image_resize = true;
   $foo->image_ratio_crop = true;
   $foo->image_y = 50;
   $foo->image_x = 50;

   //THIS LINE CURRENTLY SPECIFIES UPLOAD PATH ON WEBSERVER
   $foo->Process('avatars/');   

  // THE LINE BELOW IS WHAT I TRIED AND GOT A 'Can't create file path' ERROR
  //$foo->Process('http://XXX.XXX.XXX.XXX/PATH/TO/avatars/');


   if ($foo->processed) {
     //echo 'image renamed, resized x=100 and converted to JPG';
     $foo->Clean();
     ...then goes on with upload processing since upload is successful.    
   }    

} 

where XXX.XXX.XXX.XXX is the IP of the fileserver.

Thanks in advance for your help. If there's anything I can do to make the question better please let me know. Thanks!

1
  • 1
    Easiest, efficient and robust is simply to mount a file system export of the file server to the http servers.
    – arkascha
    Commented Oct 12, 2016 at 21:04

1 Answer 1

1

That script is for saving to the local file system. You'll need someplace that the www-data user (or whatever user your setup runs as) can write the files to.

Set up a form that only uploads the image and have the action set to a script on the file server. This way the users upload directly to it.

Or

You'll need to save the file locally, and then some how get it to the file server. Exposing a file system export may work (if you have a private network between the www server and the file server) or you'll need to send them there some how. You could use curl in PHP to ftp the images over, you could use passwordless ssh keys and spawn a shell script that does a scp to the file server, you could synchronize every so often with rsync over ssh. Lots of options.

1
  • Amazing. I had not even thought that I could action the form directly to the fileserver. I'll try this. Commented Oct 15, 2016 at 2:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.