使用php上传的文件上传:$ _post [' fileName']
我想将文件从我的Android应用程序上传到我的PHP服务器(通过HTTPS上传URL),
这是我的php代码:
$path = "../uploads";
if (!file_exists($path)) {
mkdir($path, 0777, true);
}
$filePath = $path."/".$_POST['filename'];
move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $filePath)
$ _ files的内容['uploaded_file']
是:
{"name":"scan.gltf","type":"","tmp_name":"\/srv\/data\/tmp\/phpmEM42e","error":0,"size":5071}
所以一切似乎都可以我得到文件的名称及其正确的大小。
但是,当我记录$ _ post ['filename']
时,我会得到一个空字符串。
怎么可能?如何解决问题?
在我的Android应用程序中的Java代码下方的下方,但我认为问题不是来自那里(也许我错了..):
FileInputStream fileInputStream = new FileInputStream(sourceFile);
URL url = new URL(urlString);
// Open a HTTP connection to the URL
// TODO HttpURLConnection or HttpsURLConnection ???
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true); // Allow Inputs
conn.setDoOutput(true); // Allow Outputs
conn.setUseCaches(false); // Don't use a Cached Copy
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE", "multipart/form-data");
// TODO Space or no space before 'boundary=' ?
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
conn.setRequestProperty("uploaded_file", fileUrl);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
// TODO Space or no space before 'filename=' ?
dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\"; filename=\"" + fileUrl + "\"" + lineEnd);
dos.writeBytes(lineEnd);
// create a buffer of maximum size
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// read file and write it into form...
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);
}
// send multipart form data necessary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// Responses from the server (code and message)
int serverResponseCode = conn.getResponseCode();
String serverResponseMessage = conn.getResponseMessage();
I want to upload files from my Android app to my php server (through a https upload URL)
Here is my php code :
$path = "../uploads";
if (!file_exists($path)) {
mkdir($path, 0777, true);
}
$filePath = $path."/".$_POST['filename'];
move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $filePath)
The content of $_FILES['uploaded_file']
is:
{"name":"scan.gltf","type":"","tmp_name":"\/srv\/data\/tmp\/phpmEM42e","error":0,"size":5071}
So everything seems ok as I get the name of my file and its correct size.
But when I log $_POST['filename']
, I get a null string.
How is that possible ? How to solve the problem ?
Here below my Java code from my Android app but I don't think that the problem comes from there (perhaps I am wrong..):
FileInputStream fileInputStream = new FileInputStream(sourceFile);
URL url = new URL(urlString);
// Open a HTTP connection to the URL
// TODO HttpURLConnection or HttpsURLConnection ???
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true); // Allow Inputs
conn.setDoOutput(true); // Allow Outputs
conn.setUseCaches(false); // Don't use a Cached Copy
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE", "multipart/form-data");
// TODO Space or no space before 'boundary=' ?
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
conn.setRequestProperty("uploaded_file", fileUrl);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
// TODO Space or no space before 'filename=' ?
dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\"; filename=\"" + fileUrl + "\"" + lineEnd);
dos.writeBytes(lineEnd);
// create a buffer of maximum size
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// read file and write it into form...
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);
}
// send multipart form data necessary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// Responses from the server (code and message)
int serverResponseCode = conn.getResponseCode();
String serverResponseMessage = conn.getResponseMessage();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请使用
$ _文件
而不是$ _ POST
如果您上传文件,则永远不会得到
$ _ post
响应,只有$ _文件< /代码>保留您要搜索的变量。
Please use
$_FILES
instead of$_POST
if you upload a file, you never get a
$_POST
response, only$_FILES
hold the variables you are searching for.