upload document and add file details to database not working

Hi all,

I have an upload script, it should move the selected file to the specified server location and then add the attributes i.e file size, name, upload date onto my MySQL database. For some reason I cannot get it to work, I thought I had it working correctly but cannot get back to this stage.

My form code is:-
[php]

Title of the File

File Category

Upload File (Max <?php echo ini_get('upload_max_filesize').'B'; ?>)
[/php]

and my upload script is:-
[php]<?php

if($_POST)
{
if(!isset($_POST[‘file_title’]) || strlen($_POST[‘file_title’])<1)
{
//required variables are empty
die(“File Title is empty! please go back and enter”);
}

if($_FILES['file_name']['error'])
{
	//File upload error encountered
	die(upload_errors($_FILES['file_name']['error']));
}

//This is the directory where images will be saved
$target = “uploads/”;
$target = $target . basename( $_FILES[‘file_name’][‘name’]);

//This gets all the other information from the form
$file_name = basename( $_FILES[‘file_name’][‘name’]);
$file_size = $_FILES[‘file_name’][“size”];
$file_title = $_POST[‘file_title’];
$client_id = $_POST[‘client_id’];
$file_cat = $_POST[‘file_cat’];
$file_type = $_FILES[‘file_name’][‘type’];
$uploaded_date = date(“Y-m-d H:i:s”);

//Writes the Filename to the server
if(move_uploaded_file($_FILES[‘file_name’][‘tmp_name’], $target)) {

//Tells you if its all ok
echo "The file “. basename( $_FILES[‘file_name’][‘name’]). " has been uploaded, and your information has been added to the directory”;

// Connects to database
include(’…/includes/mysqli_connect.php’);

if (mysqli_connect_errno($db)) {
trigger_error('Database connection failed: ’ . mysqli_connect_error(), E_USER_ERROR);
}

// add file details to database
$query = “INSERT INTO tbl_documents (file_name,file_title,file_size,client_id,file_typ,uploaded_date,file_cat) VALUES (’$file_name’,’$file_title’,’$file_size’,’$client_id’,’$file_typ’,’$uploaded_date’,’$file_cat’)”;
$result = mysqli_query($db, $query) or trigger_error("Query Failed! SQL: $query - Error: ". mysqli_error($db), E_USER_ERROR);

} else {

// gives an error if its not uploaded
echo “Sorry, there was a problem uploading your file.”;
}
}
?>[/php]

The only error I receive is the one at the bottom of the script “Sorry, there was a problem…”

I’m not sure if this will be the issue, but I have long moved away from relative paths when it comes to working with files, set target to something fixed, see if it still breaks. Other common ones will be max_post_size etc. in the php.ini.

I am assuming the upload folder is where the script is. Add a period and slash before the folder name.

$target = “./uploads/”;

Hi Guys,

Kevin - I tried your option but I didnt get any joy. I do have another script which interacts with the files in the same directory and it uses the path “uploads/” successfully.

I’ve since spotted a few errors with my code… in that the file id to upload wasnt matched up with the script. He’s my current code;-

Upload.php
[php]

Title of the File
File Category
Project
File
Upload File (Max <?php echo ini_get('upload_max_filesize').'B'; ?>) Back
[/php]

uploaded_sc.php
[php]<?php

if($_POST)
{
if(!isset($_POST[‘file_title’]) || strlen($_POST[‘file_title’])<1)
{
//required variables are empty
die(“File Title is empty! please go back and enter”);
}

if($_FILES['mFile']['error'])
{
	//File upload error encountered
	die(upload_errors($_FILES['mFile']['error']));
}

//This is the directory where images will be saved
$UploadDirectory = “uploads/”;
$target = $UploadDirectory . basename( $_FILES[‘file_name’][‘name’]);

//This gets all the other information from the form
$file_name = basename( $_FILES[‘mFile’][‘name’]);
$file_size = $_FILES[‘mFile’][“size”];
$file_title = $_POST[‘file_title’];
$client_id = $_POST[‘client_id’];
$file_cat = $_POST[‘file_cat’];
$client_project = $_POST[‘client_project’];
$file_type = $_FILES[‘mFile’][‘type’];
$uploaded_date = date(“Y-m-d H:i:s”);

//Writes the Filename to the server
if(move_uploaded_file($_FILES[‘mFile’][‘tmp_name’], $target)) {

//Tells you if its all ok
echo "The file “. basename( $_FILES[‘mFile’][‘name’]). " has been uploaded, and your information has been added to the directory”;

// Connects to database
include(’…/includes/mysqli_connect.php’);

if (mysqli_connect_errno($db)) {
trigger_error('Database connection failed: ’ . mysqli_connect_error(), E_USER_ERROR);
}

// add file details to database
$query = “INSERT INTO tbl_documents (file_name,file_title,file_size,client_id,file_typ,uploaded_date,file_cat, client_project) VALUES (’$file_name’,’$file_title’,’$file_size’,’$client_id’,’$file_typ’,’$uploaded_date’,’$file_cat’,’$client_project’ )”;
$result = mysqli_query($db, $query) or trigger_error("Query Failed! SQL: $query - Error: ". mysqli_error($db), E_USER_ERROR);

} else {

// gives an error if its not uploaded
echo “Sorry, there was a problem uploading your file.”;

echo “Back”;

}
}
?>[/php]

Any suggestions?

Line 20 of your uploads file should be throwing an error.

Thanks Astonecipher - it is now working, I thought I’d caught all the ‘mFile’ tags.

One item that isnt working is the script isnt adding the ‘file type’ to the database? Any suggestions?

Proofread the code backwards…

Then fix this, $file_typ

Hi astonecipher, thanks - I’d already spotted this before posting my last comment. I’ve sorted the issue out, it was to do with my database column as it stated ‘file_typ’ instead of ‘file_type’.

thanks for your help

Sponsor our Newsletter | Privacy Policy | Terms of Service