PHP上传文件中的未定义索引

时间:2014-08-19 13:49:30

标签: php android file-upload

我正在尝试使用应用程序将文件从手机上传到我的电脑。我有一个Android应用程序和一个带有PHP脚本的网络服务器。当我尝试上传文件时,PHP脚本Undefined index: uploadedfile in .... on line 3Undefined index: uploadedfile in .... on line 4出现以下错误。我不知道问题出在哪里以及为什么会发生。我的Android代码如下:

public class Upload {
URL connectURL;
String responseString;
String fileName;
byte[] dataToServer;
Upload(String urlString, String fileName ){
    try{
        connectURL = new URL(urlString);
    }catch(Exception ex){
        Log.i("URL FORMATION","MALFORMATED URL");
    }
    this.fileName = fileName;
}
void doStart(FileInputStream stream){
    fileInputStream = stream;
    thirdTry();
}
FileInputStream fileInputStream = null;
void thirdTry() {
    String existingFileName = fileName;
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "*****";
    String Tag="3rd";
    try{
        HttpURLConnection conn = (HttpURLConnection) connectURL.openConnection();
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Connection", "Keep-Alive");
        conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
        DataOutputStream dos = new DataOutputStream( conn.getOutputStream() );
        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"archivo[]\";filename=\"" + existingFileName +"\"" + lineEnd);
        dos.writeBytes(lineEnd);
        int bytesAvailable = fileInputStream.available();
        int maxBufferSize = 1024;
        int bufferSize = Math.min(bytesAvailable, maxBufferSize);
        byte[] buffer = new byte[bufferSize];
        int 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);
        fileInputStream.close();
        dos.flush();
        InputStream is = conn.getInputStream();
        int ch;
        StringBuffer b =new StringBuffer();
        while( ( ch = is.read() ) != -1 ){
            b.append( (char)ch );
        }
        String s=b.toString();
        Log.e("Response",s);
        dos.close();
    }
    catch (MalformedURLException ex){
        Log.e(Tag, "error: " + ex.getMessage(), ex);
    }

    catch (IOException ioe){ 
        Log.e(Tag, "error: " + ioe.getMessage(), ioe);
    }
}

我的PHP代码:

$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ". basename( $_FILES['uploadedfile']['name'])." has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}

3 个答案:

答案 0 :(得分:0)

您的参数不匹配:

dos.writeBytes("Content-Disposition: form-data; name=\"archivo[]\"; ...snip...
                                                       ^^^^^^^

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
                                                 ^^^^^^^^^^^^

一个简单的var_dump($_FILES)会告诉你这个。

答案 1 :(得分:0)

选项A:

更改以下内容:

dos.writeBytes("Content-Disposition: form-data; name=\"archivo[]\";filename=\"" + existingFileName +"\"" + lineEnd);` 

dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + existingFileName +"\"" + lineEnd);

选项B:

更改

的所有引用
$_FILES['uploadedfile']

$_FILES['archivo']

答案 2 :(得分:0)

首先,您需要将name=\"archivo[]\";更改为name=\"uploadedfile[]\";

如果您为脚本添加更多逻辑,则可以避免错误,请尝试以下操作:

if(is_uploaded_file($_FILES['uploadedfile']['tmp_name'])){ 
    $folder = "uploads/"; 
    $file = basename( $_FILES['uploadedfile']['name']); 
    $target_path = $folder.$file; 
    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { 
        echo "The file ". $file." has been uploaded";

    } else { 
       echo "upload received! but process failed";
    } 
}else{ 
    echo "upload failure ! Nothing was uploaded";
}