Solved

Upload form, but when I don't upload anything file there still goes textfile named untitled.txt? How i get that away?

Posted on 2014-03-03
1
377 Views
Last Modified: 2014-03-03
I have little upload form, but when I don't upload anything there still goes to mysql database textfile named untitled.txt? How i get that away? Thank you so much in advance...
Here is mys form:

<form action="lisaa_tiedosto_uutinen_action2.php" method="post" enctype="multipart/form-data">

      Title: <br><input name="title" size="40" maxlength="255">
      <br><br>
      Text1:<br> <textarea name="text1"  rows="7" cols="30"></textarea>
      <br>
      <br>
      <input type="file" name="uploaded_file">
      <br>
      <br>
            <input type="submit" value="Lisää uutinen">
</form>

Open in new window


Here is that upload page code:

<?php
// NEVER SUPPRES NOTICE MESSAGES - INSTEAD CORRECT THE UNDERLYING CONDITIONS THAT CAUSE THE NOTICE
error_reporting(E_ALL);

$dbLink = new mysqli('localhost', 'user', 'pass', 'db');
if(!$dbLink) 
{
    trigger_error("MySQL connection failed: ". mysqli_connect_error(), E_USER_ERROR);
}

// IF THERE WAS NO FILE UPLOADED
if ($_FILES['uploaded_file']['error'] == 4) 
{
    /* RUN THE QUERY WITHOUT THE FILE DATA 
     */
	 
	 // ESCAPE THE EXTERNAL DATA THAT WILL BE NEEDED
$title = $dbLink->real_escape_string($_POST['title']);
$text1 = $dbLink->real_escape_string($_POST['text1']);
	 
	             $query1 = "
                INSERT INTO `uutiset_tiedostot` (
                   `title`, `dtime`, `text1`
                )
                VALUES (
                    '$title',NOW(),'$text1'
                )";
				// Execute the query
            $result = $dbLink->query($query1);
			if($result) {
                echo 'Success 1! Your file was successfully added!';
            }
}
// IF THERE WAS A CLEAN UPLOAD
elseif ($_FILES['uploaded_file']['error'] == 0)
{

// Gather all required data
// ESCAPE THE EXTERNAL DATA THAT WILL BE NEEDED
$title = $dbLink->real_escape_string($_POST['title']);
$text1 = $dbLink->real_escape_string($_POST['text1']);
            $name = $dbLink->real_escape_string($_FILES['uploaded_file']['name']);
            $mime = $dbLink->real_escape_string($_FILES['uploaded_file']['type']);
            $data = $dbLink->real_escape_string(file_get_contents($_FILES  ['uploaded_file']['tmp_name']));
            $size = intval($_FILES['uploaded_file']['size']);
    /* USE Move_UpLoaded_File() 
     * ESCAPE NAME, TYPE, ETC
     * RUN THE QUERY WITH THE FILE DATA
     */
	 
	             $query2 = "
                INSERT INTO `uutiset_tiedostot` (
                   `title`, `dtime`, `text1`, `name`, `mime`, `size`, `data`, `created`
                )
                VALUES (
                    '$title',NOW(),'$text1','{$name}', '{$mime}', {$size}, '{$data}', NOW()
                )";
				// Execute the query
            $result = $dbLink->query($query2);
			if($result) {
                echo 'Success 2!  Your file was successfully added!';
            }
	 
}

?>

Open in new window

0
Comment
Question by:Enska77
1 Comment
 
LVL 14

Accepted Solution

by:
shobinsun earned 500 total points
Comment Utility
Hi,

Check your database table for the default values to the fields or please give the tablke structure in sql file.

For reference you cna follow the below link:

http://php.about.com/od/phpwithmysql/ss/Upload_file_sql.htm

Regards,
Shobin markose.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now