使用php上传的文件上传:$ _post [' fileName']

发布于 2025-01-30 19:36:42 字数 2522 浏览 2 评论 0原文

我想将文件从我的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦毁影碎の 2025-02-06 19:36:43

请使用$ _文件而不是$ _ 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文