|
Invité régulier
 salma mathlouthi Étudiant Inscription : octobre 2012 Messages : 11 Détails du profil  Informations personnelles : Nom :  salma mathlouthi Localisation : France, Essonne (Île de France) Informations professionnelles :
Activité : Étudiant Secteur : High Tech - Éditeur de logiciels Informations forums :
Inscription : octobre 2012 Messages : 11 Points : 6 Points : 6
|
Envoi d'image via requête http a un serveur distant
Bonjour tout le monde,
Voilà je suis debute avec l'objective c et je développe une application qui sélectionne une foto depuis l'iphone et l'envoie via requête http a un script php qui s'en charge d'enregistré l'image avec un chemin bien defini.
voici mon script php :
Code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
<?php
define('NL',"\n");
define('BR',"<br/>");
check();
session_start();
$root_path = '/home/web/autocadre.com';
$dossier = '/images/photos_voiture_iphone';
$fichier = basename($_FILES['monfichier']['name']);
$type = $_FILES['monfichier']['type'];
$taille_maxi = 300000000;
$taille = filesize($_FILES['monfichier']['tmp_name']);
$extension = strrchr($_FILES['monfichier']['name'], '.');
$nom = basename($fichier,$extension);
$out = getcwd().'['.$fichier.']['.$extension.'] - ('.$taille.' octets) [type='.$type.']';
writelog_array( ALL,__FILE__." ".__FUNCTION__."(".__LINE__.")"." files:", $_FILES);
if (( $pos = strstr( $type, 'image')) === false ) {
xml_error( xml_string( $out.BR.NL."Ce fichier n'est pas de type image.($pos)")).BR.NL;
}
if( $taille > $taille_maxi) {
$_SESSION['messUp']="Le fichier est trop gros...";
xml_error( xml_string( $out."\n"."Fichier trop important ou invalide."));
}
/*
echo '$_FILES : '.nl2br( print_r( $_FILES, true)).BR;
echo '$_POST : '.nl2br( print_r( $_POST, true)).BR;
echo '$_GET : '.nl2br( print_r( $_GET, true)).BR;
echo '$_REQUEST : '.nl2br( print_r( $_REQUEST, true)).BR;
echo '$_SESSION : '.nl2br( print_r( $_SESSION, true)).BR;
echo '<b>';
echo 'Fichier : '.$fichier.BR;
echo 'Extension : '.$extension.BR;
echo 'Type : '.$type.BR;
echo 'Taille : '.$taille.BR;
echo 'Taille maxi : '.round((($taille_maxi/1024)/1024),2).' Mo'.BR;
echo 'Nom : '.$nom.BR;
echo '</b>';
*/
$sql = "select * from autos where id_auto = $nom and membre_id = ".$_SESSION['id_membre'];
// echo $sql.BR;
$result = & Query( $sql, __FILE__,__LINE__,__FUNCTION__);
test_mysql_request( $sql);
$found = false;
$pos = 0;
while( $row = mysql_fetch_array( $result, MYSQL_ASSOC)) {
// echo nl2br( print_r( $row, true)).BR;
foreach( $row as $key => $value) {
if ( strstr( $key, 'photo') !== false) {
// trouvé une photo
// echo 'Photo trouvée ('.$key.') => '.$value.BR;
$num = ( substr( $key, strlen('photo')));
if ( empty( $num)) $num = 1;
if ( is_null( $value) || empty( $value)) {
// echo 'Valeur vide trouvée'.BR;
$found = true;
$pos = $num;
break;
}
/* Limite des 3 photos annulée.
// echo 'NUMERO : ('.$key.') => '.$num.BR;
switch( $num) {
case 1:
case 2:
case 3:
if ( is_null( $value) || empty( $value)) {
// echo 'Valeur vide trouvée'.BR;
$found = true;
$pos = $num;
break;
} else {
// echo 'Photo déjà occupée'.BR;
}
break;
default :
// echo '['.$num .'] trop important, ignoré.'.BR;
break;
}
*/
}
if ( $found) break;
}
if ($found) break;
}
if ( !$found)
xml_error( xml_string( "Tous les emplacements sont déjà occupés. Ajout photo impossible."));
switch( $num) {
case 1 : $photo = 'photo';
break;
default : $photo = 'photo'.$num;
}
// echo 'Photo remplacée ou insérée ['.$photo.']'.BR;
$sql = "update autos set `$photo` = '".$nom."_".$num.$extension."', "
. "`image_path`='$dossier' "
. "WHERE `id_auto`='$nom' ";
// echo $sql.BR;
$result = & Query( $sql, __FILE__,__LINE__,__FUNCTION__);
test_mysql_request($sql);
// echo "<H1>Mise à jour correcte......</H1>";
if(!isset($erreur)) {
$fichier = preg_replace('/([^.a-z0-9]+)/i', '-', $fichier);
move_uploaded_file($_FILES['monfichier']['tmp_name'], $root_path.'/'.$dossier.'/'.$nom."_".$num.$extension);
}
echo_simple_plist_xml( xml_string("$photo mise à jour : $nom"."_".$num.$extension));
// echo '<h3>Sortie normale</h3>';
?> |
voici le code Objective c: dans le fichier.m
[bimage1 addTarget:self action:@selector(loadimageVoiture  forControlEvents:UIControlEventTouchUpInside];
Code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
- (void) loadimageVoiture:(id)sender
{
indexButton =[sender tag];
UIActionSheet* actionSheet = [[UIActionSheet alloc] initWithTitle:@"Choisir une photo ?" delegate:self cancelButtonTitle:@"Annuler"
destructiveButtonTitle:nil otherButtonTitles:@"Sélectionner photo", @"Prendre photo", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[actionSheet showInView:[[[UIApplication sharedApplication] windows] objectAtIndex:0]];
[actionSheet setTag:1 ];
[actionSheet release];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(actionSheet.tag == 1){
if(buttonIndex == 0)
{
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentModalViewController:picker animated:YES];
[picker release];
}
if(buttonIndex == 1)
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
// un appareil photo est disponible
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
[picker release];
}
else
{
// affichage qu'il n'y a pas d'appareil
NSString *imageComp=[NSString stringWithFormat:@"%@",@"PasAppareil.png"];
UIImage *imageFromFile = [UIImage imageNamed:imageComp];
imageView.image = imageFromFile;
[imageFromFile release];
}
}
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissModalViewControllerAnimated:YES];
// imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
// [AutoCadreAppDelegate sharedInstance].simageAnnonce = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
// [AutoCadreAppDelegate sharedInstance].simageAnnonce = [[[AutoCadreAppDelegate sharedInstance]simageAnnonce] imageByScalingAndCroppingForSize:CGSizeMake(200, 267)];
UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
switch (indexButton) {
case 1:
[bimage1 setBackgroundImage:image forState:UIControlStateNormal];
[AutoCadreAppDelegate sharedInstance].simageAnnonce = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
[AutoCadreAppDelegate sharedInstance].simageAnnonce = [[[AutoCadreAppDelegate sharedInstance]simageAnnonce] imageByScalingAndCroppingForSize:CGSizeMake(200, 267)];
break;
case 2:
[bimage2 setBackgroundImage:image forState:UIControlStateNormal];
[AutoCadreAppDelegate sharedInstance].simageAnnonce1 = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
[AutoCadreAppDelegate sharedInstance].simageAnnonce1 = [[[AutoCadreAppDelegate sharedInstance]simageAnnonce1] imageByScalingAndCroppingForSize:CGSizeMake(200, 267)];
break;
case 3:
[bimage3 setBackgroundImage:image forState:UIControlStateNormal];
[AutoCadreAppDelegate sharedInstance].simageAnnonce2 = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
[AutoCadreAppDelegate sharedInstance].simageAnnonce2 = [[[AutoCadreAppDelegate sharedInstance]simageAnnonce2] imageByScalingAndCroppingForSize:CGSizeMake(200, 267)];
break;
default:
break;
}
// Save image
// UIImageWriteToSavedPhotosAlbum(ci_[[ci_VilleAppDelegate sharedInstance]simageIncident, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
} |
et je fait appel a cette methode pour envoyer les photos:
Code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
-(void) uploadimage:(NSString *)fichier : (NSString *)nomfichier : (NSString *)nomfichierEnvoi
{
NSString *site= NSLocalizedString(@"site1", @"www.ci-Ville.fr");
NSString *urlString = [NSString stringWithFormat:@"%@sd-indexx.php?page=upload",site];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
NSString *boundary = @"----BOUNDARY_IS_I";
[request setHTTPMethod:@"POST"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField:@"Content-type"];
NSData *imageData = [NSData dataWithContentsOfFile:fichier];
NSLog(@"fichier : %@",fichier);
NSLog(@"nom fichier : %@",nomfichier);
// adding the body
NSMutableData *postBody = [NSMutableData data];
// first parameter an image
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"monfichier\"; filename=\"%@\"\r\n", nomfichierEnvoi] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Type: image/png\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:imageData];
// second parameter information
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Disposition: form-data; name=\"some_other_name\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"some_other_value" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r \n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postBody];
NSLog(@"postBody : %@",postBody);
NSURLResponse* response;
NSError* error;
NSData* result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
[[[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding] autorelease];
} |
Bon je comprend kedaaaal j'ai vraimentbesoin de votre aide comment puis-je envoyer l'image qui est dans le UIimageview au serveur ??? :'((
|