Jump to content


Photo

File upload php/mysql


  • Please log in to reply
10 replies to this topic

#1 christo

christo
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 24 April 2006 - 07:30 PM

To sum it up, my goal is to save the name of an image in the DB and the image in the folder c: ... /uploads
of course along with this there is other data that we don't care about...

I did some searching + tutorials and i ended up with this code :

in HTML ->
[!--coloro:#FF6600--][span style=\"color:#FF6600\"][!--/coloro--]everything is inhtml except a tiny bit of php [!--colorc--][/span][!--/colorc--]
<form action="script.php" method="POST" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
<tr><td>Photo:</td> <td><input type="file" name="pic"></td></tr></form>

in PHP ->
$target_path = "\uploads";

$target_path = $target_path . basename( $HTTP_POST_FILES['pic']['name']);

if(move_uploaded_file($HTTP_POST_FILES['pic']['tmp_name'], $target_path)) {
echo "The file ". basename( $HTTP_POST_FILES['pic']['name']). " has been uploaded<br>";
}
else {
echo "There was an error uploading the file, please try again!";
}

// connection to the DB (works)

$req ="INSERT INTO object VALUES ('', '$region',...., '$pic', '$date_aj' )";

I click "Submit " there is no errors but there is nothing goin in the DB on in the folder /uploads does anybody have an idea ?

Christos

#2 azuka

azuka
  • Members
  • PipPipPip
  • Advanced Member
  • 55 posts
  • LocationNigeria

Posted 24 April 2006 - 08:13 PM

Would you mind showing more of your code? There might be a block of code that prevents execution from ever reaching the point of moving the uploaded files and inserting data into the database.


Given enough eyeballs all bugs are shallow. My Website

#3 sanfly

sanfly
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationNew Zealand

Posted 24 April 2006 - 11:56 PM

Is that all your code? you dont appear to be executing the query.

Try the code below, dont forget to put in your connection data. Try echoing the target path where indicated to make sure that it is all sweet. Also, make sure the directory you are uploading to has the correct permissions set etc...

$target_path = "\uploads";

if(is_dir($target_path){
    $target_path = $target_path . basename( $HTTP_POST_FILES['pic']['name']);
    
    //echo "$target_path<br><br>"; // Uncomment this line to test that the correct path is being used
    
    if(move_uploaded_file($HTTP_POST_FILES['pic']['tmp_name'], $target_path)) {
        echo "The file ". basename( $HTTP_POST_FILES['pic']['name']). " has been uploaded<br>";
        
        // Really your insert should go here, only gets done if the file is successfully uploaded
        
        // Connection to the db
        $req ="INSERT INTO object VALUES ('', '$region',...., '$pic', '$date_aj' )";
        $result = mysql_query($req) or die(mysql_error());
    }
    else {
        echo "There was an error uploading the file, please try again!";
    }
}
else{
    echo "ERROR: Directory does not exist<br><br>";
    @unlink($HTTP_POST_FILES['pic']['tmp_name']);
}

If you're not part of the solution, you're part of the precipitate

#4 christo

christo
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 25 April 2006 - 12:24 PM

No, that's not all the code, i didn't want you to read endless lines (well its not too many :-) )
so its script 1.php (contains html forms and php only to show the date) so i wont show all the code here unless you really want to (takes space..)
<form action="script.php" method="POST" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
<tr><td>Photo:</td> <td><input type="file" name="pic"></td></tr></form>

Then script2.php its only php and receives the files from script 1 and stores them in the DB
<?
$region       = $HTTP_POST_VARS['region'];
$state        = $HTTP_POST_VARS['state'];
$city         = $HTTP_POST_VARS['city'];
$district     = $HTTP_POST_VARS['district'];
$accomodation = $HTTP_POST_VARS['accomodation'];
$transaction  = $HTTP_POST_VARS['transaction'];
$nb_rooms     = $HTTP_POST_VARS['nb_rooms'];
$floor        = $HTTP_POST_VARS['floor'];
$price        = $HTTP_POST_VARS['price'];
$garage = (isset($HTTP_POST_VARS['garage']) ? $HTTP_POST_VARS['garage'] : '');
$cellar = (isset($HTTP_POST_VARS['cellar']) ? $HTTP_POST_VARS['cellar'] : '');
$garden = (isset($HTTP_POST_VARS['garden']) ? $HTTP_POST_VARS['garden'] : '');
$pool   = (isset($HTTP_POST_VARS['pool'])   ? $HTTP_POST_VARS['pool'] : '');
$descr        = $HTTP_POST_VARS['descr'];
$age_par      = $HTTP_POST_VARS['age_par'];
#$pic          = $HTTP_POST_VARS['pic'];                     pic is the var we're intrested in

$date_aj=date('l dS \of F Y, \a  h:i:s A');

$target_path = "\uploads";

$target_path = $target_path . basename( $HTTP_POST_FILES['pic']['name']);

if(move_uploaded_file($HTTP_POST_FILES['pic']['tmp_name'], $target_path)) {
echo "The file ". basename( $HTTP_POST_FILES['pic']['name']). " has been uploaded<br>";
}
else {
echo "There was an error uploading the file, please try again!";
}

$host = "a host";
$user = "a user";
$pass = "a pass";
$base = "a db";

$db_conx = mysql_connect($host,$user,$pass) or print("error de connecting mysql");
mysql_select_db($base, $db_conx) or print("error connecting to base");

$req ="INSERT INTO object VALUES ('', '$region', '$state', '$city', '$district',
'$accomodation', '$transaction', '$nb_rooms', '$floor', '$price', '$garage', '$cellar',
 '$garden', '$pool', '$descr', '$age_par', '$pic', '$date_aj' )"; #

    print ("The request is:<BR> $req<P>\n");
         if (mysql_db_query($base,$req,$db_conx)) {
             print ("Request succusfull.<BR>\n");
        }

  mysql_close ($db_conx);
And tha'ts all the php code. No errors but adds nothing to the DB !!!

#5 christo

christo
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 25 April 2006 - 06:26 PM

Ok the new is, that the problem is not the picture upload...actually it doesn't write anything in the DB so maybe i should give you the code of the script1.php just in case i'm missing something...
<html>
    <head>
        <title> * New Object * </title>
    </head>

    <body>
            <div style="text-align: center;"> </div>
            <h2 style="text-align: center;"> * Ajoutez votre annonce ! * </h2>
        <table>

            <form action="ad_obj.php" method="POST" enctype="multipart/form-data">

                    <td>Region:</td><td><select name="region" >
                        <option value="" selected="selected">Toutes les  regions</option>
                        <option >Alsace</option>
                        <option >Aquitaine</option>
                        <option >Auvergne</option>
                        <option >Basse-Normandie</option>
                        <option >Bourgogne</option>
                        <option >Bretagne</option>
                        <option >Centre</option>
                        <option >Champagne-Ardenne</option>
                        <option >Corse</option>
                        <option >DOM-TOM</option>
                        <option >Franche-Comte</option>
                        <option >Guadeloupe</option>
                        <option >Guyane</option>
                        <option >Haute-Normandie</option>
                        <option >ile-de-France</option>
                        <option >Languedoc-Roussillon</option>
                        <option >Limousin</option>
                        <option >Lorraine</option>
                        <option >Martinique</option>
                        <option >Midi-Pyrenees</option>
                        <option >Nord-Pas-de-Calais</option>
                        <option >Pays de la Loire</option>
                        <option >Picardie</option>
                        <option >Poitou-Charentes</option>
                        <option >Provence-Alpes-C.d'Azur</option>
                        <option >Reunion</option>
                        <option >Rhone-Alpes</option></select>
                    </td>
                </tr>

                    <td>Departement:</td><td align="left"><select name="state" >
                        <option value="" selected="selected">Tous les departements</option>
                        <option >01 Ain</option>
                        <option >02 Aisne</option>
                        <option >03 Allier</option>
                        <option >04 Alpes de Haute Provence</option>
                        <option >05 Hautes Alpes</option>
                        <option >06 Alpes Maritimes</option>
                        <option >07 Ardeche</option>
                        <option >08 Ardennes</option>
                        <option >09 Ariege</option>
                        <option >10 Aube</option>
                        <option >11 Aude</option>
                        <option >12 Aveyron</option>
                        <option >13 Bouches du Rhone</option>
                        <option >14 Calvados</option>
                        <option >15 Cantal</option>
                        <option >16 Charente</option>
                        <option >17 Charente Maritime</option>
                        <option >18 Cher</option>
                        <option >19 Correze</option>
                        <option >20 Corse</option>
                        <option >21 Cote d'Or</option>
                        <option >22 Cotes d'Armor</option>
                        <option >23 Creuse</option>
                        <option >24 Dordogne</option>
                        <option >25 Doubs</option>
                        <option >26 Drome</option>
                        <option >27 Eure</option>
                        <option >28 Eure et Loir</option>
                        <option >29 Finistere</option>
                        <option >30 Gard</option>
                        <option >31 Haute Garonne</option>
                        <option >32 Gers</option>
                        <option >33 Gironde</option>
                        <option >34 Herault</option>
                        <option >35 Ille et Vilaine</option>
                        <option >36 Indre</option>
                        <option >37 Indre et Loire</option>
                        <option >38 Isere</option>
                        <option >39 Jura</option>
                        <option >40 Landes</option>
                        <option >41 Loir et Cher</option>
                        <option >42 Loire</option>
                        <option >43 Haute Loire</option>
                        <option >44 Loire Atlantique</option>
                        <option >45 Loiret</option>
                        <option >46 Lot</option>
                        <option >47 Lot et Garone</option>
                        <option >48 Lozere</option>
                        <option >49 Maine et Loire</option>
                        <option >50 Manche</option>
                        <option >51 Marne</option>
                        <option >52 Haute Marne</option>
                        <option >53 Mayenne</option>
                        <option >54 Meurthe et Moselle</option>
                        <option >55 Meuse</option>
                        <option >56 Morbihan</option>
                        <option >57 Moselle</option>
                        <option >58 Nievre</option>
                        <option >59 Nord</option>
                        <option >60 Oise</option>
                        <option >61 Orne</option>
                        <option >62 Pas de Calais</option>
                        <option >63 Puy de Dome</option>
                        <option >64 Pyrenees Atlantique</option>
                        <option >65 Hautes Pyrenees</option>
                        <option >66 Pyrenees Orientales</option>
                        <option >67 Bas-Rhin</option>
                        <option >68 Haut-Rhin</option>
                        <option >69 Rhone</option>
                        <option >70 Haute Saone</option>
                        <option >71 Saone et Loire</option>
                        <option >72 Sarthe</option>
                        <option >73 Savoie</option>
                        <option >74 Haute Savoie</option>
                        <option >75 Paris</option>
                        <option >76 Seine Maritime</option>
                        <option >77 Seine et Marne</option>
                        <option >78 Yvelines</option>
                        <option >79 Deux Sevres</option>
                        <option >80 Somme</option>
                        <option >81 Tarn</option>
                        <option >82 Tarn et Garonne</option>
                        <option >83 Var</option>
                        <option >84 Vaucluse</option>
                        <option >85 Vendee</option>
                        <option >86 Vienne</option>
                        <option >87 Haute Vienne</option>
                        <option >88 Vosges</option>
                        <option >89 Yonne</option>
                        <option >90 Territoire de Belfort</option>
                        <option >91 Essonne</option>
                        <option >92 Hauts de Seine</option>
                        <option >93 Seine Saint Denis</option>
                        <option >94 Val de Marne</option>
                        <option >95 Val d'Oise</option>
                        <option >99 DOM-TOM</option></select>
                    </td>
                </tr>

                <tr><td>Ville:</td> <td><input type="text" name="city"></td></tr>
                <tr><td>Quartier:</td> <td><input type="text" name="district"></td></tr>

                <td>Type de logement:</td><td><select name="accomodation" >
                        <option value="" selected="selected">Tous les types</option>
                        <option>Maison</option>
                        <option>Appartement</option>
                        <option>Chalet</option>
                        <option>Chateau</option>
                        <option>Local</option></select>

                <tr><td>Type de Transaction :&nbsp &nbsp</td>
                <td>Vente &nbsp &nbsp &nbsp &nbsp
                    <input type="radio" name="transaction"value="Vente" checked> </input></td>
                <td>Location &nbsp &nbsp &nbsp &nbsp &nbsp
                    <input type="radio" name="transaction"value="Location"unchecked> </input></td></tr>
                <td>Colocation &nbsp &nbsp &nbsp &nbsp &nbsp
                    <input type="radio" name="transaction"value="colocq"unchecked> </input></td></tr>


                <td>Nombre de Pieces :</td>    <td><select name="nb_rooms"><option> </option>
                <option>1</option><option>2</option><option>3</option><option>4</option>
                <option>5</option><option>6</option><option>7</option><option>8</option>
                <option>9</option><option>10</option></select>

                <tr><td>Etage :</td><td><select name="floor"><option> </option>
                <option>rdc</option><option>1</option><option>2</option>
                <option>3</option><option>4</option><option>5</option></select>

                <tr><td>Prix :</td> <td><input type="text" name="price" maxlength="7"></td></tr>

                <tr><td>&nbsp &nbsp Options :</td></tr><br>
                <td>Garage &nbsp <input type="checkbox" name="garage"value="Garage"unchecked> </input></td>
                <td>Cave &nbsp <input type="checkbox" name="cellar"value="Cellar"unchecked> </input></td>
                <td>Jardin &nbsp <input type="checkbox" name="garden"value="Garden"unchecked> </input></td>
                <td>Piscine &nbsp <input type="checkbox" name="pool"value="Pool"unchecked> </input></td>

                <tr><td>Description :</td> <td><input type="textarea" name="descr" rows="3"></td></tr>

                <td>Vendeur:</td>
                    <td>Agence &nbsp &nbsp &nbsp
                          <input type="radio" name="age_par" value="Agence" checked></input></td>
                    <td>Paticulier &nbsp &nbsp
                        <input type="radio" name="age_par" value="Particulier" unchecked></input></td>

                           <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
                <tr><td>Photo:</td> <td><input type="file" name="pic"></td></tr>

                <tr><td>La date est : <?echo date('l dS \of F Y, \a   h:i:s A');?>

                <tr><td><input type="submit" value="Envoyer"></input></td></tr>
                    <td><input type="reset" value="Retablir"></input></td>
                <br>
            </form>
        </table>
                <br>
                <p><a href="index.html"> Accueil</a></p>
    </body>
</html>

At the moment i'm without any ideas...it doesn't make sence it should upload at least the rest infos.

#6 sanfly

sanfly
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationNew Zealand

Posted 25 April 2006 - 06:50 PM

in script1.php where you have

print ("The request is:<BR> $req<P>\n");
        if (mysql_db_query($base,$req,$db_conx)) {
             print ("Request succusfull.<BR>\n");
        }

Replace it with this

mysql_db_query($base,$req,$db_conx) or die("UNABLE TO WRITE TO DATABASE: . " mysql_error());
 
 print ("The request is:<BR> $req<P>\n");
 print ("Request succusfull.<BR>\n");

You should always use the general format of
mysql_query($query) or die(mysql_error())

to help identify any errors in querying the database
If you're not part of the solution, you're part of the precipitate

#7 christo

christo
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 25 April 2006 - 07:35 PM

sanfly first of all thanks for your reply, i will folow your advise but for the moment i'm trying to find whats' wrong and i don't think it's this because i have another page where i enter the infos of the user and it's working fine...so i don't think the problem is this.
But why not i'll give it a try !!!

christos

#8 christo

christo
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 27 April 2006 - 12:34 PM

That's the new code you suggested sanfly[code=auto:0]$region = $HTTP_POST_VARS['region'];
$state = $HTTP_POST_VARS['state'];
$city = $HTTP_POST_VARS['city'];
$district = $HTTP_POST_VARS['district'];
$accomodation = $HTTP_POST_VARS['accomodation'];
$transaction = $HTTP_POST_VARS['transaction'];
$nb_rooms = $HTTP_POST_VARS['nb_rooms'];
$floor = $HTTP_POST_VARS['floor'];
$price = $HTTP_POST_VARS['price'];
$garage = (isset($HTTP_POST_VARS['garage']) ? $HTTP_POST_VARS['garage'] : '');
$cellar = (isset($HTTP_POST_VARS['cellar']) ? $HTTP_POST_VARS['cellar'] : '');
$garden = (isset($HTTP_POST_VARS['garden']) ? $HTTP_POST_VARS['garden'] : '');
$pool = (isset($HTTP_POST_VARS['pool']) ? $HTTP_POST_VARS['pool'] : '');
$descr = $HTTP_POST_VARS['descr'];
$age_par = $HTTP_POST_VARS['age_par'];
$pic = $HTTP_POST_VARS['pic'];

$date_aj=date('l dS \of F Y, \a h:i:s A');


$host = "blah";
$user = "blah";
$pass = "blah";
$base = "blah";

$target_path = "\uploads";

if(is_dir($target_path){ #***************** Parse error on this line************************
$target_path = $target_path . basename( $HTTP_POST_FILES['pic']['name']);

echo "$target_path<br><br>"; // Uncomment this line to test that the correct path is being used

if(move_uploaded_file($HTTP_POST_FILES['pic']['tmp_name'], $target_path)) {
echo "The file ". basename( $HTTP_POST_FILES['pic']['name']). " has been uploaded<br>";

$db_conx = mysql_connect($host,$user,$pass) or print("erreur de connection mysql");
mysql_select_db($base, $db_conx) or print("erreur connection base");

$req ="INSERT INTO object VALUES ('', '$region', '$state', '$city', '$district',
'$accomodation', '$transaction', '$nb_rooms', '$floor', '$price', '$garage', '$cellar',
'$garden', '$pool', '$descr', '$age_par', '$pic', '$date_aj' )";
$result = mysql_query($req) or die(mysql_error());
}
else {
echo "There was an error uploading the file, please try again!";
}
}
else {
echo "ERROR: Directory does not exist<br><br>";
@unlink($HTTP_POST_FILES['pic']['tmp_name']);
}

#9 sanfly

sanfly
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationNew Zealand

Posted 27 April 2006 - 06:28 PM

If you post your problems here rather than PMing them to me, you will most likely get a quicker response. Im not the only one that would be able to help you solve this.

Anyway, the parse error was because of a missing ")". Try this instead:

$region = $HTTP_POST_VARS['region'];
$state = $HTTP_POST_VARS['state'];
$city = $HTTP_POST_VARS['city'];
$district = $HTTP_POST_VARS['district'];
$accomodation = $HTTP_POST_VARS['accomodation'];
$transaction = $HTTP_POST_VARS['transaction'];
$nb_rooms = $HTTP_POST_VARS['nb_rooms'];
$floor = $HTTP_POST_VARS['floor'];
$price = $HTTP_POST_VARS['price'];
$garage = (isset($HTTP_POST_VARS['garage']) ? $HTTP_POST_VARS['garage'] : '');
$cellar = (isset($HTTP_POST_VARS['cellar']) ? $HTTP_POST_VARS['cellar'] : '');
$garden = (isset($HTTP_POST_VARS['garden']) ? $HTTP_POST_VARS['garden'] : '');
$pool = (isset($HTTP_POST_VARS['pool']) ? $HTTP_POST_VARS['pool'] : '');
$descr = $HTTP_POST_VARS['descr'];
$age_par = $HTTP_POST_VARS['age_par'];
$pic = $HTTP_POST_VARS['pic'];

$date_aj=date('l dS \of F Y, \a h:i:s A');


$host = "blah";
$user = "blah";
$pass = "blah";
$base = "blah";

$target_path = "\uploads";

if(is_dir($target_path)){ #***************** Parse error on this line************************
$target_path = $target_path . basename( $HTTP_POST_FILES['pic']['name']);

echo "$target_path<br><br>"; // Uncomment this line to test that the correct path is being used

if(move_uploaded_file($HTTP_POST_FILES['pic']['tmp_name'], $target_path)) {
echo "The file ". basename( $HTTP_POST_FILES['pic']['name']). " has been uploaded<br>";

$db_conx = mysql_connect($host,$user,$pass) or print("erreur de connection mysql");
mysql_select_db($base, $db_conx) or print("erreur connection base");

$req ="INSERT INTO object VALUES ('', '$region', '$state', '$city', '$district',
'$accomodation', '$transaction', '$nb_rooms', '$floor', '$price', '$garage', '$cellar',
'$garden', '$pool', '$descr', '$age_par', '$pic', '$date_aj' )";
$result = mysql_query($req) or die(mysql_error());
}
else {
echo "There was an error uploading the file, please try again!";
}
}
else {
echo "ERROR: Directory does not exist<br><br>";
@unlink($HTTP_POST_FILES['pic']['tmp_name']);
}

If you're not part of the solution, you're part of the precipitate

#10 christo

christo
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 27 April 2006 - 09:37 PM

Well i replace the code and i get "ERROR: Directory does not exist" which doesn't make since the folder exists (in the same dir with the script)...

#11 sanfly

sanfly
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationNew Zealand

Posted 28 April 2006 - 12:28 AM

Try

$target_path = "uploads/";

If you're not part of the solution, you're part of the precipitate




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users