admin管理员组

文章数量:1316017

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()

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()
Share Improve this question edited Dec 6, 2015 at 11:39 Remi Guan 22.3k17 gold badges67 silver badges89 bronze badges asked Apr 30, 2015 at 20:31 iqueqiorioiqueqiorio 1,1873 gold badges35 silver badges81 bronze badges 8
  • Hmm...put that file into /tmp like fh = open("/tmp/logo.png", "wb")? – Remi Guan Commented Dec 6, 2015 at 9:21
  • @KevinGuan what do you mean? – iqueqiorio Commented Dec 6, 2015 at 9:23
  • So you're trying to save a file, and then use JavaScript to send it, but don't have enough permission to write the file? – Remi Guan Commented Dec 6, 2015 at 9:25
  • @KevinGuan yes that is correct – iqueqiorio Commented Dec 6, 2015 at 9:26
  • Then as I said, I think you could put the file into /tmp if you don't need use the file after you sent it. Because the permission of that directory is 777. – Remi Guan Commented Dec 6, 2015 at 9:28
 |  Show 3 more ments

2 Answers 2

Reset to default 10 +100

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 mand 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 mand 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 mand:

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.

本文标签: javascriptSave File to Webserver from POST RequestStack Overflow