Hello there!

I am looking around for a way to upload image on server (via browse button) and in the database insert only the path of that image.

What i have just now is the form, the form processor and the database but i don;t know how to do that.

add.php

<?php


session_start();

if(!isset($_SESSION['loggedin'])) {
   header('Location: '.$domain.'index.php?error=1');
   exit();
}

define("NGA_ADMIN", true);
?>
<form id="shtoprodukte" action="modulet/produktet/p.shtoprodukte.php" method="post" name="shtoprodukte">
<table width="740" border="0" cellspacing="2" cellpadding="0">

<tr><td width = "150"><div align="right"><label for="katid">Kategoria</label></div></td>
<td><select id="katid" name="katid" size="1">
<?php
include 'D:/Program Files/VertrigoServ/www/joni/admini/includet/variabla.php';
include (BPATH_ADM . 'includet/dbconfig.php');

$query="SELECT * FROM p_kategori ";
$result=mysql_query($query);
$num = mysql_num_rows ($result);
mysql_close();
?>

<?php
if ($num > 0 ) {
$i=0;
while ($i < $num) {
$katemri = mysql_result($result,$i,"katemri");
$katpermalink = mysql_result($result,$i,"katpermalink");
$kataktiv = mysql_result($result,$i,"kataktiv");
$id = mysql_result($result,$i,"id");
?>

<option value="<?php echo $id; ?>"><?php echo $katemri; ?></option>

<?php
++$i; } }
?>
</select></td></tr>

<tr><td width = "50"><div align="right"><label for="prodemri">Emri</label></div></td>
<td><input id="prodemri" name="prodemri" type="text" size="25" value="" maxlength="255"></td></tr>

<tr><td width = "50"><div align="right"><label for="prodinfo">Info</label></div></td>
<td><textarea id="prodinfo" name="prodinfo"></textarea>
<script type="text/javascript">
	CKEDITOR.replace( 'prodinfo',
    {
        filebrowserBrowseUrl : '/joni/admini/includet/ckfinder/ckfinder.html',
        filebrowserImageBrowseUrl : '/joni/admini/includet/ckfinder/ckfinder.html?Type=Images',
        filebrowserFlashBrowseUrl : '/joni/admini/includet/ckfinder/ckfinder.html?Type=Flash',
        filebrowserUploadUrl : '/joni/admini/includet/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files',
        filebrowserImageUploadUrl : '/joni/admini/includet/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images',
        filebrowserFlashUploadUrl : '/joni/admini/includet/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash'
    });
</script>
</td></tr>

<tr><td width = "50"><div align="right"><label for="prodimg">Fotografia</label></div></td>
<td><input id="prodimg" name="prodimg" type="text" size="25" value="" maxlength="255"></td></tr>

<tr><td width = "50"><div align="right"><label for="prodaktiv">Aktive</label></div></td>
<td><input id="prodaktiv" name="prodaktiv" type="checkbox" value="Y"></td></tr>

<tr><td width="45"></td><td>
<input type="submit" name="shtoje" value="Shtoje"> </td>
</tr></table></form>

p.add.php

<?php

ob_start(); 
session_start();

if(!isset($_SESSION['loggedin'])) {
   header('Location: '.$domain.'index.php?error=1');
   exit();
}

define("NGA_ADMIN", true);
?>

<?php
include 'D:/Program Files/VertrigoServ/www/joni/admini/includet/variabla.php';
include (BPATH_ADM . 'includet/dbconfig.php');
$prodemri = $_POST['prodemri'];
$prodinfo = $_POST['prodinfo'];
$prodimg = $_POST['prodimg'];
$katid = $_POST['katid'];
$prodaktiv = $_POST['prodaktiv'];

$query = "INSERT INTO p_produkte (id, katid, prodemri, prodinfo, prodimg, prodaktiv)
VALUES ('', '$katid ', '$prodemri', '$prodinfo', '$prodimg', '$prodaktiv')";
$results = mysql_query($query);

if ($results)
{
header( 'refresh: 0; url= '.$admurl.'admin.php?sukses=1' );
}
mysql_close();
ob_flush();
?>

p_produkte table (products)

        Field  	Type  	
	id 	         int(6) 
	katid 	int(255)
	prodemri 	varchar(255) 
	prodimg 	varchar(255)
	prodinfo 	text 	
	prodaktiv 	enum('N', 'Y')

p_kategori table (category)

 Field  	Type  	
	id 	         int(6) 
	katemri 	varchar(255)
	katpermalink 	varchar(255) 
	kataktiv 	enum('N', 'Y')

...where prodimg is the label where should be the browse button and where should be inserted the path of the image on the database table named the same (prodimg).

Hope that someone can help me. Thanks in advance!

    On your HTML form tag, in order to upload a file or photo, you need to specify:

    enctype="multipart/form-data"

    In your PHP script, if you need to limit a max size and restrict file types, you're going to have to create some validation checks.

    I don't know how to save the directory path into your database, and I don't know if this is the best way to do it, but to upload the photo to your directory folder the way I have mine is:

    
    $maxfilesize = 51200; // 51200 bytes equals 50kb
                if($_FILES['fileField']['size'] > $maxfilesize ) { 
    
                        $error_msg = "ERROR: Your image was too large, please resize your image.";
                        unlink($_FILES['fileField']['tmp_name']); 
    
            } else if (!preg_match("/\.(gif|jpg|png)$/i", $_FILES['fileField']['name'] ) ) {
    
                        $error_msg = "ERROR: Your image was not one of the accepted formats, please save as .jpg, .gif or .png.";
                        unlink($_FILES['fileField']['tmp_name']); 
    
            } else { 
    
                      $place_file = move_uploaded_file( $_FILES['fileField']['tmp_name'], "YOUR DIRECTORY PATH HERE);
                                   }
    
    
    
      5 days later

      Thank you for your reply everythingis, but is important to me a way on how to save the directory path into my database.

      Anyone can help me? Thank you in advance!

        Hi Kleidi24,

        What you need to do is,

        1. Get the last insert ID to a variable after you insert the product details. mysql_insert_id()

        2. get the uploaded file extension.

        3. create a unique file name. you can use the combination of mysql_insert_id and the extension to create the new file name so this will be unique all the time and it will allow you to manually figure out the product.

        4. create the full upload path with the image name

        5. upload the file

        6. update the table.

        Please fine below the code,

        $lastInsertedProductId = mysql_insert_id(); //step 1
        $imgExtArr = explode('.', $_FILES['fileField']['name']);
        $imgExt = $imgExtArr[1];	////step 2
        $imageName = $lastInsertedProductId.'.'.$imgExt; //step 3
        
        $uploadPath = '/img/products/'.$imageName; //step 4
        
        move_uploaded_file( $_FILES['fileField']['tmp_name'], $uploadPath); //step 5
        
        $sql = "UPDATE p_produkte SET prodimg = '".$uploadPath."' WHERE id = ".$lastInsertedProductId;
        mysql_query($sql); //step 6
        

        Hope this is clear.

        regards,
        niroshan

          Write a Reply...