Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
load array values from .txt file
Old 07-15-2009, 12:45 AM load array values from .txt file
moatist's Avatar
Skilled Talker

Posts: 64
Trades: 0
Hey,

I have this PHP script that takes files specified in an array and zips them to a zip file.

If you look near the bottom of the code, you'll see that there is an array like the following:
PHP Code:
$file_names=array('files/file1.mp3','files/file2.mp3','files/file3.mp3'); 
What I want to do is load the array from a text file that has data like this (instead loading the array by the method shown above):
Code:
path/to/file1.mp3
path/to/file2.mp3
path/to/file3.mp3
path/to/file4.mp3
I've tried some stuff but I can't figure out how to get this to work.

PHP Code:
<?php
//function to zip and force download the files using PHP
function zipFilesAndDownload($file_names,$archive_file_name,$file_path)
{
    
//  Developed by Roshan Bhattarai 
    //  Visit http://roshanbh.com.np for this script and more.
    //  This notice MUST stay intact for legal use

    
$zip = new ZipArchive();
    
//create the file and throw the error if unsuccessful
    
if ($zip->open($archive_file_nameZIPARCHIVE::CREATE )!==TRUE) {
        exit(
"cannot open <$archive_file_name>\n");
    }
    
//add each files of $file_name array to archive
    
foreach($file_names as $files)
    {
          
$zip->addFile($file_path.$files,$files);
    }
    
$zip->close();
    
//then send the headers to foce download the zip file
    
header("Content-type: application/zip"); 
    
header("Content-Disposition: attachment; filename=$archive_file_name"); 
    
header("Pragma: no-cache"); 
    
header("Expires: 0"); 
    
readfile("$archive_file_name"); 
    exit;
}
//example of using above function
$file_names=array('files/file1.mp3','files/file2.mp3','files/file3.mp3');
$archive_file_name='zipped.zip';
$file_path=dirname(__FILE__).'/';
zipFilesAndDownload($file_names,$archive_file_name,$file_path);
?>
Thanks a million,
Moatist
__________________
Think in code; Dream in digital.

<?php if($helpfull == true){ $talkupation++; } ?>

Last edited by moatist; 07-15-2009 at 01:14 AM..
moatist is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 07-15-2009, 02:35 AM Re: load array values from .txt file
Extreme Talker

Posts: 181
Name: David Jackson
Trades: 0
just use file() to read the file in

PHP Code:
$txtFile file("textfile.txt"); 
$txtFile is now an array with every line a seperate value
__________________

Please login or register to view this content. Registration is FREE
davidj is offline
Reply With Quote
View Public Profile
 
Old 07-15-2009, 03:01 AM Re: load array values from .txt file
moatist's Avatar
Skilled Talker

Posts: 64
Trades: 0
Doesn't seem to be working.

Moatist
__________________
Think in code; Dream in digital.

<?php if($helpfull == true){ $talkupation++; } ?>
moatist is offline
Reply With Quote
View Public Profile
 
Old 07-15-2009, 03:29 PM Re: load array values from .txt file
Extreme Talker

Posts: 181
Name: David Jackson
Trades: 0
post your code
__________________

Please login or register to view this content. Registration is FREE
davidj is offline
Reply With Quote
View Public Profile
 
Old 07-15-2009, 03:30 PM Re: load array values from .txt file
moatist's Avatar
Skilled Talker

Posts: 64
Trades: 0
I already did...
__________________
Think in code; Dream in digital.

<?php if($helpfull == true){ $talkupation++; } ?>
moatist is offline
Reply With Quote
View Public Profile
 
Old 07-15-2009, 03:31 PM Re: load array values from .txt file
moatist's Avatar
Skilled Talker

Posts: 64
Trades: 0
I wonder if I need to use
PHP Code:
array_push() 
?
__________________
Think in code; Dream in digital.

<?php if($helpfull == true){ $talkupation++; } ?>
moatist is offline
Reply With Quote
View Public Profile
 
Old 07-15-2009, 04:17 PM Re: load array values from .txt file
moatist's Avatar
Skilled Talker

Posts: 64
Trades: 0
All right, I took the text file that has the file information stored in it and deleted all but one line. So it has no line breaks:
Code:
path/to/file1.mp3
Then used
PHP Code:
file("path/to/file.mp3"); 
I ran the script and it worked!!

I then tried adding some more lines to the file and it stopped working. Any suggestions?

It has something to do with line breaks or something. Could I use
PHP Code:
explode() 
and put a separator between the lines?


Thanks for the help people.
__________________
Think in code; Dream in digital.

<?php if($helpfull == true){ $talkupation++; } ?>
moatist is offline
Reply With Quote
View Public Profile
 
Old 07-15-2009, 04:24 PM Re: load array values from .txt file
moatist's Avatar
Skilled Talker

Posts: 64
Trades: 0
All right, I figured it out.

I just added
PHP Code:
explode() 
and added the separator '|' to the end of each line.

Final code:

PHP Code:
<?php
//function to zip and force download the files using PHP
function zipFilesAndDownload($file_names,$archive_file_name,$file_path)
{
    
//  Developed by Roshan Bhattarai 
    //  Visit http://roshanbh.com.np for this script and more.
    //  This notice MUST stay intact for legal use

    
$zip = new ZipArchive();
    
//create the file and throw the error if unsuccessful
    
if ($zip->open($archive_file_nameZIPARCHIVE::CREATE )!==TRUE) {
        exit(
"cannot open <$archive_file_name>\n");
    }
    
//add each files of $file_name array to archive
    
foreach($file_names as $files)
    {
        
$files explode('|',$files);
          
$zip->addFile($file_path.$files[0],$files[0]);
    }
    
$zip->close();
    
//then send the headers to foce download the zip file
    
header("Content-type: application/zip"); 
    
header("Content-Disposition: attachment; filename=$archive_file_name"); 
    
header("Pragma: no-cache"); 
    
header("Expires: 0"); 
    
readfile("$archive_file_name"); 
    exit;
}

//example of using above function

$file_names file("path/to/file.txt");


$archive_file_name="zipped.zip";
$file_path=dirname(__FILE__).'/';
zipFilesAndDownload($file_names,$archive_file_name,$file_path);
?>
file.txt:

Code:
path/to/file1.mp3|
path/to/file2.mp3|
path/to/file3.mp3|
path/to/file4.mp3|
I know it may not be the best way of doing it, but at least it works!

Moatist
__________________
Think in code; Dream in digital.

<?php if($helpfull == true){ $talkupation++; } ?>

Last edited by moatist; 07-15-2009 at 04:26 PM..
moatist is offline
Reply With Quote
View Public Profile
 
Old 07-15-2009, 04:45 PM Re: load array values from .txt file
Extreme Talker

Posts: 181
Name: David Jackson
Trades: 0
nice one
__________________

Please login or register to view this content. Registration is FREE
davidj is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to load array values from .txt file
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.61493 seconds with 12 queries