how solve mkdir and fopen file in php? - php

When I want to do folder for the member who registered into my site Inside this folder is a folder for photos and the main file of the member.
this is problem :-
The first folder is done with the member name successfully but the photo folder and the profile page of the member do not succeed.
thank you .....
this is my code I changed the code by the guy who helped me
but the same problem exists
The first folder is successfully completed
The problem is in
file settings, folders, images, and index
<?php
$us = "";
if(isset($_POST['username'])){ $us= $_POST['username']; }
if(isset($_POST['username'])){
mkdir("../profiles/$us", 0777, true);
mkdir("../profiles/$us/images", 0777, true);
$filename = "../profiles/$us/index.php";
$ourFileName = $filename;
$ourFileHandle = fopen($ourFileName, 'w');
$written = "
<html>
<body>
<?php
echo \"hi man \";
</body>
</html>
";
fwrite($ourFileHandle, $written);
fclose($ourFileHandle);
$myfile = fopen("../profiles/$us/setting.php", "w") or die("Unable to open file!");
fwrite($myfile, $txt);
$txt = "Minnie Mouse\n";
fwrite($myfile, $txt);
fclose($myfile);
}
?>

You cant assign $us = $_POST["username"] inside of brackets and then use $us again later.
Anything inside of brackets is constrainted within those brackets.
You have to define $us = "" outside of the scope of the IF statement on your first statement.
<?php
$us = "";
if(isset($_POST['username'])){ $us= $_POST['username']; }
if(isset($_POST['username'])){
etc etc
}

Related

How to write a html file using PHP?

I used below HTML code and php code to Write new files.
<html>
<body>
<form action="write.php" method="get">
ID : <input type="text" name="ID"><br>
<input type="submit">
</form>
</body>
</html>
<?php
$myfile = fopen($_GET["ID"] , "w") or die("Unable to open file!");
$txt = "Your user ID =";
fwrite($myfile, $txt);
$txt = $_GET["ID"];
fwrite($myfile, $txt);
fclose($myfile);
?>
If I submit TEST to html form , php code writes a file named "test". it hasn't a file extension.
How to write "TEST.html" with above code.?
Add the extension to the filename before opening the file:-
$filename = $_GET["ID"] . '.html';
$myfile = fopen($filename, "w") or die("Unable to open file!");
Note You are opening up yourself to all kinds of security issues here.
just use this code:-
fopen($_GET["ID"].'.html' , "w")
You need to add .html with a !empty() check there, like below:-
<?php
if(!empty($_GET['ID'])){ // check first that ID is coming or not
$myfile = fopen($_GET["ID"]."html" , "w") or die("Unable to open file!");
$txt = "Your user ID =";
fwrite($myfile, $txt);
$txt = $_GET["ID"];
fwrite($myfile, $txt);
fclose($myfile);
}
?>
To open and read this file:-
<?php
if(!empty($_GET['ID'])){ // check first that ID is coming or not
$myfile = fopen($_GET["ID"]."html", "r") or die("Unable to open file!");
echo fread($myfile,filesize($_GET["ID"]));
fclose($myfile);
}
?>
OR:-
<?php
if(!empty($_GET['ID'])){ // check first that ID is coming or not
$file = fopen($_GET["ID"]."html","r") or die("Unable to open file!"); ;
while(! feof($file)){
echo fgets($file). "<br />";
}
fclose($file);
}
?>
Note:-
In my opinion you need .txt rather than .html.An HTML file with text like test really have no mean, or server any useful purpose. BTW it's up-to-you what extension you want. I just gave my opinion.

php code with random function not working

In this program i want that name of the image gets changed after it has been displayed on the page
But it gets changed before it could be displayed ...
the text file is used to get the name of image and also to store the new name of image file
<?php
$myfile = fopen("givename.txt", "r");
$d= fread($myfile,filesize("givename.txt"));
fclose($myfile);
img src="?php echo "80/$d.jpg";?>">
$rand=(mt_rand(10,100));
$mfile = fopen("givename.txt", "w") ;
fwrite($mfile, $rand);
fclose($mfile);
rename("80"."/"."$d".".jpg","80"."/"."$rand".".jpg");
?>
<?php
$myfile = fopen("givename.txt", "r");
$d= fread($myfile,filesize("givename.txt"));
fclose($myfile);
"<img src='80/$d.jpg'>";
$rand=(mt_rand(10,100));
$mfile = fopen("givename.txt", "w") ;
fwrite($mfile, $rand);
fclose($mfile);
rename("80"."/"."$d".".jpg","80"."/"."$rand".".jpg");
?>

Trying to build a file in PHP and use fwrite to file

trying to figure out how i can make a and save a file while including default info from another file. i tried include but it did not work. any suggestion on how to build this file?
<?php
$wrtID = $_POST["fileID"];
SQL statement here to get relevant info
mkdir("CNC/$wrtID", 0770, true);
?>
<?php
$batfile = fopen("CNC/$wrtID/$wrtID.bat", "w") or die("Unable to open file!");
$txt = "
#ECHO OFF
#ECHO **** Run NC-Generator WOODWOP 4.0 ****
NCWEEKE.exe -n=C:/WW4/$wrtID/$wrtID-sl.mpr
NCWEEKE.exe -n=C:/WW4/$wrtID/$wrtID-sr.mpr
NCWEEKE.exe -n=C:/WW4/$wrtID/$wrtID-tb.mpr
NCWEEKE.exe -n=C:/WW4/$wrtID/$wrtID-dc.mpr
#ECHO **** Done ****
";
fwrite($batfile, $txt);
fclose($batfile);
?>
<?php
$slfile = fopen("CNC/$wrtID/$wrtID-sl.mpr", "w") or die("Unable to open file!");
$txt = "
include("defaultcnc.php");
if ( additional file pats needed ) {
include("component-1.php");
}
";
fwrite($slfile, $txt);
fclose($slfile);
?>
I don't see a problem in the first block of code.
On the second block, the interpreter will consider the second double quotes to mean the end of the string $txt = " include(". So everything after that would produce a PHP error.
But even if you escape those the mpr file will have the string include("defaultcnc,php"); but not the actual contents of that file. For that you should do file_get_contents("defaultcnc.php").
something like:
<?php
$slfile = fopen("CNC/$wrtID/$wrtID-sl.mpr", "w") or die("Unable to open file!");
// set params to pass to defaultcnc.php
$value1 = 1;
$value2 = "I'm a text string";
$file = urlencode("defaultcnc.php?key1=".$value1."&key2=".$value2);
$txt = file_get_contents($file);
if ( additional file pats needed ) {
$txt .= file_get_contents("component-1.php");
}
fwrite($slfile, $txt);
fclose($slfile);
?>
I assume additional file pats needed means something to you. It should be a condition evaluating either true or false.

PHP: Save file to a specific directory

I have this diretorio.php file that grabs the user id in Joomla and creates a directory with that id (if it doesn't exist yet):
/* Get the current user id */
$user = JFactory::getUser();
$usr_id = $user->get('id');
/*Define the path to this user's directory */
$diretorio=$_SERVER['DOCUMENT_ROOT']."/Apps/files/".$usr_id;
/*Create the user's directory if it doesn't exist */
if (!file_exists($diretorio) and !is_dir($diretorio)) {
mkdir($diretorio, 0755);
};
Now, I want to save a file with data in an object, using an Ajax that triggers another PHP file to the same directory created above:
$myFile = $diretorio."/dados.json";
$fh = fopen($myFile, 'w') or die("não é possível abrir o ficheiro");
$stringData = $_POST['data'];
$stringData='{ "data":'.json_encode($stringData).'}';
fwrite($fh, $stringData);
fclose($fh);
However, the file isn't created. If I replace the first line to:
$myFile = "dados.json";
It will create the file in the same directory where this PHP script is stored.
I would recommend using Joomla coding standards like so:
$user = JFactory::getUser();
$usr_id = $user->get('id');
$diretorio = JPATH_SITE . "/Apps/files/" . $usr_id;
if (!JFolder::exists($diretorio)) {
JFolder::create($diretorio, 0775);
}
$myFile = $diretorio."/dados.json";
$stringData = $_POST['data'];
$stringData = '{ "data":'.json_encode($stringData).'}';
JFile::write($myFile, $stringData);
JPATH_SITE is the root of your Joomla site

writing http post contents to file in php

I am posting a string xml data to a php page hosted in IIS. All I want is to be able to read the string data that I receive in php page and write it to a file. But am not able to achieve the same. Here is my code:-
<?php
if($_SERVER['REQUEST_METHOD'] == "POST"){
echo "Hello\n";
$somecontent = print_r($_POST, TRUE);
$my_file = "resp.txt";
$handle = fopen($my_file, "w") or die('Cannot open file: '.$my_file);
fwrite($handle, $somecontent);
}else {
echo "Error\n";
}
?>
But am not able to create the file or read the POST contents. I would be glad if someone would figure out how to solve this.
if you know the input field name then use this.
<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
{
echo "Hello\n";
$somecontent = array();
$somecontent[] = $_POST['firstname'];
$somecontent[] = $_POST['lastname'];
//etc...
$my_file = "resp.txt";
$handle = fopen($my_file, "wb") or die('Cannot open file: '.$my_file);
fwrite($handle, $somecontent);
fclose($handle);
}
else {
echo "Error\n";
}
?>
does this work?
if(!empty($_POST){
file_put_contents('resp.txt', serialize($_POST));
}

Categories