I am trying to create a file in a sub directory but it is not creating it
$tempFilename = "xml/tempfile.xml";
if(file_exists($tempFilename))
{
unlink($tempFilename);
}
$file = fopen($tempFilename,"w");
$fileTemplate = ($xml);
You can use file_put_contents
file_put_contents($tempFilename, '23');
Use fwrite :
$tempFilename = "xml/tempfile.xml";
// Test if directory exists.
$dirname = dirname($tempFilename);
if (!is_dir($dirname))
{
mkdir($dirname);
}
if(file_exists($tempFilename)){
unlink($tempFilename);
}
$file = fopen($tempFilename,"w");
// Write what you need
fwrite($file, 'content of my file');
fclose($file);
try this
first you need to create directory
$path = '/xml';
mkdir($path, 0777, true);
$tempFilename = "/xml/tempfile.xml";
if(file_exists($tempFilename))
{
unlink($tempFilename);
}
$file = fopen($tempFilename,"w");
fwrite($file, '23');
fclose($fp);
Related
Reading the file for the image url and calling the copy function.
imagecopy.txt
https://server.com/2017/12/check.png
https://server.com/2017/12/contacts.png
https://server.com/2018/06/CDP.bmp
https://server.com/module-acculturation-1.png
While copying the files from the url, getting the failed to open stream: Invalid argument error only on inside while loop. but works for the last record if the file has more files.
<?php
$file=fopen("imagecopy.txt","r") or exit("Unable to open file!");
while (!feof($file))
{
$source = fgets($file);
$imagename = explode("/", $source);
$pathname = 'uploads/' . date("Y") . '/' . date("m") . '/';
if (!is_dir($pathname))
{
mkdir($pathname, 0777, true);
}
$destination = $pathname.end($imagename);
copyimageURL($source, $destination);
}
fclose($file);
function copyimageURL($source, $destination)
{
echo $source;
echo "<br>";
echo $destination;
copy($source, $destination);
}
?>
1.Working fine with singe record
2.Copying the last image only if the file has more images list.
I'm guessing imagecopy.txt you're reading ends in a newline, which makes the last line of the file blank.
If you change
$source = fgets($file);
to
$source = trim(fgets($file));
if( empty($source) ) continue;
it should work fine
Try this:
if ($file) {
while (($name = fgets($file)) !== false) {
$imagename = basename($name);
$pathname = 'uploads/' . date("Y") . '/' . date("m") . '/';
if (!is_dir($pathname))
mkdir($pathname, 0777, true);
$destination = $pathname.$imagename;
copyimageURL(trim($name), $destination);
}
fclose($file);
}
I try to read all *.txt files from a folder and write all content from each file into another txt file. But somehow it only writes one line into the txt file.
I tried with fwrite() and file_put_contents(), neither worked.
Here is my code:
<?php
$dh = opendir('/Applications/XAMPP/xamppfiles/htdocs/test/');
while($file = readdir($dh)) {
$contents = file_get_contents('/Applications/XAMPP/xamppfiles/htdocs/test/' . $file);
$dc = array($contents);
}
file_put_contents('content.txt', $dc);
?>
This should work for you:
(Here I get all *.txt files in a directory with glob(). After this I loop through every file with a foreach loop and get the content of each single file with file_get_contents() and I put the content into the target file with file_put_contents())
<?php
$files = glob("path/*.txt");
$output = "result.txt";
foreach($files as $file) {
$content = file_get_contents($file);
file_put_contents($output, $content, FILE_APPEND);
}
?>
try this
$contents = array();
$line = file(/*next file in dir*/);
foreach($lines as line){
array_push($line, $contents);
}
//File path of final result
$filepath = "mergedfiles.txt";
$out = fopen($filepath, "w");
//Then cycle through the files reading and writing.
foreach($filepathsArray as $file){
$in = fopen($file, "r");
while ($line = fgets($in)){
print $file;
fwrite($out, $line);
}
fclose($in);
}
//Then clean up
fclose($out);
return $filepath;
Basically when I click my submit button the code should create a random string that is 5 characters in length. Then it should make a folder (relative position) with the name being the random string generated. Then it should create an index file and write the "content" variable to the file. Unfortunately it never even makes the directory. Any help? I can't figure out what's wrong.
<?php
$characters = "abcdefghijklmnopqrstuvwxyz"; // Valid Folder Characters
if(isset($_POST["submit"])) {
$folder = randomString($characters, 5);
$file = fopen($folder . "/index.html", "w");
$content = "File Content";
mkdir($folder, 0777);
fwrite($file, $content);
fclose($file);
}
// Generate Random Folder Name
function randomString($valid_chars, $length) {
$random_string = "";
$num_valid_chars = strlen($valid_chars);
for($i = 0; $i < $length; $i++) {
$random_pick = mt_rand(1, $num_valid_chars);
$random_char = $valid_chars[$random_pick - 1];
$random_string .= $random_char;
}
return $random_string;
}
?>
Make sure you have writable permission where or in which directory you are creating new directory and for check try
if (!mkdir($folder, 0777, true)) {
die('Failed to create folders...');
}
Also you need to first create dir then file open
if(isset($_POST["submit"])) {
$folder = randomString($characters, 5);
if (!mkdir($folder, 0777, true)) {
die('Failed to create folders...');
}
$file = fopen($folder . "/index.html", "w");
$content = "File Content";
fwrite($file, $content);
fclose($file);
}
Try this out
$oldmask = umask(0);
if(!file_exists($dir)) mkdir($dir, 0777);
umask($oldmask);
I want to create a php script that will check if a certain html file exist or not. if it doesn't exist, then create a new one and give it a name.
I have tried the code below, but still not working.
$file = file_get_contents(site_url('appraisal/createReport'));
$filename = 'Service_delivery_report_'.date('Y-m-d', time()).'.html';
$filepath = dirname(__DIR__).'/views/sd_reports/'.$filename;
write_file($filepath, $file);
if(! file_exists ($filename))
{
$fp = fopen($filename, 'w');
fwrite($fp, $file);
fclose($fp);
}
I'm not familiar with the method you're using called 'site_url'. From a quick google search it looks like it's a method in Word Press. Ignoring the site_url method, you might want to test using a specific url, like http://ted.com for example. I've specified is_file rather than file_exists because file_exists will return true even if the path you've specified is a directory, whereas is_file will only return true if the path is an actual file. Try this code, setting the $site variable to a site url or path to a file.
I've also switched some code around, doing a check first to see if the file exists before attempting to read in the contents of $site. This way, if the file already exists, you're not needlessly reading in the contents of $site.
$filename = "Service_delivery_report_" . date("Y-m-d",
time()). ".html";
$filepath = realpath("./") . "/views/sd_reports/" . $filename;
if (!is_file($filepath))
{
$site = "http://somesite.com/somepage";
if ($content = file_get_contents($site))
{
file_put_contents($filepath,
$content);
}
else
{
echo "Could not grab the contents of some site";
}
}
Use file_exists();
<?php
$filename = '/path/to/foo.txt';
if (file_exists($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
?>
Then, if it doesn't exist, create a file with fopen();, like so:
$handle = fopen($filename, "w");
Try this
$file = file_get_contents(site_url('appraisal/createReport'));
$filename = 'Service_delivery_report_'.date('Y-m-d', time()).'.html';
if(! file_exists ($filename))
{
$f = fopen($filename, "w");
fwrite($f, $file);
fclose($f);
}
else
echo "file already exist";
I want to know whether or not a directory exists.
If not, I would like to create the directory.
My code is below:
$da = getdate();
$dat = $da["year"]."-".$da["mon"]."-".$da["mday"];
$m = md5($url)."xml";
if(is_dir($dat))
{
chdir($dat);
$fh = fopen($m, 'w');
fwrite($fh, $xml);
fclose($fh);
echo "yes";
}
else
{
mkdir($dat,0777,true);
chdir($dat);
$fh = fopen($m, 'w');
fwrite($fh, $xml);
fclose($fh);
echo "not";
}
Use is_dir, which checks whether the path exists and is a directory then mkdir.
function mkdir_if_not_there($path) {
if (!is_dir($path)) {
// Watch out for potential race conditions here
mkdir($path);
}
}
Use is_dir:
$pathname = "/path/to/dir";
if(is_dir($pathname)) {
// do something
}