Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 439
  • Last Modified:

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

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
Enska77
Asked:
Enska77
1 Solution
 
shobinsunCommented:
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now