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);
Related
I'm trying to make a visitor counter with php that will create yy-mm-dd.txt everyday and contain the number of visitors that day and after 12 AM it will create a new yy-mm-dd.txt file.
As example today is 2019-06-02 so the text file will be 2019-06-02.txt and in the next day, 2019-06-03.txt file will be automatically created.
Here is what I tried but it is not creating new 2019-06-03.txt file after 12 AM. It keeps the same 2019-06-02.txt file
<?php
$date = date('Y-m-d');
$fp = fopen('dates/'.$date.'.txt', "r");
$count = fread($fp, 1024);
fclose($fp);
$count = $count + 1;
$fp = fopen('dates/'.$date.'.txt', "w");
fwrite($fp, $count);
fclose($fp);
?>
How to fix it?
Your code should be working fine. We can also add is_dir and file_exists checks, and we can use either fopen, fwrite and fclose or file_get_content/file_put_content, if we like. We can also add a default_timezone such as:
date_default_timezone_set("America/New_York");
Then, our code would look like something similar to:
date_default_timezone_set("America/New_York");
$dir = 'dates';
if (!is_dir($dir)) {
mkdir($dir, 0755, true);
}
$count = 1;
$date = date('Y-m-d');
$filename = $dir . '/' . $date . '.txt';
if (!file_exists($filename)) {
$fp = fopen($filename, "w");
fwrite($fp, $count);
fclose($fp);
} else {
$count = (int) file_get_contents($filename) + 1;
if ($count) {
file_put_contents($filename, $count);
} else {
print("Something is not right!");
}
}
Better use file_get_contents then file_put_contents:
<?php
$count = 1;
$content = file_get_contents(date('Y-m-d').'txt');
if($content !== FALSE){
$count+=(int)$content;
}
file_put_contents(date('Y-m-d').'txt', $count);
?>
I want to store a random key for every user in temp file . But what is the problem in my code it's echo the file path and can't create a temp file .
<?php
function random_string($length) {
$key = '';
$keys = array_merge(range(0, 9), range('a', 'z'));
for ($i = 0; $i < $length; $i++) {
$key .= $keys[array_rand($keys)];
}
return $key;
}
$data = random_string(50);
$filename = tempnam(sys_get_temp_dir(), 'prefix');
$fp = fopen($filename, 'w');
fwrite($fp, $data);
fclose($fp);
?>
call this file by:
<?php
$temp_file = tempnam(sys_get_temp_dir(), '');
echo $temp_file;
?>
result is something like this:/tmp/LnRFYu
/tmp dir is empty haven't create anything .
what is the problem on my code
There is no issue with the temp file creation code, so I don't really see what you are asking. I'm assuming you want to be able to view the contents of a specific temp file that has been created.
To do that your can use file_get_contents($file_name);
Like so
$contents = file_get_contents(sys_get_temp_dir() . 'temp_file_name.tmp');
echo $contents;
I'm creating an upload form that when uploading files will check to see if a file with the same name already exists in the directory. If a file with the same name is found, the script should increment the file by appending the file name with a number such as test1.txt test2.txt and so on until a match is not found. This is what I have come up with so far but wanted to see if there is a different approach I should take.
Also note, my filenames are already cleaned before they enter this part of the script so my filename and extension functions are simplified.
function filename($file){
return substr($file,0,strrpos($file,'.'));
}
function extension($file){
return strtolower(substr(strrchr($file,'.'),1));
}
$new_file = 'test.txt';
$dir = 'files';
$files = scandir($dir);
$exclude = array('.','..');
foreach($files as $file){
if(!in_array($file,$exclude)){
$file_array[] = $file;
}
}
$i = 1;
while(in_array($new_file,$file_array)){
$filename = filename($new_file);
$extension = extension($new_file);
if($i>1){
$num = strlen($filename);
$filename = substr($filename,0,-1);
$new_file = $filename.$i.'.'.$extension;
} else {
$new_file = $filename.$i.'.'.$extension;
}
echo $new_file.'<br>';
echo $i.'<br>';
$i++;
}
echo $new_file;
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);
I have compressed_file.zip on a site with this structure:
I want to extract all content from version_1.x folder to my root folder:
How can I do that? is possible without recursion?
It's possible, but you have to read and write the file yourself using ZipArchive::getStream:
$source = 'version_1.x';
$target = '/path/to/target';
$zip = new ZipArchive;
$zip->open('myzip.zip');
for($i=0; $i<$zip->numFiles; $i++) {
$name = $zip->getNameIndex($i);
// Skip files not in $source
if (strpos($name, "{$source}/") !== 0) continue;
// Determine output filename (removing the $source prefix)
$file = $target.'/'.substr($name, strlen($source)+1);
// Create the directories if necessary
$dir = dirname($file);
if (!is_dir($dir)) mkdir($dir, 0777, true);
// Read from Zip and write to disk
$fpr = $zip->getStream($name);
$fpw = fopen($file, 'w');
while ($data = fread($fpr, 1024)) {
fwrite($fpw, $data);
}
fclose($fpr);
fclose($fpw);
}
I was getting similar errors to #quantme when using #netcoder's solution. I made a change to that solution and it works without any errors.
$source = 'version_1.x';
$target = '/path/to/target';
$zip = new ZipArchive;
if($zip->open('myzip.zip') === TRUE) {
for($i = 0; $i < $zip->numFiles; $i++) {
$name = $zip->getNameIndex($i);
// Skip files not in $source
if (strpos($name, "{$source}/") !== 0) continue;
// Determine output filename (removing the $source prefix)
$file = $target.'/'.substr($name, strlen($source)+1);
// Create the directories if necessary
$dir = dirname($file);
if (!is_dir($dir)) mkdir($dir, 0777, true);
// Read from Zip and write to disk
if($dir != $target) {
$fpr = $zip->getStream($name);
$fpw = fopen($file, 'w');
while ($data = fread($fpr, 1024)) {
fwrite($fpw, $data);
}
fclose($fpr);
fclose($fpw);
}
}
$zip->close();
}
Look at the docs for extractTo. Example 1.