dumping to text file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

dumping to text file

Post 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.
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post 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
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

Post by gurjit »

no luck in writing it at the moment. having trouble creating directories.

any chance of getting the help.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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);

?>
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

Post 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);
?>
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

Post 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?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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.
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

Post 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); 
?>
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Did m2.txt get created ok? And does it contain the test string?
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

Post by gurjit »

no my.txt never got created. but my.zip did and its 0 bytes.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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?
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

Post 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?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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.
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Post 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)
Post Reply