Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have several ziped files in a folder. I would like to unzip them to a specified folder. I have the following php code:
$path = "docs/" . $ID;
$files = scandir("temp" . '/' . $ID );
foreach ($files as $athely){
$zip = new ZipArchive;
$res = $zip->open($athely);
if ($res === TRUE) {
// extract it to the path we determined above
$zip->extractTo($path);
$zip->close();
echo "WOOT! $file extracted to $path";
} else {
echo "Doh! I couldn't open $athely";
}
}
It is not working. What am I doing wrong?
The problem was you are not using full path for opening the zip. Another thing to notice is if more than one zip file have folder with same name then one folder will overwrite the other.
<?php
$path = "docs/" . $ID;
$files = scandir("temp" . '/' . $ID );
foreach ($files as $athely){
if($athely=="." || $athely=="..") continue;
$target_path = "temp/".$ID."/".$athely;
$file = $athely;
$zip = new ZipArchive;
$res = $zip->open($target_path);
if ($res === TRUE) {
$zip->extractTo($path);
$zip->close();
echo "WOOT! $file extracted to $path";
} else {
echo "Doh! I couldn't open $athely";
}
}
?>
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 18 days ago.
Improve this question
I have 100 html files, and i want to add a line of code (at the end) to all of them.
How is this possible ?
Something like a script which appends that line into all html files
I tried searching but did not find anything.
You can use :
<?php
$folder = '/path/to/html/files';
$line_to_add = '<p>your line/p>';
foreach (glob("$folder/*.html") as $file) {
$contents = file_get_contents($file);
file_put_contents($file, $line_to_add . PHP_EOL . $contents);
}
Your question is quite unclear, because you didn't specify where you are going to append this lane? Here is a starting point:
<?php
$path = '/path/to/html/files';
$code_to_add = '<p>This line was added by a script.</p>';
$files = scandir($path); // get an array of all the files in the directory specified by $path.
foreach ($files as $file) { // Iterate over files
if (substr($file, -5) == '.html') { // hecking if each file has a ".html" extension.
$file_path = $path . '/' . $file;
$file_contents = file_get_contents($file_path); // read the contents
file_put_contents($file_path, $file_contents . $code_to_add); // put new content, you can modify it.
}
}
?>
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am moving images from xcode to an online platform and i need to generate a JSON file from a directory structure (images only) that lists the folders, and the images in those folders, with complete URL (online) AND date they where added.
I am a COMPLETE noob with PHP web stuff, so am really lost at the moment.
I found the following code, but that does not do anything so far, and does not travels into directory's.
<?php
/*
JSON list of all images files in current directory
*/
$dir = ".";
$dh = opendir($dir);
$image_files = array();
while (($file = readdir($dh)) !== false) {
$match = preg_match("/.*\.(jpg|png|gif|jpeg|bmp)/", $file);
if ($match) {
$image_files []= $file;
}
}
echo json_encode($image_files);
closedir($dh);
?>
The following code works for me. Found it here.
I've only modified the echo, added the regex and the time. I hope this answers your question.
<?php
function getDirContents($path) {
$rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
$files = array();
foreach ($rii as $file) {
if (!$file->isDir() && preg_match("/.*\.(jpg|png|gif|jpeg|bmp)/", $file)) {
$files[] = [
'path' => $file->getPathname(),
'c_time' => $file->getCTime()
];
}
}
return $files;
}
header('Content-Type: application/json');
echo json_encode([
'success' => true,
'files' => getDirContents('.')
]);
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have a question, now all my video files are in /videos now I want to move all my files in /videos/2014/05 or /videos/2014/06...in my database I have a field call date(when the video
was uploaded) now how to create this script to make folders /2014/05 and moved all videos there?
I tried an example but not received.
public function move()
{
$today_folders = date('Y') .'/'. date('m'). '/' ;
if ( !file_exists( $this->config->item("multimedia_path") . 'images/'. $today_folders) ){
$old_umask = umask(0);
mkdir( $this->config->item("multimedia_path") . 'videos/'. $today_folders, 0777, true );
umask($old_umask);
$this->load->database();
$articles=$this->db->query("SELECT *FROM videos ORDER BY date DESC");
$source = "videos/";
$destination = "videos/".$today_folders;
foreach ($articles as $file) {
if (in_array($file, array(".",".."))) continue;
if (copy($source.$file, $destination.$file)) {
$delete[] = $source.$file;
}
}
foreach ($delete as $file) {
unlink($file);
}
}
public function move()
{
if ( !file_exists( $this->config->item("multimedia_path") . 'images/'. $today_folders) ){
$old_umask = umask(0);
$this->load->database();
// Assuming mp4_video is your file name
$videos=$this->db->query("SELECT mp4_video, date FROM videos ORDER BY date DESC");
$source = "videos/";
foreach ($videos as $video) {
// Load the destination for the video based on the date it was uploaded, assuming your DB wrapper converts date into a DateTime object, if not let me know it's type(eg int because it's a timestamp, or string because it's a mysql datetime, or whatever)
$destination = $this->config->item("multimedia_path") . 'videos' . $video['date']->format('/Y/m/d/');
// Again, assuming mp4_video is your filename
$file = $video['mp4_video'];
mkdir( $destination, 0777, true);
if (copy($source.$file, $destination.$file)) {
$delete[] = $source.$file;
}
}
umask($old_umask);
foreach ($delete as $file) {
unlink($file);
}
}
I have this code to read a file for preview, but the downside is I have to download the file first from cloud and read from it, but it's a waste of space so I want to delete it after viewing a certain file. Is there an automatic way of doing this? Or do I have to integrate it to a close button?
// Get the container we want to use
$container = $conn->get_container('mailtemplate');
//$filename = 'template1.zip';
// upload file to Rackspace
$object = $container->get_object($filename);
//var_dump($object);
//echo '<pre>' . print_r($object,true) . '</pre>';
$localfile = $dir.$filename;
//echo $localfile;
$object->save_to_filename($localfile);
if($_GET['preview'] == "true")
{
$dir = "../mailtemplates/";
$file1 = $_GET['tfilename'];
$file = $dir.$file1;
$file2 = "index.html";
$info = pathinfo($file);
$file_name = basename($file,'.'.$info['extension']);
$path = $file_name.'/'.$file2;
$zip = new ZipArchive();
$zip->open($file);
$fp = $zip->getStream($path);
if(!$fp)
{
exit("faileds\n");
$zip->close();
unlink($dir.$filename);
}
else
{
$stuff = stream_get_contents($fp);
echo $stuff;
$zip->close();
if($stuff != null)
{
unlink($dir.$filename);
}
}
}
else
{
unlink($dir.$filename);
}
You didn't google this did ya?
Try Unlink
Edit:
Taking a look at this code, $zip->open($file); <-- is where you open the file. The file variable is set by:
"../mailtemplates/" . basename($_GET['tfilename'], '.' . $info['extension']) . '/' . "index.html"
So you're grabbing a relative directory and grabbing a filename as a folder, and going to that folder /index.html. Here's an example:
if you're in c:\ testing and you go to ../mailtemplates/ you'll be in c:\mailtemplates and then you're looking at file test.php but you're removing the file extension, so you'll be opening the location c:\mailtemplates\test\index.html so you open up that html file and read it. Then, you're trying to delete c:\mailtemplates\test.php
can you explain how any of that makes sense to you? 'cause that seems very odd to me.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I want to read a list the names of files in a folder in a web page using php.
is there any simple script to acheive it?
The simplest and most fun way (imo) is glob
foreach (glob("*.*") as $filename) {
echo $filename."<br />";
}
But the standard way is to use the directory functions.
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
echo "filename: .".$file."<br />";
}
closedir($dh);
}
}
There are also the SPL DirectoryIterator methods. If you are interested
This is what I like to do:
$files = array_values(array_filter(scandir($path), function($file) use ($path) {
return !is_dir($path . '/' . $file);
}));
foreach($files as $file){
echo $file;
}
There is this function scandir():
$dir = 'dir';
$files = scandir($dir, 0);
for($i = 2; $i < count($files); $i++)
print $files[$i]."<br>";
More here in the php.net manual
If you have problems with accessing to the path, maybe you need to put this:
$root = $_SERVER['DOCUMENT_ROOT'];
$path = "/cv/";
// Open the folder
$dir_handle = #opendir($root . $path) or die("Unable to open $path");
There is a glob. In this webpage there are good article how to list files in very simple way:
How to read a list of files from a folder using PHP
You can use standard directory functions
$dir = opendir('/tmp');
while ($file = readdir($dir)) {
if ($file == '.' || $file == '..') {
continue;
}
echo $file;
}
closedir($dir);
Check in many folders :
Folder_1 and folder_2 are name of folders, from which we have to select files.
$format is required format.
<?php
$arr = array("folder_1","folder_2");
$format = ".csv";
for($x=0;$x<count($arr);$x++){
$mm = $arr[$x];
foreach (glob("$mm/*$format") as $filename) {
echo "$filename size " . filesize($filename) . "<br>";
}
}
?>
There is also a really simple way to do this with the help of the RecursiveTreeIterator class, answered here: https://stackoverflow.com/a/37548504/2032235
<html>
<head>
<title>Names</title>
</head>
<body style="background-color:powderblue;">
<form method='post' action='alex.php'>
<input type='text' name='name'>
<input type='submit' value='name'>
</form>
Enter Name:
<?php
if($_POST)
{
$Name = $_POST['name'];
$count = 0;
$fh=fopen("alex.txt",'a+') or die("failed to create");
while(!feof($fh))
{
$line = chop(fgets($fh));
if($line==$Name && $line!="")
$count=1;
}
if($count==0 && $Name!="")
{
fwrite($fh, "\r\n$Name");
}
else if($count!=0 && $line!="")
{
echo '<font color="red">'.$Name.', the name you entered is already in the list.</font><br><br>';
}
$count=0;
fseek($fh, 0);
while(!feof($fh))
{
$a = chop(fgets($fh));
echo $a.'<br>';
$count++;
}
if($count<=1)
echo '<br>There are no names in the list<br>';
fclose($fh);
}
?>
</body>
</html>