2016-06-04 13 views
0

Ich versuche, ein Array von Variablen des Typs '$ _FILES' durchlaufen von einem Formular und jedes Bild verarbeitet und speichern Verzeichnisse

Der Code ist

if(isset($_POST['travsubli'])){ 

$date=Date('Y-m-d'); 
$uname=substr($utravlastname,0,5).substr($utravphone,5,10); 
$destin=str_replace(array(" ","/","."),"-",$_POST['Destination']); 
if(!is_dir("images/userimages/travelphotos")) {mkdir("images/userimages/travelphotos");} 
if(!is_dir("images/userimages/travelphotos/$uname")){mkdir("images/userimages/travelphotos/$uname");} 
echo "Check : ".$_FILES['TourPhoto1']['name']."<br>"; 
$photosarray=array($_FILES['TourPhoto1']['name'],$_FILES['TourPhoto2']['name'],$_FILES['TourPhoto3']['name'],$_FILES['TourPhoto4']['name'],$_FILES['TourPhoto5']['name'],$_FILES['TourPhoto6']['name']); 

$pai=1; 
foreach($photosarray as $pha){ 
$ext=pathinfo($pha,PATHINFO_EXTENSION); 
if(!empty($ext)){ 

$newphotoname="Travelogue-Photo-$destin-$pai.$ext"; 
$newphotopath="images/userimages/travelphotos/$uname/$newphotoname"; 

     if(move_uploaded_file($pha,$newphotopath)){ 
      echo "Photo Successfully Uploaded !<br>"; 
     }else{echo "Something went wrong with Photo Upload !<br>";  $e=error_get_last();echo $e['message']."<br>";} 
     }  
$TourPhoto="TourPhoto$pai"; 
$$TourPhoto=$newphotoname; 
echo $pha."->$TourPhoto->".$$TourPhoto.$pai."<br>"; 
$pai++; 
} 

Fragen und eher propblem ist,

move_uploaded_file funktioniert nicht.

Ich nehme an, es ist, weil ich $ _FILES ['Variable'] ['Name'] anstelle von $ _FILES ['Variable'] ['tmp_name'] im Befehl move_uploaded_file verwendet.

Bin ich richtig in meiner Diagnose? Wenn ja, was sollte getan werden? Weil ich den Parameter 'name' im Array der Schleife zugewiesen habe. Wie mache ich es in 'tmp_name' innerhalb der Schleife rückgängig? Oder gibt es eine andere bessere Alternative?

+0

Warum können Sie nicht verwenden, nur 'tmp_name'? –

+0

@u_mulder Wenn ich tmp_name verwende, kann ich die tatsächliche Erweiterung der Datei nicht erhalten. Es gibt .tmp – user3526204

+0

Dann beide Felder kombinieren. –

Antwort

1

Ich weiß nicht, was wirkliches Problem ist, aber Sie können dies versuchen:

$photosarray = array( 
    array(
     'tmp_name' => $_FILES['TourPhoto1']['tmp_name'], 
     'real_name' => $_FILES['TourPhoto1']['name'], 
    ), 
    array(
     'tmp_name' => $_FILES['TourPhoto2']['tmp_name'], 
     'real_name' => $_FILES['TourPhoto2']['name'], 
    ), 
    array(
     'tmp_name' => $_FILES['TourPhoto3']['tmp_name'], 
     'real_name' => $_FILES['TourPhoto3']['name'], 
    ), 
    // etc 
); 

foreach ($photosarray as $pha) { 
    $ext = pathinfo($pha['real_name'],PATHINFO_EXTENSION); 
    $path = ''; 
    // make your path here 

    // copy file 
    move_uploaded_file($pha['tmp_name'], $new_path); 
    // do other stuff 
} 
+0

Sie sind ein Genie! Weil du mein Problem wirklich verstanden hast und meinen Tag gerettet hast! Danke vielmals ! – user3526204

Verwandte Themen