#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    6
    Rep Power
    0

    Download works on localhost but not on server


    I'm trying out this very basic script to upload a file to a database and move file to a directory and then download the file.
    this script works fine on localhost, and every thing but the download works on the server.

    I'm getting nothing in the error log just a http 500 page.
    Only with the files that haven't expired.

    any ideas?

    Thanks much!

    PHP Code:
    <?php

    include('core/inc/init.inc.php');

    if (isset(
    $_POST['expiry'], $_FILES['file'])) {
        
    $file_name mysql_real_escape_string ($_FILES ['file'] ['name']);
        
    $expiry    time() + ((int)$_POST ['expiry'] * 60 60);
        
        
    mysql_query("INSERT INTO `files`(`file_name`, `file_expiry`) VALUES ('{$file_name}',{$expiry})");
        
        
    move_uploaded_file($_FILES['file'] ['tmp_name'], "core/files/{$_FILES ['file'] ['name']}");
    }

    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN
    <html xmlns="">
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title>Upolad A File</title>
      </head>
      <body>
          <div>
             <form action="" method="post"    enctype="multipart/form-data">
                <p>
                      <input type="text" name="expiry"/>
                </p>
                <p> 
                      <input type="file" name="file"/>
                  </p>
                <p>
                       <input type="submit" value="upload"/>
                </p>
             </form>
           </div>
       </body>
    </html>
    PHP Code:
    <?php

    include('core/inc/init.inc.php');

    $files mysql_query ('SELECT `file_id`, `file_name`,`file_expiry` FROM `files`')

    ?>
    <!DOCTYPE html PUBLIC '\"-//WC3//DTD XHTML 1.0 STRICT/EN " ">
    <html xmlns="">
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <style type="text/css">
            Table {border collapse:collapse; width:600px;}
            td,th {border:solid 1px #999; padding:4px;}
        </style>    
        <title>Temorary Download</title>
      </head>
      <body>
          <table>
              <tr> 
                   <th>File Name</th>
                   <th>Expiry</th>
              </tr>
              <?php

              
    while (($row mysql_fetch_assoc($files)) !== false) {
                 
    ?>
                 <tr>
                      <td><a href ="download.php?file_id=<?php echo $row['file_id'];?>"><?php echo $row['file_name'];?></a></td>
                      <td><?php echo date('d/m/Y H:i:s'$row['file_expiry']);?></td>
                 </tr>    
                 <?php 
                
    }
                 
    ?>            
           </table>
        </body>
    </html>
    PHP Code:
    <?php
    require_once ('core/inc/init.inc.php');

    if (isset(
    $_GET ['file_id'] )) {
        
    $file_id = (int)$_GET['file_id'];
        
        
    $file mysql_query ("SELECT `file_name`, `file_expiry` FROM `files` WHERE `file_id` = {$file_id}");
        
        if (
    mysql_num_rows($file) != 1) {
            echo 
    'Invalid File ID';
        }else{    
            
    $row mysql_fetch_assoc($file);
            
            if (
    $row['file_expiry'] < time()) {
                echo 
    'This file has expired.';
            }else{
                
    $path = ("core/files/{$row->file_name}");
               
               
                
    header ('content-type: application/octetstream');            
                
    header ('content-type: application/octet-stream');
                
    header ('content-description: File Transfer');
                
    header ("content-disposition: attachment;filename=\"{$row['file_name']}\"");
                
    header ('content-length ' filesize ($path));
                
                
    readfile($path);
            }
        }
    }
        
    ?>
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,113
    Rep Power
    9398
    If the server is sending a 500 and you didn't tell it to then there will be something in an error log saying why. You looked at Apache's log, right?

    I don't see how the download script works at all on your machine. It has two glaring errors in it:
    PHP Code:
    $path = ("core/files/{$row->file_name}"); 
    $row is not an object.
    PHP Code:
    header ('content-length ' filesize ($path)); 
    Syntax.

    Plus the two Content-Types
    PHP Code:
    header ('content-type: application/octetstream');
    header ('content-type: application/octet-stream'); 
    (remove the first one)
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    6
    Rep Power
    0

    Thank you


    I was playing around with the ...
    $path = ("core/files/{$row->file_name}");
    and didn't change it back....

    $path = ("core/files/{$row[file_name]}");

    And I lost the first...
    header ('content-type: application/octetstream');

    And fixed the missing colon....
    header ('content-length ' . filesize ($path));

    Some times it takes a fresh pair of eyes to point out these things. Basically it was the missing colon,
    right from the start.

    Now it works fine

    Thank you very much for pointing that out!!!



    Originally Posted by requinix
    If the server is sending a 500 and you didn't tell it to then there will be something in an error log saying why. You looked at Apache's log, right?

    I don't see how the download script works at all on your machine. It has two glaring errors in it:
    PHP Code:
    $path = ("core/files/{$row->file_name}"); 
    $row is not an object.
    PHP Code:
    header ('content-length ' filesize ($path)); 
    Syntax.

    Plus the two Content-Types
    PHP Code:
    header ('content-type: application/octetstream');
    header ('content-type: application/octet-stream'); 
    (remove the first one)
  6. #4
  7. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,113
    Rep Power
    9398
    Originally Posted by whomee
    $path = ("core/files/{$row[file_name]}");
    That's actually incorrect too. Choose one of the correct methods:
    PHP Code:
    $path = ("core/files/{$row["file_name"]}");
    $path = ("core/files/$row[file_name]");
    $path = ("core/files/" $row["file_name"]); 
    And while you're at it, drop the parentheses. They're not doing anything there.

IMN logo majestic logo threadwatch logo seochat tools logo