2013-02-20 4 views
1

Actuellement, je télécharge un fichier xml de mac vers un serveur. Ça fonctionne bien. Voici le code:Téléchargement de plusieurs fichiers avec le script PHP et iOS

Code osx

NSString *urlString1 = @"http://username:[email protected]/files/upload.php"; 


NSMutableURLRequest *request1 = [[NSMutableURLRequest alloc] init]; 
[request1 setURL:[NSURL URLWithString:urlString1]]; 
[request1 setHTTPMethod:@"POST"]; 



NSString *boundary1 = @"---------------------------14737809831466499882746641449"; 
NSString *contentType1 = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary1]; 
[request1 addValue:contentType1 forHTTPHeaderField:@"Content-Type"]; 

NSMutableData *body1 = [NSMutableData data]; 
[body1 appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary1] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body1 appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"data.xml\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
[body1 appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
[body1 appendData:[NSData dataWithData:xmlData]]; 
[body1 appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary1] dataUsingEncoding:NSUTF8StringEncoding]]; 
[request1 setHTTPBody:body1]; 
NSLog(@"xml data %@",xmlData); 
NSData *returnData1 = [NSURLConnection sendSynchronousRequest:request1 returningResponse:nil error:nil]; 

et script PHP:

<?php 
    $target_path = "./"; 
    $target_path = $target_path.'data.xml'; 
    if(move_uploaded_file($_FILES['userfile']['tmp_name'],$target_path)) { 
    echo "The file has been uploaded"; 
    } else{ 
    echo "There was an error uploading the file, please try again!"; 
    } 
?> 

Maintenant, j'ai un cas où je suis censé ajouter plusieurs des dossiers. Alors, comment fonctionne le nommage de fichiers? Quelqu'un s'il vous plaît aidez-moi à ce sujet.

Répondre

0

Je pense que vous pouvez vérifier avec la fonction PHP Array si cela fonctionne, c'est bon. Je ne suis pas sûr à propos de XCode mais permet de s'en tenir à l'HTML de base pour le téléchargement de plusieurs fichiers. Essayez le code ci-dessous:

Plusieurs fichiers peuvent être sélectionnés et téléchargés en utilisant le

<input type='file' name='file[]' multiple> 

L'exemple de script php qui fait le téléchargement:

<html> 
<title>Upload</title> 
<?php 
    session_start(); 
    $target=$_POST['directory']; 
     if($target[strlen($target)-1]!='/') 
       $target=$target.'/'; 
      $count=0; 
      foreach ($_FILES['file']['name'] as $filename) 
      { 
       $temp=$target; 
       $tmp=$_FILES['file']['tmp_name'][$count]; 
       $count=$count + 1; 
       $temp=$temp.basename($filename); 
       move_uploaded_file($tmp,$temp); 
       $temp=''; 
       $tmp=''; 
      } 
    header("location:../../views/upload.php"); 
?> 
</html> 

Les fichiers sélectionnés sont reçus comme un tableau avec

$ _FILES ['file'] ['nom'] [0] stocke le nom du premier fichier. $ _FILES ['file'] ['name'] [1] stocke le nom du deuxième fichier. etc.

Questions connexes