Page 1 sur 1

upload d`image

Posté : 13 août 2008, 19:43
par alex288
je suis en train d`uploader une image dans le serveur et stocker son chemin dans mysql, ca s`apload tres bien dans le serveur mais le probleme est que tous les champs du formulaire s`ajoutent dans la base donnee sauf le champ chemin ou je dois stocker l`addresse de l`image.

<code>
<?php

$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);


$uploadHandler = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'add_image.php';


$max_file_size = 30000; // size in bytes


?>

<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">

<link rel="stylesheet" type="text/css" href="stylesheet.css">

<title>Upload form</title>

</head>

<body>

<form id="Upload" action="<?php echo $uploadHandler ?>" enctype="multipart/form-data" method="post">

<table border="0" align="center" cellspacing="2" cellpadding="2">

<tr align="center">
<td> <b> reference de produit </b> </td>
<td><input type="text" name="ref_produit"></td>
</tr>

<tr align="center">
<td> <b> titre du produit </b> </td>
<td><input type="text" name="titre"></td>
</tr>

<tr align="center">
<td> <b> description </b> </td>
<td><input type="text" name="description"></td>
</tr>

<tr align="center">
<td> <b> prix </b> </td>
<td><input type="text" name="prix"></td>
</tr>

<tr align="center">
<td> <b> date d`ajout </b> </td>
<td><input type="text" name="date_ajout"></td>
</tr>


<tr align="center">
<td> <b> reference de type </b> </td>
<td><input type="text" name="ref_type"></td>
</tr>

<tr align="center">
<td> <b> reference de l`artiste </b> </td>
<td><input type="text" name="ref_artiste"></td>
</tr>


<tr align="center">
<td> <b> image </b> </td>
<td><input type="file" name="chemin" size=50></td>
</tr>


<tr align="center">
<td colspan="2"><input type="submit" name="submit" value="ajouter"></td>
</tr>
</table>
</form>
</body>
</html>
</code>

et le fichier .php:

<html >
<head>
<title>Document sans titre</title>
</head>
<body>

<?php
$db = mysql_connect("localhost","root",""); // connexion a la base de donnee
mysql_select_db("boutique",$db); // selection de la base de donnee

$ref_produit = $_POST["ref_produit"] ;

$titre = $_POST["titre"] ;

$description = $_POST["description"] ;

$prix = $_POST["prix"] ;

$date_ajout = $_POST["date_ajout"] ;

$chemin = $_POST["chemin"] ;

$ref_type = $_POST["ref_type"] ;

$ref_artiste = $_POST["ref_artiste"] ;


//création de la requête SQL:
$sql = "INSERT INTO produit ( ref_produit, titre, description, prix, date_ajout, chemin, ref_type, ref_artiste)
VALUES ('$ref_produit', '$titre', '$description', '$prix', '$date_ajout','$chemin', '$ref_type', '$ref_artiste') " ;


//exécution de la requête SQL:
//$requete = mysql_query($sql, $db) or die( mysql_error() ) ;



$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);


$uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self . 'images/';


$uploadForm = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'index18.php';


$uploadSuccess = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'upload.success.php';


$fieldname = 'chemin';



$errors = array(1 => 'php.ini max file size exceeded',
2 => 'html form max file size exceeded',
3 => 'file upload was only partial',
4 => 'no file was attached');


isset($_POST['submit'])
or error('the upload form is neaded', $uploadForm);


($_FILES[$fieldname]['error'] == 0)
or error($errors[$_FILES[$fieldname]['error']], $uploadForm);


@is_uploaded_file($_FILES[$fieldname]['tmp_name'])
or error('not an HTTP upload', $uploadForm);


@getimagesize($_FILES[$fieldname]['tmp_name'])
or error('only image uploads are allowed', $uploadForm);


$now = time();
while(file_exists($uploadFilename = $uploadsDirectory.$now.'-'.$_FILES[$fieldname]['name']))
{
$now++;
}


@move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename)
or error('receiving directory insuffiecient permission', $uploadForm);

//exécution de la requête SQL:
$requete = mysql_query($sql, $db) or die( mysql_error() ) ;
header('Location: ' . $uploadSuccess);

// make an error handler which will be used if the upload fails
function error($error, $location, $seconds = 5)
{
header("Refresh: $seconds; URL=\"$location\"");
echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"'."\n".
'"http://www.w3.org/TR/html4/strict.dtd">'."\n\n".
'<html lang="en">'."\n".
' <head>'."\n".
' <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">'."\n\n".
' <link rel="stylesheet" type="text/css" href="stylesheet.css">'."\n\n".
' <title>Upload error</title>'."\n\n".
' </head>'."\n\n".
' <body>'."\n\n".
' <div id="Upload">'."\n\n".
' <h1>Upload failure</h1>'."\n\n".
' <p>An error has occured: '."\n\n".
' <span class="red">' . $error . '...</span>'."\n\n".
' The upload form is reloading</p>'."\n\n".
' </div>'."\n\n".
'</html>';
exit;
} // end error handler
?>



</body>
</html>
</code>

sachant que jai un autre fichier mais just pour afficher un message de feliecitation:
<code>
<?php

// filename: upload.success.php

?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">

<link rel="stylesheet" type="text/css" href="stylesheet.css">

<title>Successful upload</title>

</head>

<body>

<div id="Upload">
<h1>File upload</h1>
<p>Felecitation, l`image est bien uploadee</p>
</div>

</body>

</html>
</code>

merci beaucoup et je serais tres reconnaissant pour votre aide

Posté : 13 août 2008, 20:16
par Ryle
Modération :
alex288, afin d'améliorer la lisibilité de ton message,
pense à utiliser les balises [code] ou [php] (selon le langage utilisé).

Elles sont disponibles au-dessus de la zone de saisie de ton message
lorsque tu postes un nouveau message.

Des indications sont disponibles sur la manière de mettre en forme vos messages dans la FAQ