2016-04-13 4 views
0

Ich versuche, eine Bilddatei hochzuladen.Datei-Upload in PHP, Ajax zeigt Fehler

Das ist mein Html

<form class="form-horizontal" role="form" enctype="multipart/form-data"> 
    <div class="form-group"> 
     <label for="a" class="control-label col-sm-2">A:</label> 
     <input type="text" class="form-control col-sm-10" id="a"> 
    </div> 
    <div class="form-group"> 
     <label for="b" class="control-label col-sm-2">B:</label> 
     <input type="text" class="form-control col-sm-10" id="b"> 
    </div> 
    <div class="form-group"> 
     <label for="c" class="control-label col-sm-2">C:</label> 
     <input type="text" class="form-control col-sm-10" id="c"> 
    </div> 
    <div class="form-group"> 
     <label for="d" class="control-label col-sm-2">D:</label> 
     <input type="text" class="form-control col-sm-10" id="d"> 
    </div> 
    <div class="form-group"> 
     <label for="fupload" class="control-label col-sm-2">Upload image:</label> 
     <input type="file" class="form-control col-sm-10" id="fupload"> 
    </div> 
    <button type="button" class="btn-lg btn-primary" style="margin-left:200px" id="new_save" onclick='save_all();'>Save</button> 
</form> 

Mein JavaScript-Code

var a= _("a").value; //the _ function returns document.getElementById(x) 
var b = _("b").value; 
var c = _("c").value; 
var d = _("d").value; 

var file_data = $("#fupload").prop("files")[0]; 
var fileup = new FormData();     
fileup.append("file", file_data) 

var ajax = ajaxObj("POST", "./phps/saveall.php"); 
ajax.onreadystatechange = function() { 
     alert(ajax.responseText); 
    } 
ajax.send("a="+a+"&b="+b+"&c="+c+"&d="+d+"&fileup="+fileup); 

Schließlich mein PHP

$a = preg_replace('#[^a-z0-9()., ]#i', '', $_POST['a']); 
    $b = preg_replace('#[^a-z0-9()., ]#i', '', $_POST['b']); 
    $c= htmlentities($_POST['c']); 
    $c= mysqli_real_escape_string($db_conx, $c); 
    $d = htmlentities($_POST['d']); 
    $d = mysqli_real_escape_string($db_conx, $d); 
    $fup = $_POST['fileup']; 
    //processing a-d 
    //this is where the problem comes 
    move_uploaded_file($_FILES[$fup]['tmp_name'], '../lyrics/'.$a.'.png'); 

Wenn ich diese Variablen Anzeige ausgeführt wird verarbeitet in Ordnung, aber Datei nicht bekommen hochgeladen, aber zeigt den folgenden Fehler

"Notice: Undefined index: [object Formdata]"

Wie könnte ich dieses Problem beheben?

+0

tun, um eine 'print_r ($ _ POST)' und 'print_r ($ _ FILES)' und stellen Sie sicher, '$ _POST [ 'FileUp']' ist ein Schlüssel in '$ _FILES' – cmorrissey

+0

es FileUp scheint nicht ein geben Sie '$ _FILES' ein, ist aber ein Schlüssel in' $ _POST'. Es scheint auch nicht die Bilddaten nur der falsche Weg zum Bild zu enthalten –

+0

Was ist 'ajaxObj'? – Musa

Antwort

0

Beginnen Sie damit, Ihrem Formular mitzuteilen, dass es ein hochgeladenes Objekt (oder mehr) enthalten wird.

<form enctype="multipart/form-data" action="...put your URL here..." method="POST"> 

Lesen Sie mehr hier:

http://php.net/manual/en/features.file-upload.post-method.php

+0

Ich habe 'enctype =" multipart/form-data "' hinzugefügt. Ich möchte es durch Ajax weitergeben, so dass ich abhängig von dem Ergebnis, das von PHP zurückgegeben wird, einige Änderungen an der Seite vornehmen kann. und deshalb habe ich das Aktionsattribut –

0

Die Frage ist hier:

ajax.send("a="+a+"&b="+b+"&c="+c+"&d="+d+"&fileup="+fileup); 

Sie versuchen, eine Formdata-Objekt in einen String anzuhängen. Stattdessen sollten Sie alle anderen Werte an das FormData-Objekt anhängen und senden.

var fileup = new FormData();     
fileup.append("file", file_data); 
fileup.append("a", a); 
fileup.append("b", b); 
fileup.append("c", c); 
fileup.append("d", d); 

ajax.send(fileup); 
+0

nicht hinzugefügt Danke, ich habe '$ _POST [" a "]' in PHP versucht, aber es funktioniert nicht. Es sagt _Undefinierter Index: a_. Wie bekomme ich die Werte in PHP –

+0

Ich habe eine 'print_r ($ _ POST)' und das kam 'Array ([------ WebKitFormBoundaryaBJCF5t2LUcnvGBI Content-Disposition: _form-Daten; _name] =>" a "atext ------ WebKitFormBoundaryaBJCF5t2LUcnvGBI Inhalts-Disposition: Formulardaten; name = "b" btext ------ WebKitFormBoundaryaBJCF5t2LUcnvGBI Inhalts-Disposition: Formulardaten; Name = "c" ctext ------ WebKitFormBoundaryaBJCF5t2LUcnvGBI Inhalt -Disposition: form-data; name = "d" dtext ------ WebKitFormBoundaryaBJCF5t2LUcnvGBI Inhalts-Disposition: form-data; name = "datei"; filename = "file.png" Content-Type: image/png PNG IHDReFZ) '. –

+0

Im JavaScript vor dem Sendeaufruf versuchen Sie 'ajax.setRequestHeader ('Content-type', 'multipart/form-data');' dann sehen Sie wieder, was in dem PHP-Beitrag ist. – MrCode