Upload a multi-page string to the w / $ _ POST PHP server

therefore, I want it to be configured so that the user uploads the video there, there the email and the key code product go with it, so in PHP I can create a new directory with email. Then name the video as a key code product. I can load the video perfectly if I delete the code trying to load the line.

Android Code:

try{ FileInputStream fileInputStream = new FileInputStream(sourceFile); URL url = new URL(UploadVideo_URL); conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); conn.setDoOutput(true); conn.setUseCaches(false); conn.setRequestMethod("POST"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("ENCTYPE", "multipart/form-data"); conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); conn.setRequestProperty("myFile", selectedPath); dos = new DataOutputStream(conn.getOutputStream()); dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes("Content-Disposition: form-data; name=\"" + ProductOwnerEmail + "\"" + lineEnd); dos.writeBytes(lineEnd); dos.write(ProductOwnerEmail.getBytes()); dos.writeBytes(lineEnd); dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes("Content-Disposition: form-data; name=\"" + ProductKeyCode + "\"" + lineEnd); dos.writeBytes(lineEnd); dos.write(ProductOwnerEmail.getBytes()); dos.writeBytes(lineEnd); dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes("Content-Disposition: form-data; name=\"myFile\";filename=\"" + selectedPath + "\"" + lineEnd); dos.writeBytes(lineEnd); bytesAvailable = fileInputStream.available(); Log.i("Huzza", "Initial .available : " + bytesAvailable); bufferSize = Math.min(bytesAvailable, maxBufferSize); buffer = new byte[bufferSize]; bytesRead = fileInputStream.read(buffer, 0, bufferSize); while (bytesRead > 0) { dos.write(buffer, 0, bufferSize); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); bytesRead = fileInputStream.read(buffer, 0, bufferSize); } dos.writeBytes(lineEnd); dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); serverResponseCode = conn.getResponseCode(); fileInputStream.close(); dos.flush(); dos.close(); } catch (Exception e) { e.printStackTrace(); return "Product upload failed"; } 

PHP code:

 <?php $ProductOwnerEmail = $_POST['ProductOwnerEmail']; $ProductKeyCode = $_POST['ProductKeyCode']; $NewDirectory = "/var/www/html/ProductVideos/" . $ProductOwnerEmail; mkdir($NewDirectory, 0777, true); if($_SERVER['REQUEST_METHOD']=='POST'){ $file_name = $_FILES['myFile']['name']; $file_size = $_FILES['myFile']['size']; $file_type = $_FILES['myFile']['type']; $temp_name = $_FILES['myFile']['tmp_name']; $location = "/var/www/html/ProductVideos/$ProductOwnerEmail/" . $ProductKeyCode; move_uploaded_file($temp_name, $location); } ?> 

If you have another way to do this or there is nothing wrong, tell me! thanks!

0
source share
1 answer

try using MultipartEntity with a list in that you add text text with a standard key value. Example mail method Try adapting it if you want. then call your message with your parameters in AsynchTask, for example. take care using the HttpClient. it is deprecated and replaced with HTTpClientbuilder.

  // traitement nameValuePairs.add(new BasicNameValuePair(Document, imagepath)); nameValuePairs.add(new BasicNameValuePair(TITLE, "toto")); nameValuePairs.add(new BasicNameValuePair(NAME, value)); nameValuePairs.add(new BasicNameValuePair(FIRSTNAME, value1)); nameValuePairs.add(new BasicNameValuePair(DEscription, value3)); public String post(String url, List<NameValuePair> nameValuePairs) { String result ="fail"; HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); HttpPost httpPost = new HttpPost(url); try { MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null, Charset.forName("UTF-8")); for (int index = 0; index < nameValuePairs.size(); index++) { if (nameValuePairs.get(index).getName().equalsIgnoreCase(Document)) { // If the key equals to "image", we use FileBody to transfer the data entity.addPart(nameValuePairs.get(index).getName(), new FileBody(new File(nameValuePairs.get(index).getValue()))); } else { Log.d(TAG, "getname:" + nameValuePairs.get(index).getName()); Log.d(TAG, "getvalue:" + nameValuePairs.get(index).getValue()); // Normal string data if (nameValuePairs.get(index).getValue() != null) { entity.addPart(nameValuePairs.get(index).getName(), new StringBody(nameValuePairs.get(index).getValue())); } else { // traitement try { runOnUiThread(new Runnable() { @Override public void run() { //traitment } }); } catch (InterruptedException e) { e.printStackTrace(); } } } } httpPost.setEntity(entity); HttpResponse response = httpClient.execute(httpPost, localContext); InputStream inputStream = response.getEntity().getContent(); System.out.println(response.getStatusLine().getStatusCode()); Log.d("response=", String.valueOf(response.getStatusLine().getStatusCode())); if (String.valueOf(response.getStatusLine().getStatusCode()).contains("200")) { result = "sucess"; } else { result = "fail"; } Log.d("response=" , String.valueOf(response.getStatusLine())); } catch (IOException e) { Log.e(TAG, "error upload file", e); } return result; } 
0
source

Source: https://habr.com/ru/post/955547/


All Articles