I have a form for file upload and its working no problems. The problem comes when I wish to return to the form after the file upload is complete. The form will not show. I read another post on here that said that i need to use
header("Location: upload.php?message=" . $message . "");
and so did, however the from is still not showing. What can i do to make this work?
upload.php:
<?php
session_start();
$servername = "localhost";
$username = "***********";
$password = "*********";
$dbname = "**********";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css" href="main.css">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<div class="container">
<div class="nav">
</div>
<div class="main">
<div class="content update">
<?php
if($_SESSION["logedin"] == "true"){
$sql = "SELECT * FROM content WHERE ID=" . $_POST["ID"];
//echo $sql;
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
$title=$row["title"];
$body=$row["body"];
$id=$row["ID"];
}
}
$breaks = array("<br />","<br>","<br/>","<br />","<br />","<br/>","<br>");
//$title = str_ireplace($breaks, "
", $title);
//$body = str_ireplace($breaks, "
", $body);
//echo $body;
echo $_GET['message'];
?>
<form action="doupload.php" method="post" enctype="multipart/form-data">
Name file:
<input type"text" name="title">
Select file to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="hidden" name="doload" value="doload">
<input type="submit" value="Upload" name="submit">
</form>
</div>
<div class="row2">
<h2></h2>
</div>
<div class="row2">
<div class="contentbubble">
<h2 id="demo"></h2>
<p id="demo2"></p>
</div>
</div>
</div>
</body>
</html>
doupload.php
<?php
session_start();
$message="";
$filename = 'forms/';
if (file_exists($filename)) {
//echo "The file $filename exists";
} else {
//echo "The file $filename does not exist";
}
$servername = "localhost";
$username = "**********";
$password = "**********";
$dbname = "***********";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if($_POST["doload"]=="doload"){
$target_dir = "forms/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
//echo "File Name:" . $target_file . "<br>";
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if file already exists
if (file_exists($target_file)) {
$message="Sorry, file already exists.<br>";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
$message="Sorry, your file is too large. <br>";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" && $imageFileType != "pdf" ) {
$message= "Sorry, only JPG, JPEG, PNG & GIF files are allowed.<br>";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
$message= $message . "Sorry, your file was not uploaded.<br>";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
$message = "The file " . basename( $_FILES["fileToUpload"]["name"]). " has been uploaded. <br>";
//echo $message;
$title=$_POST['title'];
$sql = "INSERT INTO forms (Name, Path)
VALUES ('" . $title . "', '" . $target_file . "')";
if ($conn->query($sql) === TRUE) {
//echo "New record created successfully";
} else {
//echo "Error: " . $sql . "<br>" . $conn->error;
}
header("Location: upload.php?message=" . $message . "demo_form_get");
} else {
$message = "Sorry, there was an error uploading your file. <br>";
header("Location: upload.php?message=" . $message . "");
}
}
}
?>