par
Chile » 15 mars 2006, 01:05
Tu devrais gérer l'upload du fichier avant l'insertion en base (comme ça en cas d'erreur, tu n'as pas d'enregistrement à modifier). Cela te pemet aussi de modifier l'insert en fonction du nom du fichier et du chemin que tu définis (tu utilises alors $chemin et $name_file dans ton insert

).
ok Ryle,
merci, j'ai appliqué tes corrections et j'ai donc retouché un peu mon code ! alors j'ai refait le code en passant l'upload en tete, comme tu m'as conseillé. Puis j'ai allegé le code au niveau des verifs pour l'instant. Le resultat est que l'image est bien uploadée dans son repertoire image. Dans ma base j'ai les deux premieres données avec l'id, mais pas le non de l'image et son chemin. Je pense que j'ecris mal le code a ce niveau :
GetSQLValueString($_POST['$nomFichier'], "text"),
puis ´j'ai un message d'erreur a la fin qui me dit : Warning: Cannot modify header information - headers already sent by .......exemple/formimag3.php on line 81
la ligne 81 c'est la derniere ligne de code sql :
header(sprintf("Location: %s", $insertGoTo));
voici mon code :
<?php require_once('../Connections/pays.php'); ?>
<?php
//Script Upload
//on vérifies que le champ est bien rempli:
if(!empty($_FILES["photo"]["name"]))
{
//nom du fichier choisi:
$nomFichier = $_FILES["photo"]["name"] ;
//nom temporaire sur le serveur:
$nomTemporaire = $_FILES["photo"]["tmp_name"] ;
//type du fichier choisi:
$typeFichier = $_FILES["photo"]["type"] ;
//poids en octets du fichier choisit:
$poidsFichier = $_FILES["photo"]["size"] ;
//code de l'erreur si jamais il y en a une:
$codeErreur = $_FILES["photo"]["error"] ;
//chemin qui mène au dossier qui va contenir les fichiers upload:
$chemin = "./images/" ;
if(move_uploaded_file($nomTemporaire, $chemin.$nomFichier))
echo("<br>l'upload a réussi") ;
else
echo("<br>l'upload a échoué") ;
}//fin if
else
{
echo("Vous n'avez pas choisit de fichier!!<br>") ;
echo("<a href="./choix_fichier.html">Retour</a>") ;
}//fin else
?>
<?php
//Script MySQL
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO bpays (id, pays, site, photo, chemin) VALUES (%s, %s, %s, %s, %s)",
GetSQLValueString($_POST['id'], "int"),
GetSQLValueString($_POST['pays'], "text"),
GetSQLValueString($_POST['site'], "text"),
GetSQLValueString($_POST['$nomFichier'], "text"),
GetSQLValueString($_POST['$chemin'], "text"));
mysql_select_db($database_pays, $pays);
$Result1 = mysql_query($insertSQL, $pays) or die(mysql_error());
$insertGoTo = "formimag3";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Document sans titre</title>
</head>
<body>
<form action="<?php echo $editFormAction; ?>" method="post" enctype="multipart/form-data" name="form1">
<table align="center">
<tr valign="baseline">
<td nowrap align="right">Pays:</td>
<td><input type="text" name="pays" value="" size="32"></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">Site:</td>
<td><input type="text" name="site" value="" size="32"></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">Photo:</td>
<td><input name="photo" type="file" id="photo"></td>
</tr>
<tr valign="baseline">
<td nowrap align="right"> </td>
<td><input type="submit" value="Insérer l'enregistrement"></td>
</tr>
</table>
<input type="hidden" name="id" value="">
<input type="hidden" name="chemin" value="">
<input type="hidden" name="MM_insert" value="form1">
</form>
<p> </p>
</body>
</html>
[quote="Ryle"]
Tu devrais gérer l'upload du fichier avant l'insertion en base (comme ça en cas d'erreur, tu n'as pas d'enregistrement à modifier). Cela te pemet aussi de modifier l'insert en fonction du nom du fichier et du chemin que tu définis (tu utilises alors $chemin et $name_file dans ton insert :)).[/quote]
ok Ryle,
merci, j'ai appliqué tes corrections et j'ai donc retouché un peu mon code ! alors j'ai refait le code en passant l'upload en tete, comme tu m'as conseillé. Puis j'ai allegé le code au niveau des verifs pour l'instant. Le resultat est que l'image est bien uploadée dans son repertoire image. Dans ma base j'ai les deux premieres données avec l'id, mais pas le non de l'image et son chemin. Je pense que j'ecris mal le code a ce niveau : [php]GetSQLValueString($_POST['$nomFichier'], "text"),[/php]
puis ´j'ai un message d'erreur a la fin qui me dit : Warning: Cannot modify header information - headers already sent by .......exemple/formimag3.php on line 81
la ligne 81 c'est la derniere ligne de code sql : [php]header(sprintf("Location: %s", $insertGoTo));[/php]
voici mon code :
[php]<?php require_once('../Connections/pays.php'); ?>
<?php
//Script Upload
//on vérifies que le champ est bien rempli:
if(!empty($_FILES["photo"]["name"]))
{
//nom du fichier choisi:
$nomFichier = $_FILES["photo"]["name"] ;
//nom temporaire sur le serveur:
$nomTemporaire = $_FILES["photo"]["tmp_name"] ;
//type du fichier choisi:
$typeFichier = $_FILES["photo"]["type"] ;
//poids en octets du fichier choisit:
$poidsFichier = $_FILES["photo"]["size"] ;
//code de l'erreur si jamais il y en a une:
$codeErreur = $_FILES["photo"]["error"] ;
//chemin qui mène au dossier qui va contenir les fichiers upload:
$chemin = "./images/" ;
if(move_uploaded_file($nomTemporaire, $chemin.$nomFichier))
echo("<br>l'upload a réussi") ;
else
echo("<br>l'upload a échoué") ;
}//fin if
else
{
echo("Vous n'avez pas choisit de fichier!!<br>") ;
echo("<a href="./choix_fichier.html">Retour</a>") ;
}//fin else
?>
<?php
//Script MySQL
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO bpays (id, pays, site, photo, chemin) VALUES (%s, %s, %s, %s, %s)",
GetSQLValueString($_POST['id'], "int"),
GetSQLValueString($_POST['pays'], "text"),
GetSQLValueString($_POST['site'], "text"),
GetSQLValueString($_POST['$nomFichier'], "text"),
GetSQLValueString($_POST['$chemin'], "text"));
mysql_select_db($database_pays, $pays);
$Result1 = mysql_query($insertSQL, $pays) or die(mysql_error());
$insertGoTo = "formimag3";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Document sans titre</title>
</head>
<body>
<form action="<?php echo $editFormAction; ?>" method="post" enctype="multipart/form-data" name="form1">
<table align="center">
<tr valign="baseline">
<td nowrap align="right">Pays:</td>
<td><input type="text" name="pays" value="" size="32"></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">Site:</td>
<td><input type="text" name="site" value="" size="32"></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">Photo:</td>
<td><input name="photo" type="file" id="photo"></td>
</tr>
<tr valign="baseline">
<td nowrap align="right"> </td>
<td><input type="submit" value="Insérer l'enregistrement"></td>
</tr>
</table>
<input type="hidden" name="id" value="">
<input type="hidden" name="chemin" value="">
<input type="hidden" name="MM_insert" value="form1">
</form>
<p> </p>
</body>
</html>[/php]