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

    Join Date
    Aug 2013
    Posts
    6
    Rep Power
    0

    Can't find the file on server glitch


    Okay here I go again, same problem different script.
    I working with this file upload everything works but the download file.

    I'm getting the error message 'The file could not be located on the server'

    any ideas? Thanks!

    PHP Code:
    <?php # add_file.php
       // This page allows users to upload files to the server.

       // Set the page title and include the HTML header.
       
    $page_title 'Upload a File';
       include (
    'include/header.html');

       
    $counter 3// Number of files to allow for.

       
    if (isset($_POST['submitted'])) { // Handle the form.

           
    require_once ('mysql_connect.php'); // Connect to the database.

           
    for ($i 0$i $counter$i++) { // Handle each uploaded file.

               // Create index names to refer to the proper upload and description.
               
    $filename 'uploads' $i;
               
    $description 'description' $i;

               
    // Check for a file.
               
    if (isset($_FILES[$filename]) && ($_FILES[$filename]['error'] != 4)) { 

               
    // Check for a description (not required).
               
    if (!empty($_POST[$description])) {
                   
    $d "'" escape_data($_POST [$description]) . "'";

               } else {
                   
    $d 'NULL';
               }

               
    // Add the record to the dataabase.
               
    $query "INSERT INTO uploads (file_name, file_size, file_type,
                 description) VALUES ('
    {$_FILES[$filename]['name']}',{$_FILES [$filename]['size']},'{$_FILES [$filename]['type']}', $d)";
                 
    $result mysql_query ($query); 
                  
               if (
    $result) {

                   
    // Return the upload_id from the database.
                   
    $upload_id mysql_insert_id(); 

                   
    // Move the file over.
                   
    if( move_uploaded_file($_FILES[$filename]['tmp_name'], "upload/
                     
    $upload_id")) {

                       echo 
    '<p>File number ' . ($i 1) . ' has been uploaded!</p>';

                   } else { 
    // File could not be moved.

                       
    echo '<p><font color="red">File number ' . ($i 1) . ' could not
                         be moved.</font></p>'
    ;

                       
    // Remove the record from the database.
                      
    $query "DELETE FROM uploads WHERE upload_id = $upload_id";
                       
    $result mysql_query ($query);

                       
    // Add more detailed error reporting, if desired.

                       


                   } else { 
    // If the query did not run OK.
                       
    echo '<p><font color="red">Your submission could not be processed
                         due to a system error. We apologize for any 
                         inconvenience.</font></p>'
    ;
                       
    // Print the query and invoke the mysql_error() function to debug.
                   
    }

               } 
    // End of if (isset($the_file)...

           
    // End of FOR loop.

           
    mysql_close(); // Close the database connection.

       
    }
       
    ?>
       <form enctype="multipart/form-data" action="add_file.php" method="post">

           <fieldset><legend>Fill out the form to upload a file:</legend>
           <input type="hidden" name="MAX_FILE_SIZE" value="5242880" /> 

           <?php // Create the inputs.
           
    for ($i 0$i $counter$i++) {
               echo 
    '<p><b>File:</b> <input type="file" name="uploads' $i '" /></p>
           <p><b>Description:</b> <textarea name="description' 
    $i '" cols="40"
             rows="5"></textarea></p><br />
           '
    ;
           }
           
    ?>

           </fieldset>
           <input type="hidden" name="submitted" value="TRUE" />
           <div align="center"><input type="submit" name="submit" value="Submit" /></div>

       </form>
       <?php
       
    include ('include/footer.html');
       
    ?>
    PHP Code:
    <?php # view_files.php
       // This page displays the files uploaded to the server.

       // Set the page title and include the HTML header.
       
    $page_title 'View Files';
       include (
    'include/header.html');

       require_once (
    'mysql_connect.php');

       
    $first TRUE// Initialize the variable.

       // Query the database.
       
    $query "SELECT upload_id, file_name, ROUND(file_size/1024) AS fs, description, DATE_FORMAT(date_entered, '%M %e, %Y') AS d FROM uploads ORDER BY date_entered DESC";
       
    $result mysql_query ($query); 


       
    // Display all URLs.
       
    while ($row mysql_fetch_array($resultMYSQL_ASSOC)) {

            
    // If this is the first record, create the table header.
            
    if ($first) {
                echo 
    '<table border="0" width="100%" cellspacing="3" cellpadding="3" align="center">
            <tr>
                <td align="left" width="20%"><font size="+1">File Name</font></td>
                <td align="left" width="40%"><font size="+1">Description</font></td> 
                <td align="center" width="20%"><font size="+1">File Size</font></td>
                <td align="left" width="20%"><font size="+1">Upload Date</font></td>
            </tr>'
    ;
                
    $first FALSE// Once record has been returned.

            
    // End of $first IF.

            // Display each record.
            
    echo " <tr>
                <td align=\"left\"><a href=\"download_file.php?uid=<?php
    {$row['upload_id']};?>\">{$row['file_name']}</a></td>
                <td align=\"left\">" 
    stripslashes($row ['description']) . "</td>
                <td align=\"center\">
    {$row ['fs']}kb</td>
                <td align=\"left\">
    {$row ['d']}</td>
                <td align=\"left\"><a href=\"edit_file.php?uid=
    {$row['upload_id']}\">edit</a></td>
            </tr>\n"
    ;

       } 
    // End of while loop.

       // If no records were diplayed...
       
    if ($first) {
            echo 
    '<div align="center">There are currently no files to be viewed.</div>';
       } else {
            echo 
    '</table>'// Close the table.
       
    }

       
    mysql_close(); // Close the database connetion.
       
    include ('include/footer.html');
       
    ?>
    PHP Code:
       <?php # download_file.php
       // This page handles file downloads through headers.

       // Check for an upload_id.
       
    if (isset($_GET['uid'])) {
           
    $uid = (int) $_GET['uid'];
       } else { 
    // Big problem!
           
    $uid 0;
       }

       if (
    $uid 0) { // OK to proceed!

           
    require_once ('mysql_connect.php');
           
           
    // Connect to the dataabase.

           // Get the information for this file.
           
    $query "SELECT 'file_name', 'file_size', 'file_type' FROM 'uploads' WHERE
             upload_id=
    $uid";
           
    $result mysql_query ($query);

           list (
    $fn$fs$ft) = mysql_fetch_array ($resultMYSQL_NUM);
           
    mysql_close(); // Close the database connection.

           // Determine the file name on the server.
           
    $the_file 'upload' '$uid'

           
    // Check if it exists.
           
    if (file_exists ($the_file)) { 

               
    // Send the file.
               
    header ('content-type: application/octet-stream');
               
    header ('content-description: File Transfer');
               
    header ("Content-disposition: attachment; filename=\"$fn\"\n"); 
               
    header ("Content-Length: $fs\n");
               
    header ("Content-Type: $ft\n");
               

               
    readfile ($the_file); 

           } else { 
    // File doesn't exist.
               
    $page_title 'File Download';
               include (
    'include/header.html');
               echo 
    '<p><font color="red">The file could not be located on the server.
                 We apologize for any inconvenience.</font></p>'
    ;
               include (
    'include/footer.html');
           }

       } else { 
    // No valid upload ID.
           
    $page_title 'File Download';
           include (
    'include/header.html');
           echo 
    '<p><font color="red">Please select a valid file to download.
             </font></p>'
    ;
           include (
    'include/footer.html');
       }
       
    ?>
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    PHP Code:
           $the_file 'upload' '$uid'
    That makes $the_fule the string 'upload$uid'. You can't put single-quotes around variables.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    6
    Rep Power
    0

    Thanks for the tip!


    I tried it like this..

    $the_file = 'upload' . $uid;

    And like this..

    $the_file = upload . $uid;

    still now file download.

    Thanks for your fast response!
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    PHP Code:
                   if( move_uploaded_file($_FILES[$filename]['tmp_name'], "upload/ 
                     
    $upload_id")) { 
    There's a slash in the path when you move the file.

    Edit: In addition to the slash, there's a newline and a couple dozen spaces.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    6
    Rep Power
    0

    Tried this


    Okay that's from the add_files script that part works
    But I removed the slash and the spaces nothings changed
    the upload still works but no download


    if( move_uploaded_file($_FILES[$filename]['tmp_name'], "upload $upload_id")) {
  10. #6
  11. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    No...the slash is supposed to be there (I assume). You're PUTTING the file somewhere ("uploads/uid") and then LOOKING for it somewhere else ("uploadsuid").

    This is very basic debugging. If the upload works, leave it. Finding the file doesn't work. Why? Problem 1: you were using single quotes. Fix it. Still broken? Why else? Oh, the path isn't right. Fix it. Still broken? Go look at other things.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    6
    Rep Power
    0

    That's what I've been doing


    I've tried everything I can think of permissions to full path
    and if I use phpmyadmin to test the sql it works, in my opinion it has something to do with the $uid , but what I don't know.
    That's why I gave up and came here hoping it would be something starring me in the face but couldn't see.

IMN logo majestic logo threadwatch logo seochat tools logo