...

View Full Version : Problem Saving file to a Directory



trazix
08-22-2009, 06:38 PM
This works



if (!file_exists("Filestore/$filename.txt")){

$fp = fopen("Filestore/$filename.txt", "w") or die("Can't open file");



This does not work



$year = date("y");

if (!file_exists("Filestore/$year/$filename.txt")){

$fp = fopen("Filestore/$year/$filename.txt", "w") or die("Can't open file");



I have a form and when posted its contents are saved to a text file in Filestore directory.
When I modified it to save it to a directory based on what year it is it does not work. Any ideas would be apreciated

barkermn01
08-22-2009, 06:53 PM
Try this


$year = date("y");

if (!file_exists("Filestore/".$year."/".$filename.".txt")){

$fp = fopen("Filestore/".$year."/".$filename.".txt", "w") or die("Can't open file or create file");

trazix
08-22-2009, 07:07 PM
Does not work getting this message

Can't open file or create file

With my first atempt i got the message

Can't open file

Edit My bad just seen the or die statement

barkermn01
08-22-2009, 07:10 PM
ok you need to check if the year folder exist if not create it and chmod 777

if it dose chmod it to 777 so php can write to the directory

one min and i will show code to check for it and create it if it dose not exist



$year = date("y");

if (!file_exists("Filestore/".$year)){
mkdir("Filestore/".$year, 0777);
} else{
if(!is_writable("Filestore/".$year)) chmod("Filestore/".$year, 0777);
}

if (!file_exists("Filestore/$year/$filename.txt")){

$fp = fopen("Filestore/$year/$filename.txt", "w") or die("Can't Create file");

trazix
08-22-2009, 07:16 PM
The folder 2009 does exist. Checked the permissions and they are all ticked as being on
Permissions
u[ rwx ] g[ rwx ]

trazix
08-22-2009, 07:19 PM
Sorry barkermn01 that dont work for me. It saves ok to Filestore directory I cant see why it will not save into 2009 directory

barkermn01
08-22-2009, 07:24 PM
Sorry barkermn01 that dont work for me. It saves ok to Filestore directory I cant see why it will not save into 2009 directory

yea that's weird as all i know php need to save a file is directory needs to be chmod 0777, All permissions, and the file exists, dose then file already exist in the directory??

Hrm i gonna write a script and try some thing for you


$year = date("y");
$filename = time();

if (!file_exists("Filestore/".$year)){
mkdir("Filestore/".$year, 0777);
} else{
if(!is_writable("Filestore/".$year)) chmod("Filestore/".$year, 0777);
}

if (!file_exists("Filestore/$year/$filename.txt")){
$fp = fopen("Filestore/$year/$filename.txt", "w") or die("Can't Create file");
}else{
unlink("Filestore/$year/$filename.txt");
$fp = fopen("Filestore/$year/$filename.txt", "w") or die("Can't Create file");
}

trazix
08-22-2009, 07:31 PM
That created a directory 09 but did not save a file also I got no or die message this time

trazix
08-22-2009, 07:36 PM
Problem solved it was the y should have been Y The origanal code works with it , I must state that I am a beginner at this PHP and must also thank barkermn01 for your time.

barkermn01
08-22-2009, 07:38 PM
Problem solved it was the y should have been Y The origanal code works with it , I must state that I am a beginner at this PHP and must also thank barkermn01 for your time.

No Problem



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum