FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Why does this code make undeletable files?





Dougie1
This code makes files (which I want) but I want them to be deletable and usable in stead of mucking up. Why?

Code:
$f = file('http://asite.com/');
$text = "$f[121]";
$newtext = str_replace('cool', 'uncool', $text);


$Word = $newtext;


$file = "Directory/$Word.txt";
if (file_exists($file)) {} else {

$fp = fopen ("$file", "wb");

fclose($fp);

}
All I want it to do is to have a file with the contents of the line as the name of the file. I do not want the file to have any contents although it doesn't really matter.

Even if I use ("$file", "w"); it still does not work to give working files.
hexkid
Because the mode of the file only allows the creator (probably "nobody", or "apache") to change it. You need to change the mode of the file.
Use chmod() for that.

Code:
//...
$fp = fopen ("$file", "wb");    // extra quotes
if ($fp) {                      // test if file creation worked
    fclose($fp);
    if (!chmod($file, 0666)) {  // change mode to "everybody can read/write"
        echo 'Unable to change mode.';
    }
}
// ...
Dougie1
OK. I have edited my code and have this now:

Code:
    fclose($fp);
chmod("Dictionary/$Word.txt", 0777);

But the files are still uneditable. Am I doing something wrong. The files have been changed to 777. The files don't even appear in smart ftp but do on online ones but cause errors.

Thanks for the help so far hexkid. You know your stuff Very Happy
DanielXP
Code:
$f = file('http://asite.com/');
$text = "$f[121]";
$newtext = str_replace('cool', 'uncool', $text);


$Word = $newtext;


$file = "Directory/$Word.txt";
if (file_exists($file)) {} else {

$fp = fopen ("$file", "wb");    // extra quotes
if ($fp) {                      // test if file creation worked
    fclose($fp);
    if (!chmod($file, 0666)) {  // change mode to "everybody can read/write"
        echo 'Unable to change mode.';
    }
}

}
hexkid
Quote:
Code:
$f = file('http://asite.com/');
$text = "$f[121]";
$newtext = str_replace('cool', 'uncool', $text);


$Word = $newtext;


$Word is now something like "This is a uncool effect.\n". Notice the last character ("\n"): it is perfectly valid to have embedded ENTERs in file names, but they might not work as you expect.

There is still, probably, one other thing you need to check/change but I'll leave it up to you to find ... or maybe I'll tell you what it is when you get this part right.
Dougie1
hexkid wrote:
$Word is now something like "This is a uncool effect.\n". Notice the last character ("\n"): it is perfectly valid to have embedded ENTERs in file names, but they might not work as you expect.

There is still, probably, one other thing you need to check/change but I'll leave it up to you to find ... or maybe I'll tell you what it is when you get this part right.
Thanks. The problem is that I have embedded enters in my file. I do not know how to stop that though... How do I stop the enters appearing in the file?

Thanks for helping me realise the problem.
hexkid
Use trim().

Code:
# $text = "$f[121]";
$text = trim($f[121]);
Dougie1
I think I just fell in love with you hexkid!

Thanks so much Smile

Could I ask you if you used a book to learn PHP or what you did as you are so good at it and I want to get better. Thanks.
hexkid
Dougie1 wrote:
I want to get better.

Write code, write more code, write even more code ... (and not only PHP, write code in any language you're willing to try).

As for books, no, I didn't use any (I once bought a book, but gave it to a friend who needed it more than me). I mostly used (and still do, a lot) the PHP manual which is organized in a way I find very easy to use. Also PHP itself is a language I find very suited to my way of thinking (no obligation to use objects is a big plus!)
Related topics
Some interesting Software
Why does EVERY "Favorite" thread get locked now?
Why does it take time for Frihost to load
why does my subdomain of my site is 403 error?
Why this code dont work ?
why does the good guys.....?
why does microsoft word create HTML pages with so many tags?
Why does google want to start another "facebook" /
Why does it feel good to be bad?
[OFFICIAL]What song are you listening to right now ?
[tutor] How to protect images without htaccess using PHP
Creating an RPG bot in IRC (yes I wrote this)
Microsoft is upto it's dirty games again!
What do you think about Mac OS X?
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.