Solved

insert image into database,php,mysql

Posted on 2009-04-06
12
969 Views
Last Modified: 2013-12-12
I need to insert an image into a database and be able to select them all later.. the insert works for the most part. but i get an issue where it will duplicat the last image when i insert the next one some of the time.

I need it to clear itself or flush itself so that a new image is uploaded each time.

Below is the code snippet that i'm working with and it works. it inserts the image into the database. however like i stated above. it will duplicate the previous image sometimes. i need help to make sure the new image is always uploaded.
if(isset($_POST['upload']))

{

        // Need to add - check for large upload. Otherwise the code

        // will just duplicate old file ;-)

        // ALSO - note that latest.img must be public write and in a

        // live appliaction should be in another (safe!) directory.

        $title = $_REQUEST['title'];

        move_uploaded_file($_FILES['imagefile']['tmp_name'],"latest.img");

        $instr = fopen("latest.img","rb");

        $image = addslashes(fread($instr,filesize("latest.img")));

        if (strlen($instr) < 149000) {

        	

	$table="tournament_images";

	$cols="image,image_name";

	$vals="'$image','$title'";

	putData($table,$cols,$vals);
 

        } else {

                $errmsg = "Too large!";

        }

        $_FILES="";

        ShowData();

}

Open in new window

0
Comment
Question by:TrinitySJ
  • 7
  • 5
12 Comments
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24079114
What is in the putData() function?
0
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 500 total points
ID: 24079130
Line 11 should probably be:

 if (strlen($image) < 149000) {
0
 
LVL 1

Author Comment

by:TrinitySJ
ID: 24082643
why $image instead of $instr

what is the difference.?

thanks in advance
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24082869
$image contains the image data, $instr contains the file handle. It is the size of the image data you want to check.
0
 
LVL 1

Author Comment

by:TrinitySJ
ID: 24083114
I tried that but it did not help.. i still get the same image uploaded he next time as the last image uploaded...
i need to make sure that it uploads the current image each time.

thanks...
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24083225
What is in the putData() function? That is where the storing is performed, without seeing that code it is impossible to say what is wrong.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 1

Author Comment

by:TrinitySJ
ID: 24083345
sorry.. the putData is below...
/******************************************/

/* Start Function to do SQL INSERT *********/

function putData($table,$cols,$values)

{

	global $databasename;

	global $dbconn;
 

	$query="INSERT INTO $table($cols)VALUES($values)";
 

	// verify DB connection still exists...

	if(!mysql_select_db($databasename,$dbconn))

	{

		showerror();

		echo "OOPS-no DB!";

	}

	// create the LOCK TABLES statement

	$lock="LOCK TABLES $table WRITE";
 

	// lock the table....

	if(!(mysql_query($lock,$dbconn)))

	{

		echo $lock . "<br>";

		showerror();

	}
 

	if(!($results=mysql_query($query,$dbconn)))

	{

		echo $query . "<br>";

		showerror();

	}
 

	// create UNLOCK TABLE statement

	$unlock="UNLOCK TABLES";
 

	// unlock the table

	if(!(mysql_query($unlock,$dbconn)))

	{

		echo $unlock . "<br>";

		showerror();

	}

	return true;
 

}

Open in new window

0
 
LVL 39

Assisted Solution

by:Roger Baklund
Roger Baklund earned 500 total points
ID: 24083415
This function seems to insert a new record every time. Can you verify that? Is the problem that the wrong file is inserted? Is it possible that this only happen when two users insert a file "simultaneously"?

Try changing this:

move_uploaded_file($_FILES['imagefile']['tmp_name'],"latest.img");
$instr = fopen("latest.img","rb");
$image = addslashes(fread($instr,filesize("latest.img")));

...into this:

$filename = 'latest'.date('Ymd_His').'.img';
move_uploaded_file($_FILES['imagefile']['tmp_name'],$filename);
$instr = fopen($filename,"rb");
$image = addslashes(fread($instr,filesize($filename)));

This way a new file is created for each upload. To avoid these files from collecting, you can delete them after you have loaded them into the db:

fclose($instr);  # close the file
unlink($filename); # remove the file
0
 
LVL 1

Author Comment

by:TrinitySJ
ID: 24083519
I will try that and see if that works...
0
 
LVL 1

Author Comment

by:TrinitySJ
ID: 24083735
This function seems to insert a new record every time. Can you verify that?
--Verified.. this is the case.. each time putData() is called it inserts a record into the database.

Is the problem that the wrong file is inserted?
--It is correct that the wrong file is inserted. it is a duplication of the last file inserted. i need it to be that current selected file that gets inserted.

Is it possible that this only happen when two users insert a file "simultaneously"?
--this is not what is happening. at the moment i am the only one inserting images into the DB
0
 
LVL 1

Author Comment

by:TrinitySJ
ID: 24089472
the above did not work.. will work on it more.
0
 
LVL 1

Author Comment

by:TrinitySJ
ID: 24232400
Found the soulution that i needed... fixed it.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Add visuals to your Prezi to keep your audience engaged. Learn how to embed images, edit them, and more in this video micro tutorial. Select "Insert" from the top menu in your Prezi editor: Select "Image": A toolbar will pop in from the right …
In addition to being a great web-based presentation tool, Prezi also makes it easy to save your presentation as a PDF to share with others as well. Learn how in this tutorial. Select the share icon from the top menu in your Prezi editor: Select "D…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now