Hello! I'm trying to create a page for users to upload text and images to a MYSQL database. However, I I'd like to store only the image information in the database, but move the image itself in a folder in an image directory on the website. I hope that makes sense so far.
The code I have so far is intended to store the text information in the database and upload the image directly to a folder on the website called /images. I'm having trouble getting it to work. First, here is the HTML form for the user to input the data:
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>post</title>
</head>
<body>
POST<br/><br/>
<form action="posted.php" method="post" enctype="multipart/form-data">
title:<br /> <input type="text" name="title" size="93"/> (optional)<br />
date: <br/> <input type="text" name="date" /><br/>
post:<br /> <textarea rows="100" cols="80" name="post"></textarea><br />
pictures: <br/> <input type="file" name="picture" accept="image/jpeg"/>
<input type="submit" name="Submit" value="do it"/><br />
</form>
</body>
</html>
and here is the PHP file (posted.php) :
<?php
$target = "/images";
$target = $target . basename ($_FILES['picture']);
$title = $_POST['title'];
$post = $_POST['post'];
$date = $_POST['date'];
$picture = $_FILES['picture'];
mysql_connect("--", "--", "--") or die(mysql_error()) ;
mysql_select_db("dbname") or die(mysql_error()) ;
mysql_query("INSERT INTO Posts (Number,Title,Content,Date,Picture)
VALUES (null,'$title', '$post', '$date', '$picture')") ;
if(move_uploaded_file $_FILES['picture'], $target)
{
echo "The file ". basename $_FILES['picture']. " has been uploaded, and your information has been added to the directory";
}
else {
echo "Sorry, there was a problem uploading your file.";
}
?>
The result is the message "I'm sorry, there was a problem uploading your file" I'm fairly sure the problem is with the $target or $_FILES variables. I know the connection to MYSQL works because I've gotten a different form to work that uploads only text to the database, but the images are posing a problem. I'm not sure I understand the basic concept for moving an image file to a remote directory. Could someone help me with the syntax?
Thanks so much!