1

I am SFTP'ing files to a directory on my ubuntu server. Ideally I would like these files in my apache public html folder as they are pictures that a user is uploading.

I've found that I can't simply SFTP the files directly to my public html folder, so am researching other methods. My picture server is ubuntu, so I thought there may some native command or setting that I could use to automatically move pictures that show up in my SFTP directory, to my public html directory.

Hopefully I am making sense, and I'm not sure where else I should be asking this question.

Kamron K.
  • 594
  • 1
  • 10
  • 18
  • you would probably have better change of finding the answer at superuser – Rafael Dec 10 '14 at 22:34
  • There are a lot of options for watching directories and acting on files there, but wouldn't you rather fix the problem of not being able to upload to `public_html` directly? Why _is_ that not possible? – Wrikken Dec 10 '14 at 22:45
  • In all honesty, I tried to SFTP directly there, and it just resulted in perm errors, did a bit of research on the error and the public_html directory does not allow it for some reason set by apache – Kamron K. Dec 11 '14 at 00:28

1 Answers1

4

Three possibilities:

  1. Why can you not simply upload the files directly in your public html folder? I assume that has something to do with access restrictions in writing to that directory, so you could try to change this directories write permissions for the user you are uploading as. The access restrictions are changed with the command chmod, the ownership of diles and directories is changed with chown. Best you read documentation to these commands ("man chmod" and "man chown").

  2. You can run a script periodically that takes all uploaded files and moves them to the specified target dir. For this you need to write a short shell script in bash, for example:

    #!/bin/bash
    mv /home/user/UPLOADS/*.jpg /var/www/images/
    

    (This script takes simply all files with the extension .jpg from directory /home/user/UPLOADS and puts them without further check to the directory /var/wwww/images )

    Place this script somewhere (eg. /home/user/bin/) and make it executable: chmod a+x /home/user/bin/SCRIPTNAME

    This script can be run periodically via cron, call crontab -e and write a new line like so:

    */5 * * * * /home/user/bin/SCRIPTNAME
    

    that executes the script every 5 minutes.

    Drawback is that it is called every 5 minutes, so there might be a gap between upload and move of max 5 minutes. additionally, if the script runs WHILE uploading of new images, something strange might happen...

  3. The 3rd possibility is to execute a script as soon as the upload is finished by watching the upload directory with the inotify feature of the kernel. If you want to do this, best google for inotify examples, that is a little bit more complicated. Here is another SO answer to that: Monitor Directory for Changes

Community
  • 1
  • 1
Alexander Köb
  • 944
  • 1
  • 9
  • 19
  • Awesome, thank you! and yes, I cannot upload directly there becuase of something set by apache. I've tried changing the perm's of the directory which resulted in the public_html directory not being reachable by a browser – Kamron K. Dec 11 '14 at 00:30
  • ok, I assume the following: apache runs as user wwwrun, the public_html directory is in the home directory of the user you are using for sftp (I call this user simply "user"). Then ssh to the box and go to the public_html dir and run "chmod -R 777 *". This gives full access to all files including subdirs to everyone on the system (attention: not particular secure). Then you should be able to write to this directory as SFTP user and also read as apache user. from there on you can play around with permissions and groups until you have a more secure setup. – Alexander Köb Dec 12 '14 at 00:54
  • I just went ahead and wrote a sh script to one per second, move all files from the sftp directory to the public_html directory. It's a bit of a cumbersome approach, and not the most professional, but it works :) it was one of your ideas, thanks a ton for the help – Kamron K. Dec 19 '14 at 01:52
  • take care that you are not moving away files in the middle of them being uploaded. With inotify you can check whether the file is still written to or not. – Alexander Köb Jan 02 '15 at 11:34