Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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()
like image 504
iqueqiorio Avatar asked Apr 30 '15 20:04

iqueqiorio


People also ask

How to download files from the web server via HTTP POST?

If id is 1, it will write my website logo to the client, else it will write my 404 icon image to the client. As with HTTP get, downloading of a file from the web server via HTTP post in C# consists of three main steps: Construct the HTTP post request to send to the web server. Send the HTTP request and get the HTTP response from the web server.

How to send data to the server using HTTP POST request?

You can send data to the server in the body of the POST message. The type and size of data are not limited. But you must specify the data type in the Content-Type header and the data size in the Content-Length header fields. The HTTP POST requests can also send data to the server using the URL parameters.

How do I send an HTTP request to a webform?

In the example below, the "method=POST" form attribute tells the browser to submit the webform using the HTTP POST method, and the "action=/login" attribute specifies the destination URL. In JavaScript, you can send HTTP requests using the XMLHttpRequest object or the new Fetch web API.

How to get the HTTP response from the web server?

Send the HTTP request and get the HTTP response from the web server. Stream httpResponseStream = httpResponse.GetResponseStream (); Sometimes, there is a need for the client to supply some information to the web server in order to download a file. This can be a case when we want to control how files are downloaded.


1 Answers

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.

like image 58
Remi Crystal Avatar answered Sep 27 '22 16:09

Remi Crystal