Jump to content

Help Writing to a file!


Deanznet

Recommended Posts

I need help! I cant get this to write the correct way!

 

What i need is based on the value of what is posted to the script it has to write it in the config file and also make a folder!

 

Please help

 

 

<?php
$start = '$uploadpath=\'';
$structure = '../banner/images/'.$_POST['FOLDER'].'\';\n';

$myFile = "PHP/confup.php";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "<?\n";
fwrite($fh, $stringData);
$stringData = "$start $structure";
fwrite($fh, $stringData);
$stringData = "?>\n";
fwrite($fh, $stringData);
fclose($fh);

// Desired folder structure


// To create the nested structure, the $recursive parameter 
// to mkdir() must be specified.

if (!mkdir($structure, 0777, true)) {
    die('Failed to create folders...');
}

// ...
?>

Link to comment
https://forums.phpfreaks.com/topic/233289-help-writing-to-a-file/
Share on other sites

There's a little problem right here:

$structure = '../banner/images/'.$_POST['FOLDER'].'\';\n';

Strings in single quotes won't evaluate special characters like \n or \r, etc. So it's literally inserting

../banner/images/';\n

into the php file.

Also, you're putting the same variable into mkdir(), which won't work.. unless you want a folder named exactly what I said above.

This'll do what you were trying to do:

<?php

$structure = "../banner/images/".$_POST['FOLDER'];

$myFile = "PHP/confup.php";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, "<?php \$uploadpath='$structure'; ?>");
fclose($fh);

// Desired folder structure


// To create the nested structure, the $recursive parameter 
// to mkdir() must be specified.

if (!mkdir($structure, 0775, true)) {
    die('Failed to create folders...');
}

// ...
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.