Page 1 of 2

dumping to text file

Posted: Wed Mar 03, 2004 6:21 am
by gurjit
hi all,

has anyone got any code that will allow me to
1. dump some data in a standard text file,
2. name that file,
3. zip it
4. and put it into a specific folder on the server. if that folder does not exist then create it.

Posted: Wed Mar 03, 2004 6:25 am
by malcolmboston
some links you should check out
Directory Functions
Writing To A Text File
rename a file
Zip Functionality

i'd write the script but im on my way out, if you havent done it by the time i get back ill write it out for you

Posted: Wed Mar 03, 2004 7:32 am
by gurjit
no luck in writing it at the moment. having trouble creating directories.

any chance of getting the help.

Posted: Wed Mar 03, 2004 7:43 am
by markl999
Here's a quick example you can tweak to your needs. It uses PclZip as PHP's native zip functions only have read support. ( http://www.phpconcept.net/pclzip/index.en.php )

Code: Select all

<?php

$txtName = 'foo.txt';
$zipName = 'foo.zip';
$dirName = 'somedir';

$string = 'some text to zip up';
//first write the text to a text file
$fp = fopen($txtName, 'w');
fputs($fp, $string);
fclose($fp);

//create the directory if it doesn't exist
if(!is_dir($dirName)){
  mkdir($dirName);
}

//create the zip file
require_once 'pclzip.lib.php';
$archive = new PclZip($dirName.'/'.$zipName);
$v_list = $archive->create($txtName);

?>

Posted: Wed Mar 03, 2004 7:49 am
by gurjit
why doesent this work. it gives errors:

and where do i get the require_once file 'require_once 'pclzip.lib.php'; '

i'm need to always create a new txt file to write to.

Code: Select all

<?php
$txtName = 'foo.txt'; 
$zipName = 'foo.zip'; 
$dirName = 'c:/inetpub/wwwroot/backups/$chosen_locationname'; 

$string = 'some text to zip up'; 
$fp = fopen($txtName, 'w'); 
fputs($fp, $string); 
fclose($fp); 

if(!is_dir($dirName)){ 
  mkdir($dirName); 
} 

require_once 'pclzip.lib.php'; 
$archive = new PclZip($dirName.'/'.$zipName); 
$v_list = $archive->create($txtName);
?>

Posted: Wed Mar 03, 2004 7:51 am
by markl999
You get 'pclzip.lib.php' from the link in the post. Like i said, you'll want to customise it to your needs, that will include making sure 'pclzip.lib.php' is in your include_path, usually putting it in the same directory as this script will do.
The code (that works for me) was to demonstrate :
1. Writing text to a file
2. Creating a directory
3. Creating a zip file

Posted: Wed Mar 03, 2004 8:18 am
by gurjit
i cant delete the file from the server now and its 0 bytes

the script creates the dir and file but it 0 bytes why?

Posted: Wed Mar 03, 2004 8:21 am
by markl999
What file? And what does your code look like?
If it's still like it is a bove then you'll need to change
$dirName = 'c:/inetpub/wwwroot/backups/$chosen_locationname';
to
$dirName = "c:/inetpub/wwwroot/backups/$chosen_locationname";
as variables don't get interpolated in single quotes, which basically means they don't work. Eg echo '$foo' will actually echo $foo whreas echo "$foo"; will echo the value of $foo. I used single quotes in my example as there were no variables in there.

Posted: Wed Mar 03, 2004 8:25 am
by gurjit
the zip file got created and i changed the code as above. but the zip file is 0 bytes. it has no text file inside it.

Code: Select all

<?php
$txtName = "m2.txt"; 
$zipName = "m2.zip"; 
$dirName = "c:/inetpub/wwwroot/$chosen_locationname"; 

$string = 'some text to zip up'; 
$fp = fopen($txtName, 'w'); 
fputs($fp, $string); 
fclose($fp); 

if(!is_dir($dirName)){ 
  mkdir($dirName); 
} 

require_once 'pclzip.lib.php'; 
$archive = new PclZip($dirName.'/'.$zipName); 
$v_list = $archive->create($txtName); 
?>

Posted: Wed Mar 03, 2004 8:26 am
by markl999
Did m2.txt get created ok? And does it contain the test string?

Posted: Wed Mar 03, 2004 8:28 am
by gurjit
no my.txt never got created. but my.zip did and its 0 bytes.

Posted: Wed Mar 03, 2004 8:30 am
by markl999
m2.txt needs to be created or the zip will be 0 length,as it will have zipped up nothing.
If m2.txt isn't created then you may have permission problems. What are you running on, i'm guessing windows..but are you using apache, IIS etc.. and what version of PHP?

Posted: Wed Mar 03, 2004 8:33 am
by gurjit
i'm using apache and i have given the folder permissions read,write execute for all users. the text file never gets created but the zip file does with 0 bytes. do i need to change anything in the pclzip.lib.php file. i downloaded it and just called it and never customised anything. if i need to change things what do i need to change?

Posted: Wed Mar 03, 2004 8:38 am
by markl999
No, the script is failing before the zip file bit, the zip code is working ok, it just has nothing to zip up. m2.txt needs to be created so there's something to zip up, if it's not being created then something else is wrong. Just

Code: Select all

<?php
error_reporting(E_ALL); 
$txtName = "m2.txt"; 
$zipName = "m2.zip"; 
$dirName = "c:/inetpub/wwwroot/$chosen_locationname"; 

$string = 'some text to zip up'; 
$fp = fopen($txtName, 'w'); 
fputs($fp, $string); 
fclose($fp);
echo 'Done';
?>
should create the .txt file, if it doesn't then i wouldn't know where to start looking for the problem on windows, but 90% of the time it's permissions/paths related.

Posted: Wed Mar 03, 2004 8:42 am
by liljester
did you set share premissions on the folder, or directory permissions? if you only adjusted the privileges in IIS, you should prolly also adjust the directory permissions (browse to the folder and right click, click security tab)