SitePoint Sponsor

User Tag List

Page 2 of 2 FirstFirst 12
Results 26 to 47 of 47
  1. #26
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,222
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Actually that was a very good first attempt (nearly nailed it!).

    First, on your download.php
    PHP Code:
    header("Content-disposition: attachment; filename= $blob"); 
    This line $blob should be the name of the file being requested (more on that later).

    Currently, you are calling download.php, but you are not providing the file that needs to be downloaded. Change it to this:
    HTML Code:
                           <li><?php echo $row['name']; ?></li> 
                         <a href="download.php?name=<?php echo urlencode($row['name']); ?>"> Download file</a>
    Then in your download.php you need to read that filename to use in your query (place this above your query).
    PHP Code:
    $nameOfFile urldecode($_GET['name']); 
    Then update your query to use the new variable
    PHP Code:
    $filequery("SELECT * FROM files where studentid= ? AND name = ?"$_SESSION['id'], $nameOfFile); 
    Last, use that same variable for the filename=
    PHP Code:
    header("Content-disposition: attachment; filename=$nameOfFile"); 

  2. #27
    SitePoint Zealot
    Join Date
    Feb 2013
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i did those changes, and it got a bit better, it now downloads the an html page for the file, and when i open the html page, i get the following errors...
    at first i was getting these errors like undefined index file and undefined index mimetype in download.php, so i changed my code thus:

    Code:
    $nameOfFile = urldecode($_GET['name']); 
    $file= query("SELECT * FROM files where studentid= ? AND name = ?", $_SESSION['id'], $nameOfFile);
    $blob = $file['0']['file'];            //////////////////////////////////THIS WAS  $file['file'] at first
    $mimetype = $file['0']['mimetype'];          ////////////////////////////// THIS TOO
    
    header("Content-disposition: attachment; filename=$nameOfFile"); 
    header("Content-type: $mimetype");
    readfile("$blob");
    ?>
    and then i tried dloading again and still got an html file, capsule4.html (capsule4.png is the file i tried downlaoding) and these errors...

    Undefined offset: 0 in /vhosts/localhost/html/download.php on line 25
    Undefined offset: 0 in /vhosts/localhost/html/download.php on line 26

    readfile(): Filename cannot be empty in /vhosts/localhost/html/download.php on line 30


    line 25 $blob = $file['0']['file'];
    line 26 $mimetype = $file['0']['mimetype'];

    line 30 readfile("$nameOfFile"); ////////i changed to this from readfile("$blob"); but still got a third error...

  3. #28
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,222
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Ah, I missed that.

    PHP Code:
    $nameOfFile urldecode($_GET['name']); 
    $filequery("SELECT * FROM files where studentid= ? AND name = ?"$_SESSION['id'], $nameOfFile);
    $blob $file[0]['file'];            //remove the quotes from the [0]
    $mimetype $file[0]['mimetype'];  //remove the quotes from the [0]

    header("Content-disposition: attachment; filename=$nameOfFile"); 
    header("Content-type: $mimetype");
    echo 
    $blob// Use echo, not readfile or file_get_contents, as you have the binary already stored
    ?> 

  4. #29
    SitePoint Zealot
    Join Date
    Feb 2013
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    got that, but it's still giving a .html file with the error Undefined offset: 0 at line 25 and 26

  5. #30
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,222
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Okay, time to do some debugging - add the var_dump line
    PHP Code:
    $nameOfFile urldecode($_GET['name']); 
    $filequery("SELECT * FROM files where studentid= ? AND name = ?"$_SESSION['id'], $nameOfFile);
    var_dump($file$nameOfFile$_SESSION['id']); die(); // add this line!
    $blob $file[0]['file'];            //remove the quotes from the [0]
    $mimetype $file[0]['mimetype'];  //remove the quotes from the [0]

    header("Content-disposition: attachment; filename=$nameOfFile"); 
    header("Content-type: $mimetype");
    echo 
    $blob// Use echo, not readfile or file_get_contents, as you have the binary already stored
    ?> 

  6. #31
    SitePoint Zealot
    Join Date
    Feb 2013
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lol...sherlock, got this....


    array (size=0)
    empty
    string 'CA' (length=2)
    int 46

  7. #32
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,222
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Okay, so your query is returning 0 records. It is searching for studentid = 46 and name = 'CA'. I somehow, doubt that is the name of your file in the table for studentid 46.

    Can you provide me with the actual name of your file?
    Also, when you are on the dloadfile.php page, right-click, perform a view-source and copy and paste the HTML here. I want to see exactly what was built using the logic I helped you create for that page.

  8. #33
    SitePoint Zealot
    Join Date
    Feb 2013
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is what i've got on the dloadfile.php excluding the headers...


    Code:
    <h1> <strong> DOWNLOAD FILES </strong></h1>
     
    
    <form action="dloadfile.php" method="post" enctype="multipart/form-data">
        <fieldset>
          <div class="control-group">
                                        
                          <li>CA</li> 
                         <a href="download.php?name=CA"> Download file</a>
                              
                                              
                          <li>CAPSUEL4</li> 
                         <a href="download.php?name=CAPSUEL4"> Download file</a>
                              
                                              
                          <li>capsule</li> 
                         <a href="download.php?name=capsule"> Download file</a>
                              
                                              
                          <li>construction</li> 
                         <a href="download.php?name=construction"> Download file</a>
                              
                                              
                          <li>glyphicons</li> 
                         <a href="download.php?name=glyphicons"> Download file</a>
                              
                                              
                          <li>logo3</li> 
                         <a href="download.php?name=logo3"> Download file</a>
                              
                          
            </div>
        </fieldset>
    </form>

    and actually my table looks thus...CAP.JPG and the name of the actual file was capsule2.png and in my upload page, i gave it the name CA

  9. #34
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,222
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Okay, so the studentid is 0 in the table, but the script is trying to use 46. Are you sure you are inserting the correct studentid into the table?

    In your upload_form.php

    There is a line similar to this:
    PHP Code:
        $upload=query("INSERT INTO files (mimetype, file, name) VALUES (?, ?, ?)"$mimetypefile_get_contents($fileuploaded), $_POST['filename']); 
    It needs to have studentid in it
    PHP Code:
        $upload=query("INSERT INTO files (studentid, mimetype, file, name) VALUES (?, ?, ?)"$_SESSION['id'], $mimetypefile_get_contents($fileuploaded), $_POST['filename']); 
    Also, if you provide the upload controller and template, there may be other changes needed to make this work properly.

  10. #35
    SitePoint Zealot
    Join Date
    Feb 2013
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well, i tried downloading with that changed, and i'm now getting the actual file instead of an html , but the images dont seem to download properly..for eg.gif image is truncated,

    my upload.php file is thus
    Code:
    <?php
      
       // display errors, warnings, and notices
        ini_set("display_errors", true);
        error_reporting(E_ALL);
    
        // requirements
        require("../includes/constants.php"); 
        require("../includes/functions.php");
       
     
        // enable sessions
         session_start();
         
             // require authentication for most pages
        if (!preg_match("{(?:login|logout|register)\.php$}", $_SERVER["PHP_SELF"]))
        {
            if (empty($_SESSION["id"]))
            {
                redirect("login.php");
            }
        }
        
        render("upload_form.php", ["title" => "Upload your stuff"]);
        
        
        if ($_SERVER["REQUEST_METHOD"] == "POST")
        {
          if(isset($_FILES))
          {
        $fileuploaded = $_FILES['upload_file']['tmp_name'];
        $mimetype = mime_content_type($fileuploaded);
        
       $upload=query("INSERT INTO files (studentid, mimetype, file, name) VALUES (?, ?, ?, ?)", $_SESSION['id'], 
       $mimetype, file_get_contents($fileuploaded), $_POST['filename']);
        
          }
          else
          aplogize("Could not upload file");
        }
        
        
        else
        render("upload_form.php", ["title" => "Upload your stuff"]);
        
    ?>


    and upload_form.php is

    Code:
    <a class="btn-large btn-custom" href="dloadfile.php"><i class="icon-truck"></i>  Download</a>
    <h1> <strong> UPLOAD YOUR FILES </strong></h1>
     
    
    <form action="upload.php" method="post" enctype="multipart/form-data">
        <fieldset>
           <div class="control-group">
                <input autofocus name="filename" placeholder="Enter file name" type="text"/>
            </div>
            <div class="control-group">
            <input type="file" name="upload_file" />
            </div>
            <div class="control-group">
                <button type="submit" class="btn-large btn-custom">Upload</button>
            </div>
        </fieldset>
    </form>

  11. #36
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,222
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    What do you mean by it being truncated? The filename or the physical file?

  12. #37
    SitePoint Zealot
    Join Date
    Feb 2013
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the physical file.... and i got another error for jpeg, the file downloaded but when i open it, it shows an error "ERROR INTERPRATING JPEG IMAGE FILE (APPLICATION TRANSFERED TOO FEW SCANLINES) and for gif it showed the error like .gif image is truncated....

  13. #38
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,222
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Okay, so maybe file_get_contents was a bad suggestion, instead try this:

    Replace:
    PHP Code:
        $fileuploaded $_FILES['upload_file']['tmp_name'];
        
    $mimetype mime_content_type($fileuploaded);
        
       
    $upload=query("INSERT INTO files (studentid, mimetype, file, name) VALUES (?, ?, ?, ?)"$_SESSION['id'], 
       
    $mimetypefile_get_contents($fileuploaded), $_POST['filename']); 
    With:
    PHP Code:
        $fileuploaded $_FILES['upload_file']['tmp_name'];
        
    $mimetype mime_content_type($fileuploaded);
        
    $handler fopen($fileuploaded'r');
        
    $content fread($handlerfilesize($fileuploaded));
        
    fclose($handler);
        
       
    $upload=query("INSERT INTO files (studentid, mimetype, file, name) VALUES (?, ?, ?, ?)"$_SESSION['id'], 
       
    $mimetype$content$_POST['filename']); 
    Also you really don't need the user to provide the filename, you can read it from $_FILES['upload_file']['name']

  14. #39
    SitePoint Zealot
    Join Date
    Feb 2013
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it;s still showing the same error... but it seems to be working alright for tiny files, i tried uploading a gif file of about 12kb, and it downloaded fine...

  15. #40
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,222
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Well there are a couple of things we could do that may solve that, 1) store the file size in the table so you can use Content-Length: $filesize in a header telling the browser how much data to expect, or 2) store the file in a physical folder on the server, and store its path in the table instead of the contents of the file, and then use readfile to output the physical file when the download request is received.

    I think either scenario will work and I know the second scenario will always work, as the physical file will exist and can be feed back to the user, it isn't stored in a DB as bytes needing to be re-formed into a physical file.

  16. #41
    SitePoint Zealot
    Join Date
    Feb 2013
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nice, i also used to think at first that files were located somewhere safe on the servers instead of being broken into bits when uploaded, how can we go about this??

  17. #42
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,222
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Okay, first you need to identify where you want to store the files (they need to be accessible to your web user account, but not necessarily web accessible)
    So let's assume your web accessible folder is /home/myuser/public_html/, then you would want to store your files in /home/myuser/uploadedFiles/ (a new folder at the same level as the web accessible folder)
    Then you want to give it the appropriate permissions, either changing the ownership to the user apache is running under or giving others write access using chmod 0777
    Drop the content column of your files table

    Then you need to update your upload.php
    PHP Code:
        $fileuploaded $_FILES['upload_file']['tmp_name'];
        
    $mimetype mime_content_type($fileuploaded);

        
    $storageLocation '/home/myuser/uploadedFiles/'// I recommend placing this line in your configuration file so it isn't defined everywhere you need it.
        
    if (!is_dir($storageLocation $_SESSION['id']))
          
    mkdir($storageLocation $_SESSION['id']);

       
    $fileName $_FILES['upload_file']['name'];
       
    move_uploaded_file($_FILES['upload_file']['tmp_name'], $storageLocation $_SESSION['id'] . '/' $fileName);
        
       
    $upload=query("INSERT INTO files (studentid, mimetype, name) VALUES (?, ?, ?)"$_SESSION['id'], $mimetypefileName); 
    Then in your download.php
    PHP Code:
    $nameOfFile urldecode($_GET['name']); 
    $filequery("SELECT * FROM files where studentid= ? AND name = ?"$_SESSION['id'], $nameOfFile);
    $mimetype $file[0]['mimetype'];  //remove the quotes from the [0]

    $storageLocation '/home/myuser/uploadedFiles/'// I recommend placing this line in your configuration file so it isn't defined everywhere you need it.

    $fileLocation $storageLocation $_SESSION['id'] . '/' $nameOfFile;
    if (
    is_file($fileLocation))
    {
      
    header("Content-disposition: attachment; filename=$nameOfFile"); 
      
    header("Content-type: $mimetype");
      
    readfile($fileLocation);
    }
    else
    {
      
    render('filenotfound.php'); // you need to create this so it tells the user, the file wasn't found.
    }
    ?> 

  18. #43
    SitePoint Zealot
    Join Date
    Feb 2013
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i got all that, but when i open my upload.php it gives this error Parse error: syntax error, unexpected 'move_uploaded_file' (T_STRING) for this line

    move_uploaded_file($_FILES['upload_file']['tmp_name'], $storageLocation . $_SESSION['id'] . '/' . $fileName);


    Drop the content column of your files table
    you mean the blob column?

  19. #44
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,222
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jay93 View Post
    i got all that, but when i open my upload.php it gives this error Parse error: syntax error, unexpected 'move_uploaded_file' (T_STRING) for this line

    move_uploaded_file($_FILES['upload_file']['tmp_name'], $storageLocation . $_SESSION['id'] . '/' . $fileName);
    Ah, I missed the semi-colon on the live above move_uploaded_file *sigh*


    Quote Originally Posted by jay93 View Post
    you mean the blob column?
    Yes

  20. #45
    SitePoint Zealot
    Join Date
    Feb 2013
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oops, i didn't notice that too... it works perfect now... i dont know how i can thank you enough... now that everything is sorted, i would be very grateful if you could explain just a little what some few lines in the code did, (i understood most of the simpler parts)


    for instance ; 1. move_uploaded_file($_FILES['upload_file']['tmp_name'], $storageLocation . $_SESSION['id'] . '/' . $fileName); i guess this is moving the file to the directory, but what is the role of the '/' in there and the session id??

    2.if (is_file($fileLocation))
    {
    header("Content-disposition: attachment; filename=$nameOfFile");
    header("Content-type: $mimetype");
    readfile($fileLocation);
    }

    and what are "header"s and content-disposition:attachment all about?? thanks budd..

  21. #46
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,222
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jay93 View Post
    1. move_uploaded_file($_FILES['upload_file']['tmp_name'], $storageLocation . $_SESSION['id'] . '/' . $fileName); i guess this is moving the file to the directory, but what is the role of the '/' in there and the session id??
    Certainly. The SESSION['id'] is in there so we can store each user's documents/files in a separate folder. At some point each student will likely upload a file with the same name as another student. To prevent the conflict, storing each user's documents into their own folder (designated by SESSION['id']) prevents the conflict.

    So the first thing it does is check if there is a directory with the name of the SESSION['id']. if there isn't, it creates it, then it moves the file to that directory (hence, the '/', as $_SESSION['id'] won't contain that character, so I append it to denote it is a directory).

    Quote Originally Posted by jay93 View Post
    2.if (is_file($fileLocation))
    {
    header("Content-disposition: attachment; filename=$nameOfFile");
    header("Content-type: $mimetype");
    readfile($fileLocation);
    }

    and what are "header"s and content-disposition:attachment all about?? thanks budd..
    To force a download of a file, you can use a variety of techniques, but the most common is Content-disposition: attachment followed by the name of the file you want the download to have. Hopefully that makes sense.

  22. #47
    SitePoint Zealot
    Join Date
    Feb 2013
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah, it looks much less like rocket science now, lol... thanks "Matt"... ( i read some of your blog) ... ;-) it was great...


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
  •