简体   繁体   中英

Save File to Webserver from POST Request

I am making a post request with some javascript to a python script in my /var/www/cgi-bin on my web server, and then in this python script I want to save the image file to my html folder, so it can later be retrieved.

Located at /var/www/html , but right now the only way I know how to do this is to set the python script to chmod 777 which I do not want to do.

So how else can I save a file that I grab from my webpage using javascript and then send to server with javascript via POST?

Currently when I do this I get an error saying the python does not have permission to save, as its chmod is 755 .

I here is python code, I know it works as the error just says I dont have permission to write the file

fh = open("/var/www/html/logo.png", "wb")
fh.write(photo.decode('base64'))
fh.close()

If you don't want to change the permission of that directory to 777 , you can change the owner of the directory to your HTTP server user, then the user of your web app will be able to write file into that directory because they have rwx - 7 permission of the directory.

To do that, via (since you're using Apache as your web server, remember login as `root):

chown -R apache:apache /var/www/cgi-bin/

Remember that then only user apache and root has rwx to that directory, and others has rx .

And this command means:

chown - change the owner of the directory
-R    - operate on files and directories recursively

apache:apache - apache user, apache group
/var/www/cgi-bin/ - the directory

Try man chown command to check the manual page of chown and learn more, here's a online version .


If you need change it back, I think the default user of that directory is root . So login as root , and run command:

chown -R root:root /var/www/cgi-bin/

We were solved the problem in chat room .

The error message is directly indicating the role/use the python server is running on doesn't have write access to the folder. You need to assign either the role or the web server user. Make sure to give only write access and not write + execute access.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM