Hey folks,
I'm trying to make a simple image upload page work. I have created a database(MySQL), and I'm trying to upload the images to a directory on my server, and the image info (including the file path to the image), in my DB. I am very new to php(as in, I know squat), and this is entirely a learning exercise. Any help would be appreciated.
I'm guessing that my file paths are incorrect, and/or I need to set up my directory differently. But who knows? I guess that's why I'm posting here. Also, when the data goes into my DB, it says "array" under "files" and nothing else.
I'm trying to keep it as simple as possible, so that I understand what each line of code is doing....
Here is what I got:
<?php
if ($_SERVER['REQUEST_METHOD']=='POST') { // if post is being received
// connect to the database
$conn=mysql_pconnect("host","user", "password") or die ("Unable to connect to database.");
mysql_select_db("database") or die ("Unable to select database.");
$sql = "INSERT INTO db"name" (file, name, type, size, tmp_name)
VALUES ('".$_FILES['file']."', '".$_FILES['name']."', '".$_FILES['type']."', '".$_FILES['size']."', '".$_FILES['tmp_name']."' )";
$result = mysql_query($sql);
} // close if post
?>
<html>
<body>
<form action="/upload.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
</body>
</html>
btw, this is my first post ever to a php forum, so if I should be doing something differently, lemme know!