Using PHP script to connect my form to Amazon RDS [duplicate]

Wednesday, November 4, 2015

This question already has an answer here:





I am in a particularly frustrating spot as I was working on a submission form on my local host using phpmyadmin and everything was working well. Now, I've set up my EC2 instance that connects properly to my amazon RDS.



However, now that I've entered in my new database information I have a problem with this particular line $myfile = fopen("testfile.txt", "w") or die("Unable to open file!").



I changed the file permissions through my sftp and once I allow maximum access the "Unable to open file" no longer pops out BUT no information is transmitted to the database. Any idea what I am doing wrong?



<?php
// Database connect
$db['db_host'] = "rrr.rrrrrrrrrrrr.us-west-1.rds.amazonaws.com; port:3306";
$db['db_user'] = "root";
$db['db_pass'] = "rrrrrrrrrrrrr";
$db['db_name'] = "rrrrrrrrrrrrr";

foreach($db as $key => $value){
define(strtoupper($key), $value);
}

$connection = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);

?>

<?php

include "includes/db.php";

if (isset($_POST['submit'])) {

$name = mysql_real_escape_string($_POST['name']);

$email = mysql_real_escape_string($_POST['email']);

$phone = mysql_real_escape_string($_POST['phone']);

$myfile = fopen("testfile.txt", "w") or die("Unable to open file!");

$file = $_FILES['uploadedfile']['name'];

$file_loc = $_FILES['uploadedfile']['tmp_name'];

$file_size = $_FILES['uploadedfile']['size'];

$file_type = $_FILES['uploadedfile']['type'];


$error='';

if (!is_dir('uploads')) {
//Do we need to make the upload directory for the files?}

mkdir('uploads'); //make the rest of the script safe
}


$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["uploadedfile"]["name"]);
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$uploadOk = 1;

$imageFileType = strtolower($imageFileType);
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" && $imageFileType != "pdf" && $imageFileType != "doc" && $imageFileType != "docx") {
$error .= "Sorry, only JPG, JPEG, PNG, GIF, DOC, DOCX, PDF files are allowed. <br />";
$uploadOk = 0;
}
//echo $_FILES["uploadedfile"]["size"]; exit();
if ($_FILES["uploadedfile"]["size"] > 1024 * 1024 * 5) {
$error .= "Sorry, your file is too large.";
$uploadOk = 0;
}

$query = "INSERT INTO report(name, email, phone)";
$query .= "VALUES ('$name','$email','$phone')";


if (mysqli_query($connection, $query)){
$last_id_inserted = mysqli_insert_id($connection);
if ($uploadOk == 0) {
$error .= "Your file was not uploaded.";
header("Location: index.php?error=".$error);
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["uploadedfile"]["tmp_name"], $target_dir.$last_id_inserted.".".$imageFileType)) {

$query = "UPDATE report SET file='". $last_id_inserted.".".$imageFileType."', type='".$imageFileType."', size='".$file_size."' WHERE id=".$last_id_inserted;
// exit($query);
if (mysqli_query($connection, $query)){
//redirect to story page
header("Location: story.php?id=".$last_id_inserted);
}
} else {
//variale error is not empty...redirect to first page and display error.
header("Location: story.php?id=".$last_id_inserted);
}
}
}
}
?>

0 comments:

Post a Comment