Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 6 of 6
  1. #1
    Senior Member DesignBox's Avatar
    Join Date
    Apr 2004
    Posts
    340
    Member #
    5659
    I have the following file in an admin folder of a website I am creating.
    Everytime i try to run it i get this error: Database error: Access denied for user 'ODBC'@'localhost' (using password: NO)

    PHP Code:
    <?php
    session_start
    ();
    if (!ISSET(
    $_SESSION['username'])) {
    header ("Location: index.php"); }

    if (isset(
    $_GET['action'])) {
      
    $action $_GET['action'];
    } else {
      
    $action '';
    }

    if ((
    $action == 'view' or $action == 'dnld') and isset($_GET['id'])) {
      
    $id $_GET['id'];

      
    // User is retrieving a file
      
    $sql "SELECT filename, mimetype, filedata
          FROM filestore WHERE id = '
    $id'";
      
    $result = @mysql_query($sql);
      if (!
    $result) {
        exit(
    'Database error: ' mysql_error());
      }
      
      
    $file mysql_fetch_array($result);
      if (!
    $file) {
        exit(
    'File with given ID not found in database!');
      }
        
      
    $filename $file['filename'];
      
    $mimetype $file['mimetype'];
      
    $filedata $file['filedata'];
      
    $disposition 'inline';
      
      if (
    $action == 'dnld') {
        
    $disposition 'attachment';
        if (
    strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 5') or
            
    strpos($_SERVER['HTTP_USER_AGENT'], 'Opera 7')) {
          
    $mimetype 'application/x-download';
        }
      }
      
      
    header("content-disposition: $disposition; filename=$filename");
      
    header("content-type: $mimetype");
      
    header('content-length: ' strlen($filedata));
        
      echo(
    $filedata);
      exit();
      
    } elseif (
    $action == 'del' and isset($_GET['id'])) {
      
    $id $_GET['id'];
        
      
    // User is deleting a file
      
    $sql "DELETE FROM filestore WHERE id = '$id'";
      
    $ok = @mysql_query($sql);
      if (!
    $ok) {
        exit(
    'Database error: ' mysql_error());
      }

      
    header('location: ' $_SERVER['PHP_SELF']);
      exit();

    } elseif (isset(
    $_FILES['upload'])) {

      
    // Bail out if the file isn?t really an upload.
      
    if (!is_uploaded_file($_FILES['upload']['tmp_name'])) {
        exit(
    'There was no file uploaded!');
      }
      
    $uploadfile $_FILES['upload']['tmp_name'];
      
    $uploadname $_FILES['upload']['name'];
      
    $uploadtype $_FILES['upload']['type'];
      
    $uploaddesc $_POST['desc'];
          
      
    // Open file for binary reading ('rb')
      
    $tempfile fopen($uploadfile'rb');

      
    // Read the entire file into memory using PHP's
      // filesize function to get the file size.
      
    $filedata fread($tempfilefilesize($uploadfile));

      
    // Prepare for database insert by adding backslashes
      // before special characters.
      
    $filedata addslashes($filedata);

      
    // Create the SQL query.
      
    $sql "INSERT INTO filestore SET
          filename = '
    $uploadname',
          mimetype = '
    $uploadtype',
          description = '
    $uploaddesc',
          filedata = '
    $filedata'";

      
    // Perform the insert.
      
    $ok = @mysql_query($sql);
      if (!
    $ok) {
        exit(
    'Database error storing file: ' mysql_error());
      }

      
    header('location: ' $_SERVER['PHP_SELF']);
      exit();

    }

    // Default page view: lists stored files

    $sql 'SELECT id, filename, mimetype, description FROM filestore';
    $filelist = @mysql_query($sql);
    if (!
    $filelist) {
      exit(
    'Database error: ' mysql_error());
    }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>PHP/MySQL File Repository</title>
    <meta http-equiv="content-type"
        content="text/html; charset=iso-8859-1" />
    </head>
    <body>
            <form action="<?php echo $_SERVER['PHP_SELF']; ?>"
        method="post" enctype="multipart/form-data">
      <p><label>Upload File:<br />
        <input type="file" name="upload" /></label></p>
      <p><label>File Description:<br />
        <input type="text" name="desc" maxlength="255" /></label></p>
      <p><input type="submit" value="Upload" /></p>
    </form>
            <p>The following files are stored in the database:</p>
    <table>
    <tr>
      <th>Filename</th>
      <th>Type</th>
                    <th>Description</th>
                </tr>
    <?php

    if (mysql_num_rows($filelist) > 0) {
      while (
    $f mysql_fetch_array($filelist)) {
        
    ?>

    <tr valign="top">
      <td>
        <a href="<?php echo $_SERVER['PHP_SELF']; ?>?action=view&id=<?php echo $f['id']; ?>">
          <?php echo $f['filename']; ?></a>
      </td>
      <td><?php echo $f['mimetype']; ?></td>
      <td><?php echo $f['description']; ?></td>
      <td>
        [<a href="<?php echo $_SERVER['PHP_SELF']; ?>?action=dnld&id=<?php echo $f['id']; ?>"
          >Download</a> |
        <a href="<?php echo $_SERVER['PHP_SELF']; ?>?action=del&id=<?php echo $f['id']; ?>"
          onclick="return confirm('Delete this file?');"
          >Delete</a>]
      </td>
    </tr>

        <?php
      
    }
    } else {
      
    ?>
      <tr>
                    <td colspan="3">No Files!</td>
                </tr>
      <?php
    }
    ?>
    </table>
    </body>
    </html>

  2.  

  3. #2
    Senior Member
    Join Date
    May 2003
    Location
    UK
    Posts
    2,354
    Member #
    1326
    It looks like you know your stuff, so I am not sure if I am climbing the right branch..

    However I do not see any database connection code in your script.

    Normally, in my case anyway, I stick this code in it's own file and include it.

    So yeah, where is your mysql_select_db() and such.

  4. #3
    Senior Member DesignBox's Avatar
    Join Date
    Apr 2004
    Posts
    340
    Member #
    5659
    Yes i totally forgot the database connection!!

    I have another problem now though. When I'm trying to upload a file I get a different message: Warning: Cannot modify header information - headers already sent by (output started at xxxxxxxxxxxxx\admin\db.inc.php:12) in xxxxxxxxxxxxx\admin\filestore.php on line 98

    My file is actually uploaded though.

    Line 98 is: header('location: ' . $_SERVER['PHP_SELF']);

    PHP Code:
    <?php
    session_start
    ();
    if (!ISSET(
    $_SESSION['username'])) {
    header ("Location: index.php"); }

    include 
    'db.inc.php';

    if (isset(
    $_GET['action'])) {
      
    $action $_GET['action'];
    } else {
      
    $action '';
    }

    if ((
    $action == 'view' or $action == 'dnld') and isset($_GET['id'])) {
      
    $id $_GET['id'];

      
    // User is retrieving a file
      
    $sql "SELECT filename, mimetype, filedata
          FROM filestore WHERE id = '
    $id'";
      
    $result = @mysql_query($sql);
      if (!
    $result) {
        exit(
    'Database error: ' mysql_error());
      }
      
      
    $file mysql_fetch_array($result);
      if (!
    $file) {
        exit(
    'File with given ID not found in database!');
      }
        
      
    $filename $file['filename'];
      
    $mimetype $file['mimetype'];
      
    $filedata $file['filedata'];
      
    $disposition 'inline';
      
      if (
    $action == 'dnld') {
        
    $disposition 'attachment';
        if (
    strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 5') or
            
    strpos($_SERVER['HTTP_USER_AGENT'], 'Opera 7')) {
          
    $mimetype 'application/x-download';
        }
      }
      
      
    header("content-disposition: $disposition; filename=$filename");
      
    header("content-type: $mimetype");
      
    header('content-length: ' strlen($filedata));
        
      echo(
    $filedata);
      exit();
      
    } elseif (
    $action == 'del' and isset($_GET['id'])) {
      
    $id $_GET['id'];
        
      
    // User is deleting a file
      
    $sql "DELETE FROM filestore WHERE id = '$id'";
      
    $ok = @mysql_query($sql);
      if (!
    $ok) {
        exit(
    'Database error: ' mysql_error());
      }

      
    header('location: ' $_SERVER['PHP_SELF']);
      exit();

    } elseif (isset(
    $_FILES['upload'])) {

      
    // Bail out if the file isn?t really an upload.
      
    if (!is_uploaded_file($_FILES['upload']['tmp_name'])) {
        exit(
    'There was no file uploaded!');
      }
      
    $uploadfile $_FILES['upload']['tmp_name'];
      
    $uploadname $_FILES['upload']['name'];
      
    $uploadtype $_FILES['upload']['type'];
      
    $uploaddesc $_POST['desc'];
          
      
    // Open file for binary reading ('rb')
      
    $tempfile fopen($uploadfile'rb');

      
    // Read the entire file into memory using PHP's
      // filesize function to get the file size.
      
    $filedata fread($tempfilefilesize($uploadfile));

      
    // Prepare for database insert by adding backslashes
      // before special characters.
      
    $filedata addslashes($filedata);

      
    // Create the SQL query.
      
    $sql "INSERT INTO filestore SET
          filename = '
    $uploadname',
          mimetype = '
    $uploadtype',
          description = '
    $uploaddesc',
          filedata = '
    $filedata'";

      
    // Perform the insert.
      
    $ok = @mysql_query($sql);
      if (!
    $ok) {
        exit(
    'Database error storing file: ' mysql_error());
      }

      
    header('location: ' $_SERVER['PHP_SELF']);
      exit();

    }

    // Default page view: lists stored files

    $sql 'SELECT id, filename, mimetype, description FROM filestore';
    $filelist = @mysql_query($sql);
    if (!
    $filelist) {
      exit(
    'Database error: ' mysql_error());
    }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>PHP/MySQL File Repository</title>
    <meta http-equiv="content-type"
        content="text/html; charset=iso-8859-1" />
    </head>
    <body>
            <form action="<?php echo $_SERVER['PHP_SELF']; ?>"
        method="post" enctype="multipart/form-data">
      <p><label>Upload File:<br />
        <input type="file" name="upload" /></label></p>
      <p><label>File Description:<br />
        <input type="text" name="desc" maxlength="255" /></label></p>
      <p><input type="submit" value="Upload" /></p>
    </form>
            <p>The following files are stored in the database:</p>
    <table>
    <tr>
      <th>Filename</th>
      <th>Type</th>
                    <th>Description</th>
                </tr>
    <?php

    if (mysql_num_rows($filelist) > 0) {
      while (
    $f mysql_fetch_array($filelist)) {
        
    ?>

    <tr valign="top">
      <td>
        <a href="<?php echo $_SERVER['PHP_SELF']; ?>?action=view&id=<?php echo $f['id']; ?>">
          <?php echo $f['filename']; ?></a>
      </td>
      <td><?php echo $f['mimetype']; ?></td>
      <td><?php echo $f['description']; ?></td>
      <td>
        [<a href="<?php echo $_SERVER['PHP_SELF']; ?>?action=dnld&id=<?php echo $f['id']; ?>"
          >Download</a> |
        <a href="<?php echo $_SERVER['PHP_SELF']; ?>?action=del&id=<?php echo $f['id']; ?>"
          onclick="return confirm('Delete this file?');"
          >Delete</a>]
      </td>
    </tr>

        <?php
      
    }
    } else {
      
    ?>
      <tr>
                    <td colspan="3">No Files!</td>
                </tr>
      <?php
    }
    ?>
    </table>
    </body>
    </html>

  5. #4
    Junior Member
    Join Date
    Nov 2005
    Location
    Christchurch, New Zealand
    Posts
    17
    Member #
    11987
    Designbox:

    Warning: Cannot modify header information - headers already sent by (output started at xxxxxxxxxxxxx\admin\db.inc.php:12) in xxxxxxxxxxxxx\admin\filestore.php on line 98

    The above warning message, from my memory, is telling us that you are trying to trying to change the page headers (on line 98 of filestore.php) when they have already been created. You have something outputing to the browser (in db.inc.php) on line 12. This finalizes the headers and thus means they cannot be changed again.

    I would check that you dont have an echo or print command in db.inc.php (presumably on line 12) that is being executed before you make your header call in filestore.php

    The reason your file/files are still being uploaded however is because the above message is simply a warning, it is not a fatal error which would prevent the rest of the script from running.

    Hope that helps and wasnt too confusing

  6. #5
    Senior Member DesignBox's Avatar
    Join Date
    Apr 2004
    Posts
    340
    Member #
    5659
    The empty spaces were in the include file!! thank you so much it works now

  7. #6
    Junior Member
    Join Date
    Nov 2005
    Location
    Christchurch, New Zealand
    Posts
    17
    Member #
    11987
    Glad I could help


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 09:14 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com