PHP School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a PHP Expert!

Join 300,398 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,602 people online right now. Registration is fast and FREE... Join Now!




Downloading contents of a folder

 

Downloading contents of a folder

NeekWorld

1 Jul, 2009 - 09:52 PM
Post #1

New D.I.C Head
*

Joined: 5 Oct, 2008
Posts: 49


My Contributions
i am trying to download a folder that contains images from the server. i would like to have a link/button on the page that says download folder for this user. i have a text box on the page that allows you to enter a user name and would like to be able to download all the images that that user has uploaded. i am unsure how to do it. i found a script that allows the download of zipped files. im not sure if the code i have will work.

this is the code i am thinking of using but am not sure if it will do what i want and not quite sure where to place in my code.

CODE

if (file_exists("downloads/" . $_GET['package'] . ".zip")) {
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="' . $_GET['package'] . '.zip"');
readfile("downloads/" . $_GET['package'] . ".zip");
}


im assuming that "downloads/" is the file location and i can change it to mine and $_GET['package'] i can change to my session variable.
the problem is i get an error when i change to them

error reads
Notice: Undefined index: userImages in C:\wamp\www\Site\zip.php on line 11
below is my code.
CODE

<?php
  session_start();

if (file_exists("upload_test/" . $_SESSION['userImages'] . ".zip")) {//line 11 here
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="' . $_SESSION['userImages'] . '.zip"');
readfile("upload_test/" . $_SESSION['userImages'] . ".zip");
}

exit;
?>


User is offlineProfile CardPM
+Quote Post


noorahmad

RE: Downloading Contents Of A Folder

1 Jul, 2009 - 09:57 PM
Post #2

Webmaster
Group Icon

Joined: 12 Mar, 2009
Posts: 2,018



Thanked: 125 times
Dream Kudos: 1350
My Contributions
QUOTE
Notice: Undefined index: userImages in C:\wamp\www\Site\zip.php on line 11

where is this session start from please check the spelling.
User is online!Profile CardPM
+Quote Post

NeekWorld

RE: Downloading Contents Of A Folder

1 Jul, 2009 - 10:05 PM
Post #3

New D.I.C Head
*

Joined: 5 Oct, 2008
Posts: 49


My Contributions
QUOTE(noorahmad @ 1 Jul, 2009 - 09:57 PM) *

QUOTE
Notice: Undefined index: userImages in C:\wamp\www\Site\zip.php on line 11

where is this session start from please check the spelling.


there is a session start in the main file

then the variable session is set. just curios does it have to be set before or after the form? because i have it before the form

this is where it is set. yes i changed the names so they are all the same so the zip.php file has the userName now too instead of userImages but i still get the same error
CODE
$userName = $_POST[userName];
$_SESSION['$userName'] = $userName;


this is the form
CODE
<form id="form2" name="form2" method="post" action="">
      <label>Enter Username to get images for that user
      <input type="text" name="userName" id="userName" />
      </label>
    </form>

User is offlineProfile CardPM
+Quote Post

noorahmad

RE: Downloading Contents Of A Folder

1 Jul, 2009 - 10:15 PM
Post #4

Webmaster
Group Icon

Joined: 12 Mar, 2009
Posts: 2,018



Thanked: 125 times
Dream Kudos: 1350
My Contributions
QUOTE
$userName = $_POST[userName];
$_SESSION['$userName'] = $userName;

here you are missing single quotes
php
$userName = $_POST['userName'];
$_SESSION['$userName'] = $userName;


the $_POST & $_GET are using after submitting forms!


and also this link will help you


User is online!Profile CardPM
+Quote Post

NeekWorld

RE: Downloading Contents Of A Folder

1 Jul, 2009 - 10:45 PM
Post #5

New D.I.C Head
*

Joined: 5 Oct, 2008
Posts: 49


My Contributions
QUOTE(noorahmad @ 1 Jul, 2009 - 10:15 PM) *

QUOTE
$userName = $_POST[userName];
$_SESSION['$userName'] = $userName;

here you are missing single quotes
php
$userName = $_POST['userName'];
$_SESSION['$userName'] = $userName;


the $_POST & $_GET are using after submitting forms!


and also this link will help you


ok i think you didnt understand what i ment.
when you are declaring the sessions do you do it before or after you create the form?
because im not sure it matters because ive done it before actually creating the form i sure.
User is offlineProfile CardPM
+Quote Post

noorahmad

RE: Downloading Contents Of A Folder

1 Jul, 2009 - 11:07 PM
Post #6

Webmaster
Group Icon

Joined: 12 Mar, 2009
Posts: 2,018



Thanked: 125 times
Dream Kudos: 1350
My Contributions
you can declare SESSION every where,
but if you want to assign value to form element such as Text, Radio, CheckBox etc... then you must declare it before form,
if you want to assign value from form to SESSION then you should declare it after form.
User is online!Profile CardPM
+Quote Post

RudiVisser

RE: Downloading Contents Of A Folder

1 Jul, 2009 - 11:14 PM
Post #7

.. does not guess solutions
Group Icon

Joined: 5 Jun, 2009
Posts: 1,872



Thanked: 137 times
Dream Kudos: 125
Expert In: PHP, MySQL, HTML, CSS, C#

My Contributions
QUOTE(NeekWorld @ 1 Jul, 2009 - 10:45 PM) *

ok i think you didnt understand what i ment.
when you are declaring the sessions do you do it before or after you create the form?
because im not sure it matters because ive done it before actually creating the form i sure.

noorahmad's confusing me aswell..

Anyway the reason userImages is undefined is because you're not setting it, you're setting $_SESSION['$userImages'], not 'userImages':
CODE
$userName = $_POST['userName'];
$_SESSION['$userName'] = $userName;


Just change it to be correct like this:
CODE
$userName = $_POST['userName'];
$_SESSION['userName'] = $userName;


I assume you're doing some checking before you store it like that though...
User is offlineProfile CardPM
+Quote Post

NeekWorld

RE: Downloading Contents Of A Folder

2 Jul, 2009 - 05:07 PM
Post #8

New D.I.C Head
*

Joined: 5 Oct, 2008
Posts: 49


My Contributions
QUOTE(MageUK @ 1 Jul, 2009 - 11:14 PM) *


noorahmad's confusing me aswell..

Anyway the reason userImages is undefined is because you're not setting it, you're setting $_SESSION['$userImages'], not 'userImages':
CODE
$userName = $_POST['userName'];
$_SESSION['$userName'] = $userName;


Just change it to be correct like this:
CODE
$userName = $_POST['userName'];
$_SESSION['userName'] = $userName;


I assume you're doing some checking before you store it like that though...


ok that gets rid of the error now all it displays is a blank screen. which if im understanding correctly this script should open up a dialog box that allows a user to select where to save the file. ive changed the code so many times i must have missed that line to change.
User is offlineProfile CardPM
+Quote Post

NeekWorld

RE: Downloading Contents Of A Folder

2 Jul, 2009 - 10:48 PM
Post #9

New D.I.C Head
*

Joined: 5 Oct, 2008
Posts: 49


My Contributions
ok still working on this if i get to work i will post the working code in here.

right now the code i have opens the dialog box to save the zip file and it even lets me choose the file via a text box input that i want to save. some progress.
alas this is not the last of it.
now when i do save the file it doesn't automatically add the .zip file extension.
if i add the file extension and then try to open it it says the file is corrupt, if i try to extract the data it says the file is empty.

code is as follows
this is the form from main page
CODE
<form id="form2" name="form2" method="post" action="zip3.php">
      <label>Enter Username to get images for that user<br />
      <input type="text" name="userName" id="userName" />
      </label>
          <input name="Submit2" type="submit" />
    </form>
    <?php
        $userName = $_POST['userName'];
        $_SESSION['userName'] = $userName;
    ?>


zip3.php code
CODE
<?php
    $filename = "upload_test/".$_POST['userName'];
    $buffer = file_get_contents($filename);

    /* Force download dialog... */
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");

    /* Don't allow caching... */
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

    /* Set data type, size and filename */
    header("Content-Type: application/octet-stream");
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: " . strlen($buffer));
    header("Content-Disposition: attachment; filename=$filename");

    /* Send our file... */
    echo $buffer;
?>

User is offlineProfile CardPM
+Quote Post

NeekWorld

RE: Downloading Contents Of A Folder

3 Jul, 2009 - 12:30 AM
Post #10

New D.I.C Head
*

Joined: 5 Oct, 2008
Posts: 49


My Contributions
ok so i figured out how to fix the .zip problem. if i change this line

CODE
  header("Content-Disposition: attachment; filename=$filename");


to this
CODE
    header("Content-Disposition: attachment; filename=$filename.zip");


it saves it automatically as a zip.

as for the other problem it still exists. i think it is because there is no code in there to actually add the files in that folder to the zip folder but i dont know how that is supposed to be worded or where to put the code.

User is offlineProfile CardPM
+Quote Post

NeekWorld

RE: Downloading Contents Of A Folder

3 Jul, 2009 - 11:22 AM
Post #11

New D.I.C Head
*

Joined: 5 Oct, 2008
Posts: 49


My Contributions
going through the code i understand that this part is only to force the dialog box to open
CODE
$buffer = file_get_contents($filename);

/* Force download dialog... */
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");

    /* Don't allow caching... */
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

    /* Set data type, size and filename */
    header("Content-Type: application/octet-stream");
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: " . strlen($buffer));
    header("Content-Disposition: attachment; filename=$filename.zip");

    /* Send our file... */
    echo $buffer;


so this part in theory should put the files into the zip folder right?
CODE
$files_to_zip = array(
    'upload_test/2009-07-01-214005_Logo.jpg',
    'upload_test/2009-07-01-215314orange_bg.gif',
    'upload_test/2009-07-01-215326emptycart.gif',
    'upload_test/2009-07-01-223317dash.gif',
    'upload_test/2009-07-02-020536arrow.gif',
    'upload_test/2009-07-02-020717check_green.gif'
);
//if true, good; if false, zip creation failed
$result = create_zip($files_to_zip,'my-archive.zip');

/* creates a compressed zip file */
function create_zip($files = array(),$destination = '',$overwrite = false) {
    //if the zip file already exists and overwrite is false, return false
    if(file_exists($destination) && !$overwrite) { return false; }
    //vars
    $valid_files = array();
    //if files were passed in...
    if(is_array($files)) {
        //cycle through each file
        foreach($files as $file) {
            //make sure the file exists
            if(file_exists($file)) {
                $valid_files[] = $file;
            }
        }
    }
    //if we have good files...
    if(count($valid_files)) {
        //create the archive
        $zip = new ZipArchive();
        if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
            return false;
        }
        //add the files
        foreach($valid_files as $file) {
            $zip->addFile($file,$file);
        }
        //debug
        //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
        
        //close the zip -- done!
        $zip->close();
        
        //check to make sure the file exists
        return file_exists($destination);
    }
    else
    {
        return false;
    }
}


now i know the images are hard coded in there which is not what i want to do. but this gives me the same error as the first one. does anyone know what is wrong with the code. and possibly if they know how to put the files into the array dynamically?

This post has been edited by NeekWorld: 3 Jul, 2009 - 11:33 AM
User is offlineProfile CardPM
+Quote Post

NeekWorld

RE: Downloading Contents Of A Folder

3 Jul, 2009 - 12:44 PM
Post #12

New D.I.C Head
*

Joined: 5 Oct, 2008
Posts: 49


My Contributions
ok i guess im going to answer my own questions oh well. well i did actually get it to work sorta.
this code is doing exactly what i want with two exceptions. it is saving the files into the current directory and it has the images hard coded into the array. does anyone know how to set it up so i can input a name and have that name be the folder with the files in it to zip?

it also pops up a dialog box which is what i want and saving a file where i want but that file is empty.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 10:12PM

Live PHP Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month