SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Enthusiast wyte raven's Avatar
    Join Date
    Feb 2007
    Location
    England, High Wycombe Bucks
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Error with PHP 5.2 setup?

    Hello Everyone!

    Ok firstly! I have read the many other threads relating to problems like this, and although I'm a newbie at PHP/MySQL I'm almost positive that this is a problem with the setup, rather than an error in the script. The reason I say this is because the book follows PHP 5.1 and I'm currently using PHP 5.2 and I've noticed that at least one of the directories was a different path to what it should have been in the book. As far as I know, I have followed all the setup instructions that Kevin Yank suggested, which further tells me that I need to do something extra for the new version. His script should work, or atleast you'd think. I'm getting this error when I upload a file:

    Warning: filesize() [function.filesize]: stat failed for C:\WINDOWS\TEMP\phpE5.tmp in C:\Inetpub\wwwroot\filestore.php on line 85

    Warning: fread() [function.fread]: Length parameter must be greater than 0 in C:\Inetpub\wwwroot\filestore.php on line 85

    Warning: Cannot modify header information - headers already sent by (output started at C:\Inetpub\wwwroot\filestore.php:85) in C:\Inetpub\wwwroot\filestore.php on line 104


    This is the script that I copied and used from the archives (note that I have only changed the password and database name).

    PHP Code:
    <?php

    $dbcnx 
    = @mysql_connect('localhost''root''password');
    if (!
    $dbcnx) {
      exit(
    '<p>Unable to connect to the ' .
          
    'database server at this time.</p>');
    }

    if (!@
    mysql_select_db('jo')) {
      exit(
    '<p>Unable to locate the joke ' .
          
    'database at this time.</p>');
    }

    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>

    <h1>PHP/MySQL File Repository</h1>

    <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>

    I'm just curious, are these settings correct? I ask because it mentions in the script about a tmp directory, I don't think I have quite grasped this yet. Should there be something set to the upload_tmp_dir setting?
    Code:
    ;;;;;;;;;;;;;;;;
    ; File Uploads ;
    ;;;;;;;;;;;;;;;;
    
    ; Whether to allow HTTP file uploads.
    file_uploads = On
    
    ; Temporary directory for HTTP uploaded files (will use system default if not
    ; specified).
    ;upload_tmp_dir =
    
    ; Maximum allowed size for uploaded files.
    upload_max_filesize = 2M
    I appreciate any help you can offer.

    Thank you

    Wyte Raven
    Last edited by wyte raven; Mar 2, 2007 at 15:54.
    Wyte R@ven - Creator of the Rift

  2. #2
    SitePoint Evangelist ClickHeRe's Avatar
    Join Date
    Mar 2005
    Location
    Ottawa, Canada
    Posts
    580
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    let's go through the errors here:

    Warning: filesize() [function.filesize]: stat failed for C:\WINDOWS\TEMP\phpE5.tmp in C:\Inetpub\wwwroot\filestore.php on line 85

    The file couldn't be found, so maybe you could explicitly specify the
    upload_tmp_dir = "C:\Windows\TEMP" as you upload temp directory, make sure it is there and writable.

    Warning: fread() [function.fread]: Length parameter must be greater than 0 in C:\Inetpub\wwwroot\filestore.php on line 85

    Because the precedent filesize() call failed, the returned value vas 0 and reading 0 bytes from the file is generating a warning that you aren't reading anything.

    Warning: Cannot modify header information - headers already sent by (output started at C:\Inetpub\wwwroot\filestore.php:85) in C:\Inetpub\wwwroot\filestore.php on line 104

    And because the 2 other warnings were brought, the content of those warnings was sent directly to the client browser and once any output is sent, the headers are already with that first output. So trying to set the headers again is an error since they are already sent (because of the output of the errors that are displayed (echo/printed)).
    David

  3. #3
    SitePoint Enthusiast wyte raven's Avatar
    Join Date
    Feb 2007
    Location
    England, High Wycombe Bucks
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hiya mate!

    Ok I explicitly specified the c:\windows\temp directory, it didn't however seem to fix the error. The only noticeable change was that the path in the error became lower case text, as expected. So I guess this setting was correct. It would be interested to find out why this code isn't working..... could there be something else in php.ini that isn't set correctly perhaps?
    Wyte R@ven - Creator of the Rift

  4. #4
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,609
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Does that TEMP folder have WRITE permission for the IUSR_ComputerName account? (Assuming you're using IIS that's built into XP pro, rather than Apache). The error might be related to you not being able to upload the file into the TEMP folder. (This is what clickhere also said).
    Ian Anderson
    www.siteguru.co.uk

  5. #5
    SitePoint Enthusiast wyte raven's Avatar
    Join Date
    Feb 2007
    Location
    England, High Wycombe Bucks
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

    I know this may sound a bit stupid......but how can I check/alter that? I right clicked the temp folder, is it something I do this way or must I check IIS?

    I am using IIS with XP pro yes.

    Appreciate any further help

    Thanks
    Wyte R@ven - Creator of the Rift

  6. #6
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,609
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Right-click on the folder | Properties | Security ... check in there for the IUSR_ComputerName account. (ComputerName is the actual name of your computer - it does not show in the list as literally ComputerName). Check what permissions level it has.
    Ian Anderson
    www.siteguru.co.uk

  7. #7
    SitePoint Enthusiast wyte raven's Avatar
    Join Date
    Feb 2007
    Location
    England, High Wycombe Bucks
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hiya mate

    Ok I right clicked the c:\windows\temp folder and clicked properties. I can't see a tab for security. Just these four tabs :

    General
    sharing
    web sharing - shares on default website (I then clicked share this folder, created an alias and enabled write permission and execute script permission)
    customize
    Is this the right approach? and do I have to do something with IIS if this is the case? I can't see anything about my computer name when I right click this folder however.
    Wyte R@ven - Creator of the Rift

  8. #8
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,609
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Internet Information Services (in Administrative Tools - might be called Internet Services Manager) won't allow you to set permissions on any folder NOT within your wwwroot file structure, unless you have shared the folder as you described. if this is the case then find that share in IIS, right-click and select Properties, then make sure the WRITE checkbox is checked.
    Ian Anderson
    www.siteguru.co.uk

  9. #9
    SitePoint Enthusiast wyte raven's Avatar
    Join Date
    Feb 2007
    Location
    England, High Wycombe Bucks
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hiya

    Ok, well I did that. Checked the file and it does seem to have write access to it.

    How odd. It seems to be uploading the descriptions, file types and the links to see the files but no images are there to browse.
    Wyte R@ven - Creator of the Rift

  10. #10
    SitePoint Enthusiast wyte raven's Avatar
    Join Date
    Feb 2007
    Location
    England, High Wycombe Bucks
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    After attempting file uploads I have checked the table via MySql to see if it contains any information, it shows this :

    Code:
    mysql> DESCRIBE filestore;
    +-------------+--------------+------+-----+---------+----------------+
    | Field       | Type         | Null | Key | Default | Extra          |
    +-------------+--------------+------+-----+---------+----------------+
    | id          | int(11)      | NO   | PRI | NULL    | auto_increment |
    | filename    | varchar(255) | NO   |     |         |                |
    | mimetype    | varchar(50)  | NO   |     |         |                |
    | description | varchar(255) | NO   |     |         |                |
    | filedata    | mediumblob   | YES  |     | NULL    |                |
    +-------------+--------------+------+-----+---------+----------------+
    5 rows in set (0.14 sec)
    
    mysql> SELECT * FROM filestore;
    +----+-------------------+-------------+-------------+----------+
    | id | filename          | mimetype    | description | filedata |
    +----+-------------------+-------------+-------------+----------+
    | 27 | kat_nick_nat1.jpg | image/pjpeg |             |          |
    +----+-------------------+-------------+-------------+----------+
    1 row in set (0.01 sec
    When I check the updated code on the web page (after reloading the file again after the error) it shows this:

    PHP/MySQL File Repository
    Upload File: (browse button here)
    (text box here)

    File Description:
    (textbox here)

    (upload button here)

    The following files are stored in the database:

    (info displayed in table, link to file, download and delete links)
    Filename Type Description
    kat_nick_nat1.jpg image/pjpeg [Download | Delete]

    Can someone tell me, are the files actually being uploaded? I get the impression that the file descriptions, types and names are all being uploaded to the database and not the file refs themselves.

    I'd appreciate any further help..
    Wyte R@ven - Creator of the Rift

  11. #11
    SitePoint Evangelist ClickHeRe's Avatar
    Join Date
    Mar 2005
    Location
    Ottawa, Canada
    Posts
    580
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, the script will still execute even if it can't write the file because it doesn't trap that error (by checking if the file was copied).

    I don't use IIS so I can't really help you further with troubleshooting your setup.
    David

  12. #12
    SitePoint Enthusiast wyte raven's Avatar
    Join Date
    Feb 2007
    Location
    England, High Wycombe Bucks
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hiya

    Is there a way I can find out or gather a more informative error report? I have noticed that by changing line 81 and 85 it seems to alter the error as expected. I just think it's odd that I'm getting this error, others have had it but for some reason theres no mention of how it's fixed. Could it be a bug in PHP? This I doubt but I have tried everything I can think of as a newbie.

    Lines 81 and 85 respectively:

    PHP Code:
    // 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)); 
    Wyte R@ven - Creator of the Rift


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •