Jump to content

Recommended Posts

I am somewhat beginner, I am using PHP 5.1.1, according to php.net i need to activate the php_zip library which i have already done.

Oh btw i am using windows, wamp5 server.

 

Here is my problem.

 

I have lots of music files, therefore i would like to create a filelist using php which will allow users to browse through folders & files (not streaming the files jus downloading only) but i would like them to mix and match the files that they wan n zip it n then download them.

 

Can anyone help me with the codes (in details coz i really trying to learn here)?

or does any codes like this already exist.. pls specify whr i can download them.

 

Thank you...

Link to comment
https://forums.phpfreaks.com/topic/71199-zip-help-pls/
Share on other sites


<?php
//----
//Personal Programmers Assistant
//Created by Joyel Puryear (http://www.freelancebusinessman.com
//----
//Copyright (c) 2006, "Freelance Businessman Network"
//All rights reserved.
//License Location: /ppassistant/userdocumentation/license.txt
//----
// file functions

// Function
// Purpose: Get File Extensions
// Parameter: Takes the full name of a file (file.ext).  it strips
// out the extension and leaves the .ext part.

function getext($file) {
  $pos = strrpos($file, '.');
  if(!$pos) {
    return FALSE;
  }else {
  	$str = substr($file, $pos, strlen($file));
  	return $str;
  }
}

// Function
// Purpose: Checks an extension for type
// Parameter: You create an array of the extensions you want
// to be approved, then you put them in array with the .
// example .gif or .jpg.  The array can be as many as you want
// then you call the function and put the returned value from
// getext() and the array.  It returns true if it was on the
// allowed list, and false if it was not (allowing you to
// populate your error handler, or however you want to act on it.)

function testext($ext, $allowedextensions) {
if (!is_array($allowedextensions)) {
	return "2nd parameter has to be an array";
}else {
	foreach($allowedextensions as $k => $v) {
		if ($ext == $v) {
			$success = TRUE;
			return TRUE;
		}
	}
	if ($success !== TRUE) {
		return FALSE;
	}
}
}

# Some below are from code igniter
/*
Instructions
File Helper

The File Helper file contains functions that assist in working with files.
Loading this Helper

This helper is loaded using the following code:
$this->load->helper('file');

The following functions are available:
read_file('path')

Returns the data contained in the file specified in the path. Example:
$string = read_file('./path/to/file.php');

The path can be a relative or full server path. Returns FALSE (boolean) on failure.

Note: The path is relative to your main site index.php file, NOT your controller or view files. Code Igniter uses a front controller so paths are always relative to the main site index.

If you server is running an open_basedir restriction this function might not work if you are trying to access a file above the calling script.
write_file('path', $data)

Writes data to the file specified in the path. If the file does not exist the function will create it. Example:
$data = 'Some file data';

if ( ! write_file('./path/to/file.php', $data))
{
     echo 'Unable to write the file';
}
else
{
     echo 'File written!';
}

You can optionally set the write mode via the third parameter: write_file('./path/to/file.php', $data, 'r+');

The default mode is wb. Please see the PHP user guide for mode options.

Note: In order for this function to write data to a file its file permissions must be set such that it is writable (666, 777, etc.). If the file does not already exist, the directory containing it must be writable.

Note: The path is relative to your main site index.php file, NOT your controller or view files. Code Igniter uses a front controller so paths are always relative to the main site index.
delete_files('path')

Deletes ALL files contained in the supplied path. Example:
delete_files('/path/to/directory/');

If the second parameter is set to true, any directories contained within the supplied root path will be deleted as well. Example:
delete_files('/path/to/directory/', TRUE);

Note: The files must be writable or owned by the system in order to be deleted.
get_filenames('path/to/directory/')

Takes a server path as input and returns an array containing the names of all files contained within it. The file path can optionally be added to the file names by setting the second parameter to TRUE.

Download Helper

The Download Helper lets you download data to your desktop.
Loading this Helper

This helper is loaded using the following code:
$this->load->helper('download');

The following functions are available:
force_download('filename', 'data')

Generates server headers which force data to be downloaded to your desktop. Useful with file downloads. The first parameter is the name you want the downloaded file to be named, the second parameter is the file data. Example:
$data = 'Here is some text!';
$name = 'mytext.txt';

force_download($name, $data);

If you want to download an existing file from your server you'll need to read the file into a string:
$data = file_get_contents("/path/to/photo.jpg"); // Read the file's contents
$name = 'myphoto.jpg';

force_download($name, $data);
*/
/**
* Read File
*
* Opens the file specfied in the path and returns it as a string.
*
* @access	public
* @param	string	path to file
* @return	string
*/	
function read_file($file)
{
if ( ! file_exists($file))
{
	return FALSE;
}

if (function_exists('file_get_contents'))
{
	return file_get_contents($file);		
}

if ( ! $fp = @fopen($file, 'rb'))
{
	return FALSE;
}

flock($fp, LOCK_SH);

$data = '';
if (filesize($file) > 0)
{
	$data =& fread($fp, filesize($file));
}

flock($fp, LOCK_UN);
fclose($fp);

return $data;
}

// ------------------------------------------------------------------------

/**
* Write File
*
* Writes data to the file specified in the path.
* Creates a new file if non-existent.
*
* @access	public
* @param	string	path to file
* @param	string	file data
* @return	bool
*/	
function write_file($path, $data, $mode = 'wb')
{
if ( ! $fp = @fopen($path, $mode))
{
	return FALSE;
}

flock($fp, LOCK_EX);
fwrite($fp, $data);
flock($fp, LOCK_UN);
fclose($fp);	

return TRUE;
}

// ------------------------------------------------------------------------

/**
* Delete Files
*
* Deletes all files contained in the supplied directory path.
* Files must be writable or owned by the system in order to be deleted.
* If the second parameter is set to TRUE, any directories contained
* within the supplied base directory will be nuked as well.
*
* @access	public
* @param	string	path to file
* @param	bool	whether to delete any directories found in the path
* @return	bool
*/	
function delete_files($path, $del_dir = FALSE, $level = 0)
{	
// Trim the trailing slash
$path = preg_replace("|^(.+?)/*$|", "\\1", $path);

if ( ! $current_dir = @opendir($path))
	return;

while(FALSE !== ($filename = @readdir($current_dir)))
{
	if ($filename != "." and $filename != "..")
	{
		if (is_dir($path.'/'.$filename))
		{
			$level++;
			delete_files($path.'/'.$filename, $del_dir, $level);
		}
		else
		{
			unlink($path.'/'.$filename);
		}
	}
}
@closedir($current_dir);

if ($del_dir == TRUE AND $level > 0)
{
	@rmdir($path);
}
}

// ------------------------------------------------------------------------

/**
* Get Filenames
*
* Reads the specified directory and builds an array containing the filenames.  
* Any sub-folders contained within the specified path are read as well.
*
* @access	public
* @param	string	path to source
* @param	bool	whether to include the path as part of the filename
* @return	array
*/	
function get_filenames($source_dir, $include_path = FALSE)
{
static $_filedata = array();

if ($fp = @opendir($source_dir))
{
	while (FALSE !== ($file = readdir($fp)))
	{
		if (@is_dir($source_dir.$file) && substr($file, 0, 1) != '.')
		{
			 get_filenames($source_dir.$file."/", $include_path);
		}
		elseif (substr($file, 0, 1) != ".")
		{

			$_filedata[] = ($include_path == TRUE) ? $source_dir.$file : $file;
		}
	}
	return $_filedata;
}
}


/**
* Force Download
*
* Generates headers that force a download to happen
*
* @access	public
* @param	string	filename
* @param	mixed	the data to be downloaded
* @return	void
*/	
function force_download($filename = '', $data = '')
{
if ($filename == '' OR $data == '')
{
	return FALSE;
}

// Try to determine if the filename includes a file extension.
// We need it in order to set the MIME type
if (FALSE === strpos($filename, '.'))
{
	return FALSE;
}

// Grab the file extension
$x = explode('.', $filename);
$extension = end($x);

// Load the mime types
@include(APPPATH.'config/mimes'.EXT);

// Set a default mime if we can't find it
if ( ! isset($mimes[$extension]))
{
	$mime = 'application/octet-stream';
}
else
{
	$mime = (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];
}

// Generate the server headers
if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE"))
{
	header('Content-Type: "'.$mime.'"');
	header('Content-Disposition: attachment; filename="'.$filename.'"');
	header('Expires: 0');
	header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
	header("Content-Transfer-Encoding: binary");
	header('Pragma: public');
	header("Content-Length: ".strlen($data));
}
else
{
	header('Content-Type: "'.$mime.'"');
	header('Content-Disposition: attachment; filename="'.$filename.'"');
	header("Content-Transfer-Encoding: binary");
	header('Expires: 0');
	header('Pragma: no-cache');
	header("Content-Length: ".strlen($data));
}

echo $data;
}


?>
<?php
//----
//Personal Programmers Assistant
//Created by Joyel Puryear (http://www.freelancebusinessman.com
//----
//Copyright (c) 2006, "Freelance Businessman Network"
//All rights reserved.
//License Location: /ppassistant/userdocumentation/license.txt
//----
// file functions

// Function
// Purpose: Get File Extensions
// Parameter: Takes the full name of a file (file.ext).  it strips
// out the extension and leaves the .ext part.

function getext($file) {
  $pos = strrpos($file, '.');
  if(!$pos) {
    return FALSE;
  }else {
  	$str = substr($file, $pos, strlen($file));
  	return $str;
  }
}

// Function
// Purpose: Checks an extension for type
// Parameter: You create an array of the extensions you want
// to be approved, then you put them in array with the .
// example .gif or .jpg.  The array can be as many as you want
// then you call the function and put the returned value from
// getext() and the array.  It returns true if it was on the
// allowed list, and false if it was not (allowing you to
// populate your error handler, or however you want to act on it.)

function testext($ext, $allowedextensions) {
if (!is_array($allowedextensions)) {
	return "2nd parameter has to be an array";
}else {
	foreach($allowedextensions as $k => $v) {
		if ($ext == $v) {
			$success = TRUE;
			return TRUE;
		}
	}
	if ($success !== TRUE) {
		return FALSE;
	}
}
}

# Some below are from code igniter
/*
Instructions
File Helper

The File Helper file contains functions that assist in working with files.
Loading this Helper

This helper is loaded using the following code:
$this->load->helper('file');

The following functions are available:
read_file('path')

Returns the data contained in the file specified in the path. Example:
$string = read_file('./path/to/file.php');

The path can be a relative or full server path. Returns FALSE (boolean) on failure.

Note: The path is relative to your main site index.php file, NOT your controller or view files. Code Igniter uses a front controller so paths are always relative to the main site index.

If you server is running an open_basedir restriction this function might not work if you are trying to access a file above the calling script.
write_file('path', $data)

Writes data to the file specified in the path. If the file does not exist the function will create it. Example:
$data = 'Some file data';

if ( ! write_file('./path/to/file.php', $data))
{
     echo 'Unable to write the file';
}
else
{
     echo 'File written!';
}

You can optionally set the write mode via the third parameter: write_file('./path/to/file.php', $data, 'r+');

The default mode is wb. Please see the PHP user guide for mode options.

Note: In order for this function to write data to a file its file permissions must be set such that it is writable (666, 777, etc.). If the file does not already exist, the directory containing it must be writable.

Note: The path is relative to your main site index.php file, NOT your controller or view files. Code Igniter uses a front controller so paths are always relative to the main site index.
delete_files('path')

Deletes ALL files contained in the supplied path. Example:
delete_files('/path/to/directory/');

If the second parameter is set to true, any directories contained within the supplied root path will be deleted as well. Example:
delete_files('/path/to/directory/', TRUE);

Note: The files must be writable or owned by the system in order to be deleted.
get_filenames('path/to/directory/')

Takes a server path as input and returns an array containing the names of all files contained within it. The file path can optionally be added to the file names by setting the second parameter to TRUE.

Download Helper

The Download Helper lets you download data to your desktop.
Loading this Helper

This helper is loaded using the following code:
$this->load->helper('download');

The following functions are available:
force_download('filename', 'data')

Generates server headers which force data to be downloaded to your desktop. Useful with file downloads. The first parameter is the name you want the downloaded file to be named, the second parameter is the file data. Example:
$data = 'Here is some text!';
$name = 'mytext.txt';

force_download($name, $data);

If you want to download an existing file from your server you'll need to read the file into a string:
$data = file_get_contents("/path/to/photo.jpg"); // Read the file's contents
$name = 'myphoto.jpg';

force_download($name, $data);
*/
/**
* Read File
*
* Opens the file specfied in the path and returns it as a string.
*
* @access	public
* @param	string	path to file
* @return	string
*/	
function read_file($file)
{
if ( ! file_exists($file))
{
	return FALSE;
}

if (function_exists('file_get_contents'))
{
	return file_get_contents($file);		
}

if ( ! $fp = @fopen($file, 'rb'))
{
	return FALSE;
}

flock($fp, LOCK_SH);

$data = '';
if (filesize($file) > 0)
{
	$data =& fread($fp, filesize($file));
}

flock($fp, LOCK_UN);
fclose($fp);

return $data;
}

// ------------------------------------------------------------------------

/**
* Write File
*
* Writes data to the file specified in the path.
* Creates a new file if non-existent.
*
* @access	public
* @param	string	path to file
* @param	string	file data
* @return	bool
*/	
function write_file($path, $data, $mode = 'wb')
{
if ( ! $fp = @fopen($path, $mode))
{
	return FALSE;
}

flock($fp, LOCK_EX);
fwrite($fp, $data);
flock($fp, LOCK_UN);
fclose($fp);	

return TRUE;
}

// ------------------------------------------------------------------------

/**
* Delete Files
*
* Deletes all files contained in the supplied directory path.
* Files must be writable or owned by the system in order to be deleted.
* If the second parameter is set to TRUE, any directories contained
* within the supplied base directory will be nuked as well.
*
* @access	public
* @param	string	path to file
* @param	bool	whether to delete any directories found in the path
* @return	bool
*/	
function delete_files($path, $del_dir = FALSE, $level = 0)
{	
// Trim the trailing slash
$path = preg_replace("|^(.+?)/*$|", "\\1", $path);

if ( ! $current_dir = @opendir($path))
	return;

while(FALSE !== ($filename = @readdir($current_dir)))
{
	if ($filename != "." and $filename != "..")
	{
		if (is_dir($path.'/'.$filename))
		{
			$level++;
			delete_files($path.'/'.$filename, $del_dir, $level);
		}
		else
		{
			unlink($path.'/'.$filename);
		}
	}
}
@closedir($current_dir);

if ($del_dir == TRUE AND $level > 0)
{
	@rmdir($path);
}
}

// ------------------------------------------------------------------------

/**
* Get Filenames
*
* Reads the specified directory and builds an array containing the filenames.  
* Any sub-folders contained within the specified path are read as well.
*
* @access	public
* @param	string	path to source
* @param	bool	whether to include the path as part of the filename
* @return	array
*/	
function get_filenames($source_dir, $include_path = FALSE)
{
static $_filedata = array();

if ($fp = @opendir($source_dir))
{
	while (FALSE !== ($file = readdir($fp)))
	{
		if (@is_dir($source_dir.$file) && substr($file, 0, 1) != '.')
		{
			 get_filenames($source_dir.$file."/", $include_path);
		}
		elseif (substr($file, 0, 1) != ".")
		{

			$_filedata[] = ($include_path == TRUE) ? $source_dir.$file : $file;
		}
	}
	return $_filedata;
}
}


/**
* Force Download
*
* Generates headers that force a download to happen
*
* @access	public
* @param	string	filename
* @param	mixed	the data to be downloaded
* @return	void
*/	
function force_download($filename = '', $data = '')
{
if ($filename == '' OR $data == '')
{
	return FALSE;
}

// Try to determine if the filename includes a file extension.
// We need it in order to set the MIME type
if (FALSE === strpos($filename, '.'))
{
	return FALSE;
}

// Grab the file extension
$x = explode('.', $filename);
$extension = end($x);

// Load the mime types
@include(APPPATH.'config/mimes'.EXT);

// Set a default mime if we can't find it
if ( ! isset($mimes[$extension]))
{
	$mime = 'application/octet-stream';
}
else
{
	$mime = (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];
}

// Generate the server headers
if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE"))
{
	header('Content-Type: "'.$mime.'"');
	header('Content-Disposition: attachment; filename="'.$filename.'"');
	header('Expires: 0');
	header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
	header("Content-Transfer-Encoding: binary");
	header('Pragma: public');
	header("Content-Length: ".strlen($data));
}
else
{
	header('Content-Type: "'.$mime.'"');
	header('Content-Disposition: attachment; filename="'.$filename.'"');
	header("Content-Transfer-Encoding: binary");
	header('Expires: 0');
	header('Pragma: no-cache');
	header("Content-Length: ".strlen($data));
}

echo $data;
}


?>

There you go, that should help some.  These functions here can do almost anything with files.

For the zipping it's not that hard.

The functions up there can easly get you directory listing of items in folders (as well as sub-folders) and everything else).

You may even find one up there that deals with zipping, not sure if I had one for that, but it's not hard just wshow your code.

Link to comment
https://forums.phpfreaks.com/topic/71199-zip-help-pls/#findComment-358251
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.