How to upload an image or file to php server?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Manuel

It is quite simple what I want to do, I just want to upload a .png image to the server. It’s something I’ve done before on a local server, the difference is that locally I placed as url in the request /img_folder.

I got this code from here https://forum.godotengine.org/79098/httprequest-or-httpclient-for-upload-file-to-server

This is what I have for now, the error I get is:

Condition “status != STATUS_CONNECTED” is true. Returning: ERR_INVALID_PARAMETER
<C++ Source> core/io/http_client_tcp.cpp:153 @ request()

func _upload_to_server( path_to_png : String ):
	
	var file_name := path_to_png.get_file()
	var file := FileAccess.open(path_to_png, FileAccess.READ)
	var file_content = file.get_buffer( file.get_length( ) )
	
	var body := PackedByteArray()
	body.append_array( "\r\n--WebKitFormBoundaryePkpFF7tjBAqx29L\r\n".to_utf8_buffer() )
	body.append_array( "Content-Disposition: form-data; name=\"image\"; filename=\"".to_utf8_buffer() )
	body.append_array( file_name.to_utf8_buffer() )
	body.append_array( "\"\r\n".to_utf8_buffer() )
	body.append_array( "Content-Type: image/png\r\n\r\n".to_utf8_buffer() )
	body.append_array( file_content )
	body.append_array( "\r\n--WebKitFormBoundaryePkpFF7tjBAqx29L--\r\n".to_utf8_buffer() )
	
	var headers := PackedStringArray()
	headers.append("Content-Type: multipart/form-data;boundary=\"WebKitFormBoundaryePkpFF7tjBAqx29L\"")
	
	var http = HTTPClient.new()
	http.connect_to_host( "https://mywebsite.com/server", 80 )
	
	while http.get_status() == HTTPClient.STATUS_CONNECTING or http.get_status() == HTTPClient.STATUS_RESOLVING:
		
		http.poll()
		OS.delay_msec(500)
	
	var err = http.request_raw(HTTPClient.METHOD_POST, "/upload.php" , headers, body)
	
	if err != OK:
		print("error ", err)
		return
	
	while http.get_status() == HTTPClient.STATUS_REQUESTING:
		
		# Keep polling for as long as the request is being processed.
		http.poll()
		
		if not OS.has_feature("web"):
			OS.delay_msec(500)
			return
		
		print("requesting")
		
		await Engine.get_main_loop().idle_frame
	# -------------------------------------------------------------------------------------
:bust_in_silhouette: Reply From: Manuel

I did it!!

func _upload_png( path_to_png : String ):
	
	var file_name := path_to_png.get_file()
	var file := FileAccess.open(path_to_png, FileAccess.READ)
	var file_content = file.get_buffer( file.get_length( ) )
	
	var body := PackedByteArray()
	body.append_array( "\r\n--WebKitFormBoundaryePkpFF7tjBAqx29L\r\n".to_utf8_buffer() )
	body.append_array( "Content-Disposition: form-data; name=\"image\"; filename=\"".to_utf8_buffer() )
	body.append_array( file_name.to_utf8_buffer() )
	body.append_array( "\"\r\n".to_utf8_buffer() )
	body.append_array( "Content-Type: image/png\r\n\r\n".to_utf8_buffer() )
	body.append_array( file_content )
	body.append_array( "\r\n--WebKitFormBoundaryePkpFF7tjBAqx29L--\r\n".to_utf8_buffer() )
	
	var headers := PackedStringArray()
	headers.append("Content-Type: multipart/form-data;boundary=\"WebKitFormBoundaryePkpFF7tjBAqx29L\"")
	
	var http = HTTPClient.new()
	http.connect_to_host( "https://website.com", 443 )
	
	while http.get_status() == HTTPClient.STATUS_CONNECTING or http.get_status() == HTTPClient.STATUS_RESOLVING:
		
		http.poll()
		OS.delay_msec(500)
		
		print("conectando...")
	
	if http.get_status() != HTTPClient.STATUS_CONNECTED:
		print("no conectado ", http.get_status())
		return
	
	var err = http.request_raw(HTTPClient.METHOD_POST, "/php_server/php_file_code.php" , headers, body)
	
	if err != OK:
		print("error sending ", err)
		return
	
	while http.get_status() == HTTPClient.STATUS_REQUESTING:
		
		print("requesting")
		
		http.poll()
		
		if not OS.has_feature("web"):
			OS.delay_msec(500)
		else:
			await Engine.get_main_loop().idle_frame
	# -------------------------------------------------------------------------------------
	
	# IF THERE A RESPONSE -----------------------------------------------------------------
	if http.has_response():
		
		#headers = http.get_response_headers_as_dictionary() # Get response headers.
		print("code: ", http.get_response_code())           # Show response code.
		#print("headers: ", headers)                     # Show headers.
		
		# Getting the HTTP Body
		if http.is_response_chunked():
			# Does it use chunks?
			print("Response is Chunked!")
		else:
			# Or just plain Content-Length
			var bl = http.get_response_body_length()
			print("Response Length: ", bl)
		
		# This method works for both anyway
		var resp_body := PackedByteArray()
		
		while http.get_status() == HTTPClient.STATUS_BODY:
			
			# While there is body left to be read
			http.poll()
			
			# Get a chunk.
			var chunk = http.read_response_body_chunk()
			if chunk.size() == 0:
				if not OS.has_feature("web"):
					OS.delay_usec(1000)
				else:
					await Engine.get_main_loop().idle_frame
			else:
				resp_body = resp_body + chunk
		
		# Done!
		var text := resp_body.get_string_from_utf8().strip_edges()