I'm trying to save all key values from the GET parameters, but its not writing anything to the file.
foreach ($_GET as $key => $value) {
$contents = $key . " => " . $value . "<br>";
echo($contents);
file_put_contents("./test.log", $contents, FILE_APPEND);
}
Don't use file_put_contents() inside loop. Put it outside:-
$contents='';
foreach ($_GET as $key => $value) {
$contents .= $key . " => " . $value . "\n"; // or use `"\r\n"`
}
file_put_contents("./test.log", $contents, FILE_APPEND);
Note:- Check that file have write permission (644) + folder in which this file lies have the permission too (777) and path of the file is correct.
Below are the screen-shots of working code at my local end:- http://prntscr.com/e98o04 And http://prntscr.com/e98oco
print_r will give you the same output you are trying to build, and you can solve this using just one line.
file_put_contents("./test.log", print_r($_GET, true), FILE_APPEND);
AS i seen the save data in file is in every iteration an only at iteration current position:
$contents='';
foreach ($_GET as $key => $value) {
$contents.= $key . " => " . $value . "<br>";
}
file_put_contents("./test.log", $contents, FILE_APPEND);
Same path of php code?, not needed "./", could you try to open file, before and put here if there is no error.
Related
I'm using ZipArchive's addEmptyDir/addFile methods to add files to a ZIP file in a loop and measure the current time.
Currently, the relevant part from my code looks like this:
$zip = new ZipArchive();
if (!$zip->open($inProgressZipName, ZIPARCHIVE::CREATE)) {
return array(
"result" => "error"
);
}
echo "start time=" . time() . "\n";
foreach ($list as $filePath) {
echo "loop time=" . time() . "\n";
$file = utf8_decode($filePath);
$zip->addFile($file, str_replace($path . '/', '', $file));
}
echo "end time=" . time() . "\n";
$zip->close();
I'm getting a very wrong output:
start time=1666532175
loop time=1666532175
loop time=1666532175
.
.
loop time=1666532175
loop time=1666532175
end time=1666532175
If I change my code to work with exec command instead like this:
$zip = new ZipArchive();
if (!$zip->open($inProgressZipName, ZIPARCHIVE::CREATE)) {
return array(
"result" => "error"
);
}
echo "start time=" . exec("date +%s") . "\n"; // exec() instead of time()
foreach ($list as $filePath) {
echo "loop time=" . exec("date +%s") . "\n"; // exec() instead of time()
$file = utf8_decode($filePath);
$zip->addFile($file, str_replace($path . '/', '', $file));
}
echo "end time=" . exec("date +%s") . "\n"; // exec() instead of time()
$zip->close();
I'm getting a correct input like this:
1666532505
loop time=1666532505
loop time=1666532505
.
.
loop time=1666532506
loop time=1666532506
.
.
loop time=1666532606
end time=1666532606
As you probably know, I can't use exec because this code will exist on a WordPress plugin, so... what is happening here?
I encounter the same problem before.
Please try to use PharData instead
note.. all folders chmod set to 777 for testing.
Okay, so i have been trying to design a simple cloud storage file system in php.After users log in they can upload and browse files in their account.
I am having an issue with my php code that scans the user's storage area. I have a script called scan.php that is called to return all of the users files and folders that they saved.
I originally placed the scan script in the directory called files and it worked properly, when the user logged in the scan script scanned the users files using "scan(files/usernamevalue)".
However I decided that I would prefer to move the scan script inside the files area that way the php script would only have to call scan using "scan(usernamevalue)". However now my script does not return the users files and folders.
<?php
session_start();
$userfileloc = $_SESSION["activeuser"];
$dir = $userfileloc;
// Run the recursive function
$response = scan($dir);
// This function scans the files folder recursively, and builds a large array
function scan($dir)
{
$files = array();
// Is there actually such a folder/file?
$i=0;
if(file_exists($dir))
{
foreach(scandir($dir) as $f)
{
if(!$f || $f[0] === '.')
{
continue; // Ignore hidden files
}
if(!is_dir($dir . '/' . $f))
{
// It is a file
$files[] = array
(
"name" => $f,
"type" => "file",
"path" => $dir . '/' . $f,
"size" => filesize($dir . '/' . $f) // Gets the size of this file
);
//testing that code actually finding files
echo "type = file, ";
echo $f .", ";
echo $dir . '/' . $f. ", ";
echo filesize($dir . '/' . $f)." ";
echo"\n";
}
else
{
// The path is a folder
$files[] = array
(
"name" => $f,
"type" => "folder",
"path" => $dir . '/' . $f,
"items" => scan($dir . '/' . $f) // Recursively get the contents of the folder
);
//testing that code actually finding files
echo "type = folder, ";
echo $f .", ";
echo $dir . '/' . $f. ", ";
echo filesize($dir . '/' . $f)." ";
echo"\n";
}
}
}
else
{
echo "dir does not exist";
}
}
// Output the directory listing as JSON
if(!$response)
{ echo"failes to respond \n";}
header('Content-type: application/json');
echo json_encode(array(
"name" => $userfileloc,
"type" => "folder",
"path" => $dire,
"items" => $response
));
?>
As you can see i added i echoed out all of the results to see if there
was any error in the scan process, here is what i get from the output as you
can see the function returns null, but the files are being scanned, i cant
seem to figure out where i am going wrong. Your help would be greatly
appreciated. Thank you.
type = file, HotAirBalloonDash.png, test/HotAirBalloonDash.png, 658616
type = folder, New directory, test/New directory, 4096
type = file, Transparent.png, test/Transparent.png, 213
failes to respond
{"name":"test","type":"folder","path":null,"items":null}
You forgot to return files or folders in scan function, just echo values. That is the reason why you get null values in the response.
Possible solution is to return $files variable in all cases.
I´m building a php programm which uploads a zip file, extracts it and generates a link for a specific file in the extracted folder. Uploading and extracting the folder works fine. Now I´m a bit stuck what to do next. I have to adress the just extracted folder and find the (only) html file that is in it. Then a link to that file has to be generated.
Here is the code I´m using currently:
$zip = new ZipArchive();
if ($zip->open($_FILES['zip_to_upload']['name']) === TRUE)
{
$folderName = trim($zip->getNameIndex(0), '/');
$zip->extractTo(getcwd());
$zip->close();
}
else
{
echo 'Es gab einen Fehler beim Extrahieren der Datei';
}
$dir = getcwd();
$scandir = scandir($dir);
foreach ($scandir as $key => $value)
{
if (!in_array($value,array(".",".."))) //filter . and .. directory on linux-systems
{
if (is_dir($dir . DIRECTORY_SEPARATOR . $value) && $value == $folderName)
{
foreach (glob($value . "/*.html") as $filename) {
$htmlFiles[] = $filename; //this is for later use
echo "<a href='". SK_PICS_SRV . DIRECTORY_SEPARATOR . $filename . "'>" . SK_PICS_SRV . DIRECTORY_SEPARATOR . $filename . "</a>";
}
}
}
}
So this code seems to be working. I just noticed a rather strange problem. The $zip->getNameIndex[0] function behaves differently depending on the program that created the zip file. When I make a zip file with 7zip all seems to work without a problem. $folderName contains the right name of the main folder which I just extracted. For example "folder 01". But when I zip it with the normal windows zip programm the excat same folder (same structure and same containing files) the $zip->getNameIndex[0] contains the wrong value. For example something like "folder 01/images/" or "folder 01/example.html". So it seems to read the zip file differently/ in a wrong way. Do you guys know where that error comes from or how I can avoid it? This really seems strange to me.
Because you specify the extract-path by yourself you can try finding your file with php's function "glob"
have a look at the manual:
Glob
This function will return the name of the file matching the search pattern.
With your extract-path you now have your link to the file.
$dir = "../../suedkurier/werbung/"
$scandir = scandir($dir);
foreach ($scandir as $key => $value)
{
if (!in_array($value,array(".",".."))) //filter . and .. directory on linux-systems
{
if (is_dir($dir . DIRECTORY_SEPARATOR . $value))
{
foreach (glob($dir . DIRECTORY_SEPARATOR . $value . "/*.html") as $filename) {
$files[] = $value . DIRECTORY_SEPARATOR $filename;
}
}
}
}
The matched files will now be saved in the array $files (with the subfolder)
So you get your path like
foreach($files as $file){
echo $dir . DIRECTORY_SEPARATOR . $file;
}
$dir = "the/Directory/You/Extracted/To";
$files1 = scandir($dir);
foreach($files1 as $str)
{
if(strcmp(pathinfo($str, PATHINFO_EXTENSION),"html")===0||strcmp(pathinfo($str, PATHINFO_EXTENSION),"htm")===0)
{
echo $str;
}
}
Get an array of each file in the directory, check the extension of each one for htm/html, then echo the name if true.
Can anyone tell me if it's possible to iterate over an array of SimpleXML objects that contain PDF data and have each print to separate PDF files? I've been fighting with this for over a week now. My latest loop code is as follows:
foreach($xml->DocumentPDFs->DocumentPDF->PDFBytes as $PDFBytes => $value) {
$binary = base64_decode($value);
file_put_contents($xml->EnvelopeStatus->EnvelopeID . "/" . $xml->EnvelopeStatus->DocumentStatuses->DocumentStatus->Name . ".pdf", $binary,FILE_APPEND);
}
This prints out the first PDF and then exits the loop.
So it turns out it was syntax issues. Both in the base64_decode call and the file_put_contents call:
foreach($xml->DocumentPDFs->DocumentPDF as $value) {
$binary = base64_decode($value->PDFBytes);
file_put_contents($xml->EnvelopeStatus->EnvelopeID . "/" . $value->Name . ".pdf", $binary);
}
So there you go.
i've no idea how to do that and need your help!
i have an array of filenames called $bundle. (file_one.jpg, file_two.pdf, file_three.etc)
and i have the name of the folder stored in $folder. (my_directory)
i now would like to move all the files stored in $bundle to move to the directory $folder.
how can i do that?
//print count($bundle); //(file_one.jpg, file_two.pdf, file_three.jpg)
$folder = $folder = PATH . '/' . my_directory;
foreach ($bundle as $value) {
//rename(PATH.'/'.$value, $folder . '/' . $value);
}
just so it's not confusing: PATH just stores the local file-path im using for my project. in my case it's just the folder i'm working in-so it's "files".
i have no idea which method i have to use for this and how i could solve that!
thank you for your help!
The code given by you should work with minor changes:
$folder = PATH . '/' . 'my_directory'; // enclose my_directory in quotes.
foreach ($bundle as $value) {
$old = PATH.'/'.$value, $folder;
$new = $folder . '/' . $value;
if(rename($old,$new) !== false) {
// renamed $old to $new
}else{
// rename failed.
}
}
$folder = PATH . '/' . $folder;
foreach ($bundle as $value) {
$old = PATH.'/'.$value;
$new = $folder . '/' . $value;
if(rename($old,$new) !== false) {
// renamed $old to $new
}else{
// rename failed.
}
}
Untested but should work:
function bulkMove($src, $dest) {
foreach(new GlobIterator($src) as $fileObject) {
if($fileObject->isFile()) {
rename(
$fileObject->getPathname(),
rtrim($dest, '\\/') . DIRECTORY_SEPARATOR . $fileObject->getBasename()
);
}
}
}
bulkMove('/path/to/folder/*', '/path/to/new/folder');
Could add some checks to see if the destination folder is writable. If you dont need wildcard matching, change the GlobIterator to DirectoryIterator. That would also eliminate the need for PHP5.3