I have a script that uploads documents, it uploads them in the destination folder, but not the info about the document. Pleasa help i need this as soon as possible. No errors show.

<?php
$UploadDirectory    = 'C:/wamp/www/agenda1/uploaddok/uploads/'; //Upload Directory, ends with slash & make sure folder exist
$SuccessRedirect    = 'http://localhost:8080/agenda1/index2.php'; //Redirect to a URL after success

// replace with your mysql database details

$MySql_username     = "root"; //mysql username
$MySql_password     = ""; //mysql password
$MySql_hostname     = "localhost"; //hostname
$MySql_databasename = "agenda"; //databasename


if (!@file_exists($UploadDirectory)) {
    //destination folder does not exist
    die("Make sure Upload directory exist!");
}

if($_POST)
{   
    if(!isset($_POST['mName']) || strlen($_POST['mName'])<1)
    {
        //required variables are empty
        die("Title is empty!");
    }


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

    $FileName           = strtolower($_FILES['mFile']['name']); //uploaded file name
    $FileTitle          = mysql_real_escape_string($_POST['mName']); // file title
    $ImageExt           = substr($FileName, strrpos($FileName, '.')); //file extension
    $FileType           = $_FILES['mFile']['type']; //file type
    $FileSize           = $_FILES['mFile']["size"]; //file size
    $RandNumber         = rand(0, 9999999999); //Random number to make each filename unique.
    $uploaded_date      = date("Y-m-d H:i:s");

    switch(strtolower($FileType))
    {
        //allowed file types
        case 'image/png': //png file
        case 'image/gif': //gif file 
        case 'image/jpeg': //jpeg file
        case 'application/pdf': //PDF file
        case 'application/docx': //ms word file
        case 'application/vnd.ms-excel': //ms excel file
        case 'application/x-zip-compressed': //zip file
        case 'text/plain': //text file
        case 'text/html': //html file
            break;
        default:
            die('Unsupported File!'); //output error
    }


    //File Title will be used as new File name
    $NewFileName = preg_replace(array('/\s/', '/\.[\.]+/', '/[^\w_\.\-]/'), array('_', '.', ''), strtolower($FileTitle));
    $NewFileName = $NewFileName.'_'.$RandNumber.$ImageExt;
   //Rename and save uploded file to destination folder.
   if(move_uploaded_file($_FILES['mFile']["tmp_name"], $UploadDirectory . $NewFileName ))
   {
        //connect & insert file record in database
        $dbconn = mysql_connect($MySql_hostname, $MySql_username, $MySql_password)or die("Unable to connect to MySQL");
        mysql_select_db($MySql_databasename,$dbconn);
        @mysql_query("INSERT INTO file_records (file_name, file_title, file_size, uploaded_date) VALUES ('$NewFileName', '$FileTitle',$FileSize,'$uploaded_date')");
        mysql_close($dbconn);

        header('Location: '.$SuccessRedirect); //redirect user after success

   }else{
        die('error uploading File!');
   }
}

//function outputs upload error messages, http://www.php.net/manual/en/features.file-upload.errors.php#90522
function upload_errors($err_code) {
    switch ($err_code) { 
        case UPLOAD_ERR_INI_SIZE: 
            return 'The uploaded file exceeds the upload_max_filesize directive in php.ini'; 
        case UPLOAD_ERR_FORM_SIZE: 
            return 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'; 
        case UPLOAD_ERR_PARTIAL: 
            return 'The uploaded file was only partially uploaded'; 
        case UPLOAD_ERR_NO_FILE: 
            return 'No file was uploaded'; 
        case UPLOAD_ERR_NO_TMP_DIR: 
            return 'Missing a temporary folder'; 
        case UPLOAD_ERR_CANT_WRITE: 
            return 'Failed to write file to disk'; 
        case UPLOAD_ERR_EXTENSION: 
            return 'File upload stopped by extension'; 
        default: 
            return 'Unknown upload error'; 
    } 
} 
?>
Member Avatar for LastMitch

I have a script that uploads documents, it uploads them in the destination folder, but not the info about the document. Pleasa help i need this as soon as possible. No errors show.

I don't see any issue? You mention there's no error?

Can you INSERT the data if not it means there something wrong with your query.

I assume you are connected to the database. Then the issue is your query.

Put this code immediately after line 68:

die("INSERT INTO file_records (file_name, file_title, file_size, uploaded_date) VALUES ('$NewFileName', '$FileTitle',$FileSize,'$uploaded_date')");

It will display the query as it gets contructed using the variables. Paste it into phpmyadmin and check for errors. You can also post it here.

BTW: none of the data in the query is binary I guess. You are not inserting the file contents to database, are you (just looking at the title of the post)?

So what is the function that stores the data of the binary file in the database? In my code it's supposed to be lines 63-69

If you ask me, none of the fields here are binary:

@mysql_query("INSERT INTO file_records (file_name, file_title, file_size, uploaded_date) VALUES ('$NewFileName', '$FileTitle',$FileSize,'$uploaded_date')");

file_name and file_title should be strings, file size could be integer, uploaded date should be integer (if timestamp) or date. But if you wanted to store binary file in database the table field should be declared binary or varbinary. The query itself does not change.

I want to store in the db not the binary file itself, onlu info about it like tilte, file, size and date posted, so this i what i get when i add tha die line

INSERT INTO file_records (file_name, file_title, file_size, uploaded_date) VALUES ('hjgbhjbj_116056714.jpg', 'hjgbhjbj',879394,'2013-02-18 21:04:42')

but nothing happens in the db

The query itself looks OK. Please double check if table name is file_records, and field names are also spelled correctly. Please paste the displayed query into the SQL tab of phpmyadmin and test if it works OK. And do not to remove the die() line.

Also change line 67 to include a check:

if(!mysql_select_db($MySql_databasename,$dbconn)) {
    die("Could not select database");
}

Do similar checks when connecting to the database and when performing queries. And temporary remove the @ form the mysql_query command on line 68 so you can see possible error messages.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.