Hi there,
I am trying to allow visitors to upload images ( and later want to allow them to add audio files too so it would be nice to get comments on that as well ) .
All the information gets uploaded correctly except the files. What could be the problem?
I came up with the following code:
<form id="add-new-form" enctype="multipart/form-data">
<input type="text" name="author" placeholder="author:" />
<input type="text" name="title" placeholder="title:" />
<textarea rows="10" cols="34" name="content" placeholder="content:"></textarea>
<input type="file" name="image" />
<input type="submit" id="add-new-button" value="" />
</form>
try {
$conn = new PDO('mysql:host=localhost;dbname=brownie_manualforajump;', 'username', 'pass');
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
if(isset($_POST['author'])) {
$author = htmlspecialchars($_POST['author']);
$title = htmlspecialchars($_POST['title']);
$content = htmlspecialchars($_POST['content']);
// properties of the uploaded file
$name = $_FILES["image"]["name"];
$type = $_FILES["image"]["type"];
$size = $_FILES["image"]["size"];
$temp = $_FILES["image"]["tmp_name"];
$error = $_FILES["image"]["error"];
// if($error > 0) {
// die("Error uploading file! Code $error.");
// } else {
// if($type == "video/avi") { // conditions for the file
// die("That format is not allowed");
// } elseif ($size > 1048576) {
// die("File size cannot exceed 1mb");
// } else {
move_uploaded_file($temp,"uploaded/".$name);
// }
// }
//
$result = $author;
echo json_encode($result);
try {
$stmt = $conn->prepare("INSERT INTO projects VALUES ('', :author, :title, :content, :path)");
$stmt->bindParam('author', $author, PDO::PARAM_STR);
$stmt->bindParam('title', $title, PDO::PARAM_STR);
$stmt->bindParam('content', $content, PDO::PARAM_STR);
$stmt->bindParam('path', $name, PDO::PARAM_STR);
$stmt->execute();
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
}