File Download

/* download.php */

<?php

if (isset($_GET['file_name'])) {
    $filename = $_GET['file_name'];
} else {
    $filename = NULL;
}

$err = "Sorry, the file you are requesting is unavailable";

if (!$filename) {
    echo $err;
} else {
    $path = 'uploads/' . $filename;
    if (file_exists($path) && is_readable($path)) {
        $size = filesize($path);
        header('Content-Type: application/octet-stream');
        header('Content-Length: ' . $size);
        header('Content-Disposition: attachment; filename=' . $filename);
        header('Content-Transfer-Encoding: binary');

        $file = @fopen($path, 'rb');
        if ($file) {
            fpassthru($file);
            exit;
        } else {
            echo $err;
        }
    }
}
?>

Advertisements

File upload & Delete

<?php
$conn = mysqli_connect("localhost", "root", "", "fresh");

/* This is for delete file */
/*
if (isset($_REQUEST['file_name'])) {
    $filename = $_REQUEST['file_name'];
    $query = mysqli_query($conn, "DELETE FROM user WHERE profile_image='$filename'");
    if (mysqli_affected_rows($conn) > 0) {
        @ unlink("uploads/$filename");
        echo "Your file succefully delete";
    } else {
        echo "Your file not delete";
    }
}
 * */



/* file upload from this here */
if (isset($_FILES['filefield'])) {

    $name = $_REQUEST['name'];
    $file = $_FILES['filefield'];
    //echo "<pre>";
    //print_r($file);

    $upload_directory = "uploads/";

    $allowed_extensions = array("gif", "jpg", "jpeg", "mp3", "mp4", "tiff", "bmp", "doc", "docx", "ppt", "pptx", "txt", "pdf");
    $ext = substr($file['name'], strrpos($file['name'], '.') + 1);
    if (!in_array($ext, $allowed_extensions)) {
        die('only "gif", "jpg", "jpeg", "mp3", "tiff", "bmp", "doc", "docx", "ppt", "pptx", "txt", "pdf" files allowed to upload');
    }

    $max_file_size = 10485760;
    if ($file['size'] >= $max_file_size) {
        echo "Only the file size less than $max_file_size mb allowed";
    }

    $path = md5(microtime()) . '.' . $ext;

    if (move_uploaded_file($file['tmp_name'], $upload_directory . $path)) {

        // This query file name insert in to the database
        $query = mysqli_query($conn, "INSERT INTO user SET name='$name', profile_image='$path' ");

        //if (move_uploaded_file($file['tmp_name'], $upload_directory . $file['name'])) {
        echo "Your file successfully uploaded";
    } else {
        echo "The file can't moved";
    }
}
?>
/* file upload finished */

<form action="" method="post" enctype="multipart/form-data">
    <label>Name
        <input id="name" type="input" name="name" />
    </label>

    <label>Upload File
        <input id="filefield" type="file" name="filefield" />
    </label>

    <label>
        <input id="Upload" type="submit" name="Upload" value="Submit" />
    </label>



    <!-- This part is file name fetch from database and show up table -->

    <table width="700" border="1">
        <tr>
            <td>Name</td>
            <td>File Name</td>
            <td>Image</td>
        </tr>

        <?php
        /*
        $query = mysqli_query($conn, "SELECT name, profile_image FROM user");
        $hold = mysqli_fetch_all($query, MYSQLI_ASSOC);
        //echo "<pre>";
        //print_r($show);
        foreach ($hold as $data) {
            extract($data);
            ?>     
            <tr>
                <td><?php echo $name; ?></td>
                <td><?php echo $profile_image; ?></td>
                <!--<td><img src="uploads/<?php //echo $profile_image;?> "/></td>-->
                <td>
                    <?php
                    $file_ext = substr($profile_image, strrpos($profile_image, '.') + 1);
                    if ($file_ext == 'jpg') {
                        ?>
                        <img src = "uploads/<?php echo $profile_image; ?>" width = "100" />
                        <?php
                    }

                    if ($file_ext == 'pdf') {
                        ?>
                        <a href="uploads/<?php echo $profile_image; ?>"><img src = "uploads/pdf.jpg" width = "100" /></a>
                        <?php
                    }
                    if ($file_ext == 'docx') {
                        ?>
                        <a href="uploads/<?php echo $profile_image; ?>"><img src = "uploads/docx.jpg" width = "100" /></a>
                        <?php
                    }
                    ?>
                    <a href="download.php?file_name=<?php echo $profile_image; ?>"><input type="button" value="Download" name="download"></a>
                    <a href="upload.php?file_name=<?php echo $profile_image; ?>"><input type="button" value="Delete" name="delete" /></a>     
                </td>

            </tr>
            <?php
        }
        
        */
        ?>
    </table>

</form>