Jump to content

Capturing and storing an uploaded filename


Tjobbe

Recommended Posts

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;

index.php
[code=php:0]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>

<BODY>
<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">
<option>blog</option>
<option>web</option>
</select>
<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" />
</form>
</BODY>
</HTML>
[/code]

store_image.php
[code=php:0]
<?php

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";

$title=$_POST['title'];
$content=$_POST['content'];
$category=$_POST['category'];
$image=$uploadfile;

$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\" />";
?>
[/code]
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.