How to read and write images and video with PHP? [closed] - php

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 4 years ago.
Improve this question
I am creating a downloadable zip file. In that zip, I want to add php, html, and images and videos.
All of them are in other folders.
I can read and write PHP and HTML files from other files/folders.
But I don't know how to read and write (or move from one folder to anther folder) images and videos.

Please try this code
function move_files($dir)
{
if(is_dir($dir))
{
if($handle = opendir($dir))
{
while(($file = readdir($handle)) !== false)
{
if($file != "." && $file != ".." && $file != "Thumbs.db"/*pesky windows, images..*/)
{
$sourcefile = $dir.$file;
$destinationfile = 'Your New Location';
if (!copy($sourcefile, $destinationfile)) {
echo "failed to copy $file...\n";
}
}
}
closedir($handle);
}
}
}
move_files("folder/");

I you want to move files, see PHP manual: copy, unlink

Related

Is there any way to add a line of code to all .html files? [closed]

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.
}
}
?>

Create JSON file with folder contents (images) [closed]

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('.')
]);

Build an array from .INI file using PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I've my .ini file as follows
languages.ini
--------------------
fr = "French"
en = "English"
Now, I'd like to read the file and keep the keys and values in the form of an array using PHP. I'm very new to PHP. I hope any one help me in achieving this. This is my following code.
public function GetLanguages()
{
$langdir = ISC_BASE_PATH.'/language';
$skip = Array (
'.',
'..',
'CVS',
'.svn',
);
$langs1 = array();
$dh = opendir($langdir);
while (($file = readdir($dh)) !== false) {
if (!is_file($langdir.'/'.$file.'/languages.ini')) {
continue;
}
$langs1[] = $file;
}
echo "FileLL:".$file;
foreach ($langs1 as $key) {
echo "Store languagesss::".$key."<br>";
}
return $langs1;
}
Thank you in advance.
You are looking for PHP's ini parse function:
parse_ini_file
Just call $arrayResult = parse_ini_file($path_to_file)

PHP function to remove a directory and all its sub content. [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
function rrmdir($dir) {
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object);
}
}
reset($objects);
rmdir($dir);
}
}
I have this function to delete a directory and all its contents (sub directories and sub files).
This function works great and can delete over 5k files in just a second or two.
But does anyone have any suggestions on optimizing this function?
Also ... if anyone has any "system" or method to securely host custom php functions on one server and call them on other servers let me know... that would be awesome as I have a huge collection of functions and I work off of 3 servers and would love to have them all in one location. I use cPanel's global prepend to include all my functions in all my php files easily and that works extremely well BUT if there was a way to simply call a remotely hosted PHP file into the prepend file that is included in each file on the server that would be superb... Any suggestions for a similar setup would be awesome.
while loop should be faster then foreach, also order of if statement does matter.
function removeDirectory($path)
{
$path = rtrim($path, '/').'/';
$files = scandir($path);
$i = count($files);
while (--$i) {
$file = $files[$i];
$fullpath = $path.$file;
is_file($fullpath)
? #unlink($fullpath)
: ($file != '.' and $file != '..' and removeDirectory($fullpath));
}
rmdir($path);
return true;
}
if you are allowed to to do shell execution you can execute the simple linux function
$out = shell_exec('rm -rf /path/to/directory');
and you can do
var_dump($out);
to check out the result if it was removed or not.

How can I create a search form that searches files in a folder? [closed]

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 2 years ago.
Improve this question
I have a search feature on my website. I would like it to search through a certain folder for files on my server and display results from there. I'd rather not use databases.
Is there a way to do this?
<?php
$dir = "/your_folder_here/";
// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if($file == $_POST['SEARCHBOX_INPUT']){
echo(''. $file .''."\n");
}
}
closedir($dh);
}
}
?>
From php.net mostly. Obviously change your file path. Also change the $_POST command in the middle to whatever the name of your search box input is. This will only find exact matches of your search. Some changes could be made to find close matches.
$dir = "/etc/php5/";
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if($file == 'songs1.php')
//ur code here ...
}
closedir($dh);
}
}
I got this from PHP.net. I hope this helps you.
This is how I did it, although it displays all the files which are in that directory and it does not give a brief description of each file. I dont know if you can help modify it.
<?php
print "<h2>Showing results for $search</h2>";
$dirName="MYBOOKS";
$dp=opendir($dirName);
chdir($dirName);
while ($currentFile !== false) {
$currentFile = readDir($dp);
$theFiles[] = $currentFile;
}
$BookFiles= preg_grep("/pdf$|gif$|png$|jpg$|jed$/", $theFiles);
$output="";
foreach ($BookFiles as $currentFile) {
$output .= <<< Here
<ul>
<li><a href=MYBOOKS/$currentFile>$currentFile</a></li>
</ul>
Here;
}
$fp=fopen("BookIndex.htm","w");
fputs ($fp,$output);
fclose($fp);
readfile ("BookIndex.htm");
?>

Categories