Jump to content


Capturing and storing an uploaded filename

  • Please log in to reply
2 replies to this topic

#1 Tjobbe

  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 29 September 2006 - 12:52 PM

I have a form that submits text data to a database (mysql), whilst uploading an image to a folder.

I'd like to store the file path or name in the database entry alsongside the rest of the data.

How do i capture the filename and submit it to be stored?

here is what I have;

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">

<form enctype="multipart/form-data" action="store_image.php" method="post">
	Title:<br />
	<input type="text" name="title" /><br />
	Content:<br />
	<textarea rows="12" cols="50" name="content"></textarea><br />
	<select name="category" id="category">
	<br />
    <input type="hidden" name="MAX_FILE_SIZE" value="300000" />
    <!-- Name of input element determines name in $_FILES array -->
    Send this file: <input name="userfile" type="file" />
	<br />
	<input type="submit" />


include "dbconnect.php";

$uploaddir = 'images/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
   echo "File is valid, and was successfully uploaded.\n";


$sqlquery = "INSERT INTO $category VALUES(id,'$title','$content',NOW()),'$image'";

$results = mysql_query($sqlquery) or die(mysql_error());

} else {
   echo "Possible file upload attack!\n";
echo "<br /><img src=\"$uploadfile\" />";

#2 steveclondon

  • Members
  • PipPipPip
  • Advanced Member
  • 161 posts

Posted 29 September 2006 - 12:56 PM

use the $_FILE array. Have a look at the link below and download the manual then search for $_FILE. There are alot of examples there. If you still need help I have post an upload script here before.

Remember to check the file name first and get ride of spaces etc and ensure that two names are not the same.

#3 Tjobbe

  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 29 September 2006 - 01:01 PM

Thanks for the reply mate! looking now.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users