Jump to content

Need help uploading image into database.


Techmate

Recommended Posts

After the image is created I need to upload it into the database. I saves it into a subdirectory I just need it to go from the subdirectory to the database. Got stuck here and can not wrap my head around it. 

 

<?php


ini_set('display_errors', 1 );

//upload image

$to =  $_FILES['image']['name'];
$where = $_FILES['image']['tmp_name'];

//move uploaded	image
move_uploaded_file($where, "images/$to");

// call function 
mark("images/$to");

//add mark()here later

function mark($location){

$watermark = imagecreatefromgif("watermark.gif");
$watermarkheight = imagesy($watermark); //height of watermark
$watermarkwidth = imagesx($watermark);	//width of watermark

$image = imagecreatetruecolor($watermarkwidth, $watermarkheight);
$image = imagecreatefromjpeg($location);

// get image size

$size = getimagesize($location);

$xpos = $size[0] - $watermarkwidth - 10; //10px away from right
$ypos = $size[1] - $watermarkheight - 10; //10px away from bottom

// merge watermark with original image

imagecopymerge($image, $watermark, $xpos, $ypos, 0, 0, $watermarkwidth, $watermarkheight, 100);

// save watermarked image
imagejpeg($image, $location);

//clearing buffer
imagedestroy($image);
imagedestroy($watermark);


}

echo "<img src='images/$to'>";


?>
<form  action="watermark.php" method ="POST" enctype="multipart/form-data">
<input type="file" name="image">
<input type="submit" value="Watermark">
</form>

 

 

Thank you in advance.

 

 

Link to comment
Share on other sites

Can you at least post the relevant code that you have tried?

 

Sure  it is rather sorry  :shy:. Complete script not working:

 

<html>
<head>
<body>

<form action:"imguploader.php" method ="POST" enctype="multipart/form-data">
File:
<input type="file" name="image"><input type="submit" value="upload">
</form>

<?php
$dbc=$_GET['dbchange'];

$tablec = $dbc;
$tablecl = array();
$tablecl[0] = '/_/';
$replace[0] = ' ';
$remix = preg_replace($tablecl,$replace, $tablec);
// connect to database

//database connection stuff I removed it for now  
//error(); //uncomment to check for errors in the script some errors my only occur during file ch

$result = mysql_query ("SELECT position FROM images ORDER BY position DESC LIMIT 1 ");

// Generate the liks from user created galleries from db
while($row = mysql_fetch_array($result)){

$name = $row['position'];



}

$name2 = ++$name;
$lastid = mysql_insert_id();
$lastidp = ++$lastid;

//file properties
$file =$_FILES['image'] ['tmp_name'];
$checkit = $_FILES['image']['name'];
$result = mysql_query ("SELECT name FROM images");

// Generate the liks from user created galleries from db
while($row = mysql_fetch_array($result)){

$name = $row['name'];
// Check if image excists or not
if ($name == $checkit){

die ("$checkit already excists please delete it or change the files name.");
}
else {


}

}

if (!isset($file))

echo "Please select a image to upload into category: <font color=\"#ff0000\">$remix</font>";
else{
$to =  $_FILES['image']['name'];
$where = $_FILES['image']['tmp_name'];

//move uploaded	image
move_uploaded_file($where, "images/$to");

// call function 
mark("images/$to");

//add mark()here later

function mark($location){

$watermark = imagecreatefromgif("watermark.gif");
$watermarkheight = imagesy($watermark); //height of watermark
$watermarkwidth = imagesx($watermark);	//width of watermark

$image = imagecreatetruecolor($watermarkwidth, $watermarkheight);
$image = imagecreatefromjpeg($location);

// get image size

$size = getimagesize($location);

$xpos = $size[0] - $watermarkwidth - 10; //10px away from right
$ypos = $size[1] - $watermarkheight - 10; //10px away from bottom

// merge watermark with original image

imagecopymerge($image, $watermark, $xpos, $ypos, 0, 0, $watermarkwidth, $watermarkheight, 100);

// save watermarked image
imagejpeg($image, $location);

//clearing buffer
imagedestroy($image);
imagedestroy($watermark);




//echo "<img src='images/$to'>";

//$galleryids= $_POST['galleryid'];

if ($image_size == FALSE)
echo "That's not an image";
else {
if (!$insert = mysql_query("INSERT INTO images VALUES ('','$to','$where','$name2')"))
echo "Problem uploading image. Contact web administrator.";
else{

$lastid= mysql_insert_id();
echo "$image_name uploaded into $remix Gallery. <p / > <p/> <img src=get.php?id=$lastid&dbchange=$dbc&cat=$image_name>";

}

}
}
}

//var_dump($_FILES);

?>

</body>

</head>

</html>

Link to comment
Share on other sites

Hi There,

 

You havn't specified WHICH fields you want to insert what into

 

if (!$insert = mysql_query("INSERT INTO images VALUES ('','$to','$where','$name2')"))

 

Here's an example of INSERT

 

INSERT INTO tbl_name (field1,field2,field2) VALUES ('a','b','c')

 

Also this line checks if the variable has been set, but for security reasons please also check if its empty or not

if (!isset($file))

to

if ((!isset($file)) || ($file == ""))

 

 

Link to comment
Share on other sites

Hi There,

 

You havn't specified WHICH fields you want to insert what into

 

if (!$insert = mysql_query("INSERT INTO images VALUES ('','$to','$where','$name2')"))

 

Here's an example of INSERT

 

INSERT INTO tbl_name (field1,field2,field2) VALUES ('a','b','c')

 

Also this line checks if the variable has been set, but for security reasons please also check if its empty or not

if (!isset($file))

to

if ((!isset($file)) || ($file == ""))

 

I checked it over and I think it is fine but based on my script can you revise it how it should be?

Link to comment
Share on other sites

You do not need to specify the names of the fields if you have a value set for all of them. The first query looked fine.

 

op: What error are you getting?

 

Well I would like to make it  work with this basic piece of code first because I know it puts my watermark on my image but it does not upload it into the database when I add the my_query string:

 

<?php
$dbc=$_GET['dbchange'];

include ("../confffee.php");
confff("$dbc");
ini_set('display_errors', 1 );
//error(); //uncomment to check for errors in the script some errors my only occur during file ch

$result = mysql_query ("SELECT position FROM images ORDER BY position DESC LIMIT 1 ");

// Generate the liks from user created galleries from db
while($row = mysql_fetch_array($result)){

$name = $row['position'];



}

$name2 = ++$name;
//upload image

$to =  $_FILES['image']['name'];
$where = $_FILES['image']['tmp_name'];

//move uploaded	image
move_uploaded_file($where, "images/$to");

// call function 
mark("images/$to");

//add mark()here later

function mark($location){

$watermark = imagecreatefromgif("watermark.gif");
$watermarkheight = imagesy($watermark); //height of watermark
$watermarkwidth = imagesx($watermark);	//width of watermark

$image = imagecreatetruecolor($watermarkwidth, $watermarkheight);
$image = imagecreatefromjpeg($location);

// get image size

$size = getimagesize($location);

$xpos = $size[0] - $watermarkwidth - 10; //10px away from right
$ypos = $size[1] - $watermarkheight - 10; //10px away from bottom

// merge watermark with original image

imagecopymerge($image, $watermark, $xpos, $ypos, 0, 0, $watermarkwidth, $watermarkheight, 100);

// save watermarked image
imagejpeg($image, $location);

//clearing buffer
imagedestroy($image);
imagedestroy($watermark);

mysql_query("INSERT INTO images VALUES ('','$to','$where','$name2')"))

}

echo "<img src='images/$to'>";


?>
<form  action="watermark.php?dbchange=Picuters" method ="POST" enctype="multipart/form-data">
<input type="file" name="image">
<input type="submit" value="Watermark">
</form>

Link to comment
Share on other sites

Heres the updated code for debugging, let me know the error

 

<html>
<head>
<body>

<form action:"imguploader.php" method ="POST" enctype="multipart/form-data">
File:
<input type="file" name="image"><input type="submit" value="upload">
</form>

<?php
$dbc=$_GET['dbchange'];
ini_set("display_errors","1");
error_reporting(E_ALL);
$tablec = $dbc;
$tablecl = array();
$tablecl[0] = '/_/';
$replace[0] = ' ';
$remix = preg_replace($tablecl,$replace, $tablec);
// connect to database

//database connection stuff I removed it for now  
//error(); //uncomment to check for errors in the script some errors my only occur during file ch

$result = mysql_query ("SELECT position FROM images ORDER BY position DESC LIMIT 1 ") or die(mysql_error());

// Generate the liks from user created galleries from db
while($row = mysql_fetch_array($result)){

$name = $row['position'];



}

$name2 = ++$name;
$lastid = mysql_insert_id();
$lastidp = ++$lastid;

//file properties
$file =$_FILES['image'] ['tmp_name'];
$checkit = $_FILES['image']['name'];
$result = mysql_query ("SELECT name FROM images");

// Generate the liks from user created galleries from db
while($row = mysql_fetch_array($result)){

$name = $row['name'];
// Check if image excists or not
if ($name == $checkit){

die ("$checkit already excists please delete it or change the files name.");
}
else {


}

}

if (!isset($file))

echo "Please select a image to upload into category: <font color=\"#ff0000\">$remix</font>";
else{
$to =  $_FILES['image']['name'];
$where = $_FILES['image']['tmp_name'];

//move uploaded	image
move_uploaded_file($where, "images/$to");

// call function 
mark("images/$to");

//add mark()here later

function mark($location){

$watermark = imagecreatefromgif("watermark.gif");
$watermarkheight = imagesy($watermark); //height of watermark
$watermarkwidth = imagesx($watermark);	//width of watermark

$image = imagecreatetruecolor($watermarkwidth, $watermarkheight);
$image = imagecreatefromjpeg($location);

// get image size

$size = getimagesize($location);

$xpos = $size[0] - $watermarkwidth - 10; //10px away from right
$ypos = $size[1] - $watermarkheight - 10; //10px away from bottom

// merge watermark with original image

imagecopymerge($image, $watermark, $xpos, $ypos, 0, 0, $watermarkwidth, $watermarkheight, 100);

// save watermarked image
imagejpeg($image, $location);

//clearing buffer
imagedestroy($image);
imagedestroy($watermark);
$insert = mysql_query("INSERT INTO ('image','position','name') VALUES ('$to','$where','$name2')") or die(mysql_error());




//echo "<img src='images/$to'>";

//$galleryids= $_POST['galleryid'];

if ($image_size == FALSE)
echo "That's not an image";
else {
if (!$insert)
echo "Problem uploading image. Contact web administrator.";
else{

$lastid= mysql_insert_id();
echo "$image_name uploaded into $remix Gallery. <p / > <p/> <img src=get.php?id=$lastid&dbchange=$dbc&cat=$image_name>";

}

}
}
}

//var_dump($_FILES);

?>

</body>

</head>

</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.