Download file from database path (given upload scripts)

Discussion in 'HTML & Website Design' started by PhpNOvice, May 17, 2014.

  1. #1
    Good Day Forums,

    I have manage to make a script to upload file in database and its working. I will share to you the codes so for other viewers and readers to use also.

    if($result){
                if($_FILES['LRCard']['name'] != ""){
                    $filename = $_FILES['LRCard']['name'];
                    $ext = strrchr($filename,".");
                    $LRCardname = $student_id;
                    $LRCardname .="_". $filename; 
                    if($ext ==".jpg" || $ext ==".jpeg" || $ext ==".JPG" || $ext ==".JPEG" || $ext ==".gif" || $ext ==".GIF"){
                        $size = $_FILES['LRCard']['size'];
                        if($size > 0 && $size < 5000000){
                            $archive_dir = "LRCards";
                            $userfile_tmp_name = $_FILES['LRCard']['tmp_name'];
                            if(move_uploaded_file($userfile_tmp_name, "$archive_dir/$LRCardname")){
                                /*
                                    if LRC is successfully uploaded then LRC is stored in database.
                                */
                                mysql_query("update student_information set LRCard='$LRCardname' where student_id='$student_id'", $link_id); 
                                $flag = "success"; 
                                if(mysql_error()!=null){
                                    die(mysql_error());
                                }
     
                            }
                            else{
                                if(file_exists('LRCard/' . $LRCardname)) {
                                    unlink('LRCards/' . $LRCardname); 
                                }
                                rollbackData();
                            }
                        }
                        else{
                            if(file_exists('LRCards/' . $LRCardname)) {
                                unlink('LRCard/' . $LRCardname); 
                            }
                            rollbackData();
                            die("You can upload LRCard of 5 MB size only. Please, try again.");
                        }
                    }
                    else{
                        if(file_exists('LRCards/' . $LRCardname)) {
                            unlink('LRCards/' . $LRCardname); 
                        }
                        rollbackData();
                        die("You can upload LRCard of .jpg, .jpeg, .gif extensions only. Please, try again. ");
                    }
                }     
            }
            else{
                $flag="error";
            }
            if($flag == "success"){
                mysql_query(" COMMIT ");
                $flag="success";
                if(mysql_error() != null){
                    die(mysql_error());
                }
            }  
    Code (markup):
    Now, my problem is how to make a Php script to DOWNLOAD this uploaded file considering the
    1. file path
    2. file name
    3. file extension.
    4. student_id (to determine the specific file from a specific student)

    The images are stored in the

    the-http://localhost/a/LRCards/

    My plan is to make a download button which hyperlink to download.php and after clicking that button the specified file to a specific student id will automatically be downloaded by the browser.

    Please advise..
     
    PhpNOvice, May 17, 2014 IP