473,395 Members | 1,443 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

create file and write content in same code block

This a modified part of a larger script I made. I couldn't get it
working so I made a smaller part of it - hoping to get it working.
Well it still doesn't work :(
The following code creates the new file, but does not write the
$content strings to the file. It then prints "Did nothing!" instead of
"done!".

<?php

$word = "2002";

$content1 = "one ";
$content2 = "two ";
$content3 = "three";

$fileName = '/home/leke/www/dic/' . $word . '.html';
$handle = fopen($fileName, 'w');
if (file_exists($fileName)) {
print "Did nothing!";
}
else
{
fwrite($handle, "$content1 . $content2 . $content3");
fclose($handle);
print "done!";
}
?>

I cant figure out the problem. Can anyone help me and maybe explain
what went wrong?
Thanks :)
Jul 31 '08 #1
3 1454
Maybe you need to change the permissions for the '/home/leke/www/dic/'
directory to allow write.
Jul 31 '08 #2
le************@gmail.com wrote:
This a modified part of a larger script I made. I couldn't get it
working so I made a smaller part of it - hoping to get it working.
Well it still doesn't work :(
The following code creates the new file, but does not write the
$content strings to the file. It then prints "Did nothing!" instead of
"done!".

<?php

$word = "2002";

$content1 = "one ";
$content2 = "two ";
$content3 = "three";

$fileName = '/home/leke/www/dic/' . $word . '.html';
$handle = fopen($fileName, 'w');
if (file_exists($fileName)) {
print "Did nothing!";
}
else
{
fwrite($handle, "$content1 . $content2 . $content3");
fclose($handle);
print "done!";
}
?>

I cant figure out the problem. Can anyone help me and maybe explain
what went wrong?
Thanks :)
The fopen($filename,'w') function will create the file. Therefore, the
file exists, and file_exists() will return true, causing the code to
take the "Did nothing" branch.

Try this:

$fileName = '/home/leke/www/dic/' . $word . '.html';

if (file_exists($fileName)) {
print "Did nothing!";
}
else
{
$handle = fopen($fileName, 'w');
fwrite($handle, "$content1 . $content2 . $content3");
fclose($handle);
print "done!";
}

Of course, you probably want to add some error checking after the
fopen() call to make sure the file was created properly before writing
to it, but this sequence should work.

Jul 31 '08 #3
Ok, I see what you mean. Originally I thought because fopen($fileName,
'w'); was assigned to $handle , it wouldn't be executed until $handle
was called in fwrite($handle, ...

Thanks for pointing that out :)
Jul 31 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: moondaddy | last post by:
I want to dynamically create a JavaScript file and cache it on the client for re-use. I know how to write javascript to a web page from the code behind, but I don't know how to actually create a...
7
by: theyas | last post by:
How can I get my code to NOT display two "Open/Save/Cancel/More Info" dialog boxes when using the "Response.WriteFile" method to download a file to IE I've asked about this before and didn't get a...
6
by: windandwaves | last post by:
Hi Folk Some of my clients asked me to create "fancy emails" for them (aka html formatted emails). I know how to make a nice html document, but I had trouble creating a simple way to provide...
5
by: Michael Sperlle | last post by:
Is it possible? Bestcrypt can supposedly be set up on linux, but it seems to need changes to the kernel before it can be installed, and I have no intention of going through whatever hell that would...
5
by: peter | last post by:
Hello all, I'm looking for an advice. Example (one block in ascii file): $------------------------ NAME='ALFA' CODE='x' $------------------------
1
by: Alex | last post by:
Hello, I'm trying to write a little php script to transfert some files from a server to clients (web/http). It's working fin with small files. But transfering big files (try on 1Gb) failed!...
1
by: ujjwaltrivedi | last post by:
Hey guys, Can anyone tell me how to create a text file with Unicode Encoding. In am using FileStream Finalfile = new FileStream("finalfile.txt", FileMode.Append, FileAccess.Write); ...
15
by: lxyone | last post by:
Using a flat file containing table names, fields, values whats the best way of creating html pages? I want control over the html pages ie 1. layout 2. what data to show 3. what controls to...
6
by: slinky | last post by:
I found the following code to transfer datagrid data to an Excel file. Is this written in C#?... I'm a vb.netter. I'm just not sure where to place the code to experiment on it. Should I place it in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.