<?php
$chataudiopath = "uploadss/";
$tmp_dir = "tmp/";
$target_dir = $chataudiopath;
if (!file_exists($target_dir)) { mkdir($target_dir, 0777, true); } $mname = date("Y") . date("m") . date("d"); $tarihPath = $mname . "/"; $new_dir = $chataudiopath . $tarihPath; if (!file_exists($new_dir)) { mkdir($new_dir, 0777, true); copy($chataudiopath . "index.php", $new_dir . "index.php"); }
$target_file = $new_dir . basename($_FILES["file"]["name"]);
$uploadOk = 1;
$fileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
if (isset($_FILES["file"])) { $check = filesize($_FILES["file"]["tmp_name"]); if ($check !== false) { echo "File is an audio file - " . $check . " bytes."; $uploadOk = 1; } else { echo "File is not an audio file."; $uploadOk = 0; } }
if (file_exists($target_file)) { echo "Sorry, file already exists."; $uploadOk = 0; }
if ($fileType != "3gp" && $fileType != "mp3" && $fileType != "wav" && $fileType != "m4a" && $fileType != "aac") { echo "Sorry, only 3GP, MP3, WAV, M4A, and AAC files are allowed."; $uploadOk = 0; }
if ($uploadOk == 0) { echo "Sorry, your file was not uploaded."; } else { if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) { echo "The file " . htmlspecialchars(basename($_FILES["file"]["name"])) . " has been uploaded."; } else { echo "Sorry, there was an error uploading your file."; } }
?>koduyla yüklüyorum dosyayı.sorun şu ki bir dosya bozulmadan yükleniyor ve sonrakileri bozuyor. yaklaşık 30dk geçene kadaarda tekrar düzgün yüklemiyor.
java tarafında sorun yok. cache te sesler dinleniyor. ancak sunucuda bu olay gerçekleşiyor. sunucu tarafında bir ayarmı yapmam gerekiyor? bu dosyalar sürekli yüklenecek. 30 dk beklemek gibi birşey hiç mantıklı olmaz.
public class UploadAudioTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String filePath = params[0];
try {
return uploadFileA(filePath, "https://xxxxxxxxxxxxx/vupload.php");
} catch (IOException e) {
e.printStackTrace();
return "Upload failed: " + e.getMessage();
}
}
private String uploadFileA(String filePath, String serverUrl) throws IOException {
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "AaB03x87yxdkjnxvi7";
int maxBufferSize = 20 * 1024;
byte[] buffer;
int bytesRead, bytesAvailable, bufferSize;
File file = new File(filePath);
if (!file.exists()) {
throw new IOException("File not found: " + filePath);
}
FileInputStream fileInputStream = new FileInputStream(file);
URL url = new URL(serverUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("ENCTYPE", "multipart/form-data");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
connection.setRequestProperty("file", filePath);
DataOutputStream dos = new DataOutputStream(connection.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"file\";filename=\"" + filePath + "\"" + lineEnd);
dos.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
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);
int serverResponseCode = connection.getResponseCode();
String serverResponseMessage = connection.getResponseMessage();
fileInputStream.close();
dos.flush();
dos.close();
if (serverResponseCode == HttpURLConnection.HTTP_OK) {
//sendA();
} else {
throw new IOException("Server returned non-OK status: " + serverResponseCode);
}
return serverResponseMessage;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
// Toast.makeText(context, result, Toast.LENGTH_LONG).show();
}
}java tarafıda bu şekilde..acaba bu kısımdamı bir byte şeklinde bişey yaşıyorum... ama o zamanda sanırım hiç bir dosyayı yüklemezdi... request kısmıyla ilgili bir sorunda olabilir.