List files, read files as links, php? - javascript

This is a strange request I suppose, but I have a directory full of txt files. For example:
- david_smith_interview.txt -
- beth_martin_interview.txt -
- sally_smithart_interview.txt
The contents of these text files are a link to their interview in an mp3 format, for example, if you open the file david_smith_interview.txt, it is simply this:
http://www.interviews/employees/david_smith.mp3
All of the other text files follow the same format. They are simply links to their mp3 interview.
I am trying to use something like below to list the text files:
<?php
$directory = "/employees/";
$phpfiles = glob($directory . "*.txt");
foreach($phpfiles as $phpfile)
{
echo $phpfile; // This will list the files by name
// How can I output something to reflect this:
// david_smith_interview
}
?>
So I am asking is it possible that the text file can be "read" and used as the actual link?
Any thoughts?

Replace _interview.txt with .mp3
echo "" . str_replace(".txt", "", $phpfile). "\";

Since those are .txt files you can just read them one by one to a variable and then echo the result in a for-loop.
In pseudo:
$paths fetch_paths()
$urls = array();
foreach($paths as $path)
{
$url=fopen($path);
array_push($urls,fgets(url)); // Assuming there's only one link per file and it is on one line.
}
foreach($urls as $url)
{
echo <Your formatted link here>
}

Related

How to include a random PHP file from one folder together with a fitting JS and CSS file to one document? [duplicate]

I'm trying to make a site where users can submit photos, and then randomly view others photos one by one on another page. I have a directory called "uploads" where the pictures are submitted. I'm having trouble reading the pictures from the file. I just want to randomly select a picture from the directory uploads and have it displayed on the page. Any suggestions appreciated.
You can use glob to get all files in a directory, and then take a random element from that array. A function like this would do it for you:
function random_pic($dir = 'uploads')
{
$files = glob($dir . '/*.*');
$file = array_rand($files);
return $files[$file];
}
I've turned it a little to get more than one random file from a directory using array.
<?php
function random_pic($dir)
{
$files = glob($dir . '/*.jpg');
$rand_keys = array_rand($files, 3);
return array($files[$rand_keys[0]], $files[$rand_keys[1]], $files[$rand_keys[2]]);
}
// Calling function
list($file_1,$file_2,$file_3)= random_pic("images");
?>
You can also use loop to get values.
This single line of code displays one random image from the target directory.
<img src="/images/image_<?php $random = rand(1,127); echo $random; ?>.png" />
Target directory: /images/
Image prefix: image_
Number of images in directory: 127
https://perishablepress.com/drop-dead-easy-random-images-via-php/
Drawbacks
images must be named sequentially (eg image_1.png, image_2.png, image_3.png, etc).
you need to know how many images are in the directory in advance.
Alternatives
Perhaps there's a simple way to make this work with arbitrary image-names and file-count, so you don't have to rename or count your files.
Untested ideas:
<img src=<?php $dir='/images/'; echo $dir . array_rand(glob($dir . '*.jpg')); ?> />
shuffle()
scanDir() with rand(1,scanDir.length)
Or you can use opendir() instead of glob() because it's faster

Recursively generate script tags for all JavaScript files within a directory with PHP

I'm building a complex app with lots of JavaScript files in lots of sub-directories. I know I want to include them all (it won't affect performance), but I don't want to manually create a script tag for each. Given that all of my files are children of a "/js" directory, how could I dynamically generate the script tags for each with PHP? Something like this:
// first somehow recursively get all .js files, then:
foreach($files as $file) {
echo '<script src="' . $file->path . '"></script>';
}
Most elegant way is to use SPL in my opinion.
$dirIterator = new RecursiveDirectoryIterator("/path/to/js");
$iterator = new RecursiveIteratorIterator(
$dirIterator,
RecursiveIteratorIterator::SELF_FIRST
);
foreach ($iterator as $file) {
if($file->getExtension() == 'js') {
// You probably have to adjust the full path according to your DOC_ROOT
$url = $file->getPathname();
echo '<script src="' . $url . '"></script>';
}
}
Have a look at http://php.net/manual/en/class.splfileinfo.php to see what else you can do with $file .

write a file on local disk from web app [duplicate]

I am trying to create and save a file to the root directory of my site, but I don't know where its creating the file as I cannot see any. And, I need the file to be overwritten every time, if possible.
Here is my code:
$content = "some text here";
$fp = fopen("myText.txt","wb");
fwrite($fp,$content);
fclose($fp);
How can I set it to save on the root?
It's creating the file in the same directory as your script. Try this instead.
$content = "some text here";
$fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/myText.txt","wb");
fwrite($fp,$content);
fclose($fp);
If you are running PHP on Apache then you can use the enviroment variable called DOCUMENT_ROOT. This means that the path is dynamic, and can be moved between servers without messing about with the code.
<?php
$fileLocation = getenv("DOCUMENT_ROOT") . "/myfile.txt";
$file = fopen($fileLocation,"w");
$content = "Your text here";
fwrite($file,$content);
fclose($file);
?>
This question has been asked years ago but here is a modern approach using PHP5 or newer versions.
$filename = 'myfile.txt'
if(!file_put_contents($filename, 'Some text here')){
// overwriting the file failed (permission problem maybe), debug or log here
}
If the file doesn't exist in that directory it will be created, otherwise it will be overwritten unless FILE_APPEND flag is set.
file_put_contents is a built in function that has been available since PHP5.
Documentation for file_put_contents
fopen() will open a resource in the same directory as the file executing the command. In other words, if you're just running the file ~/test.php, your script will create ~/myText.txt.
This can get a little confusing if you're using any URL rewriting (such as in an MVC framework) as it will likely create the new file in whatever the directory contains the root index.php file.
Also, you must have correct permissions set and may want to test before writing to the file. The following would help you debug:
$fp = fopen("myText.txt","wb");
if( $fp == false ){
//do debugging or logging here
}else{
fwrite($fp,$content);
fclose($fp);
}

automatically printing using php

I have pdf files coming in a folder after some duration. I want to print them using php automatically by default printer and after printing those files move to other folder. How can I do this? I am using the code given below but it's not printing anything..Please help..
$printer = "\\\\server\printer";
if($ph = printer_open($printer))
{
$fh = file_get_contents('455.pdf');
printer_set_option($ph, PRINTER_MODE, "RAW");
printer_write($ph, $fh);
printer_close($ph);
}
else
echo "Can't connect to printer";
Here https://www.daniweb.com/web-development/php/threads/449709/php-print-pdf-directly-to-a-printer i found the solution of this issue . i hope it will work for you and use rename('foo/test.php', 'bar/test.php'); for the transfer of all files from one folder to another after printing .

Convert contents of an HTML file to a string

I'd like to know how to input a .html file to a browser, which then takes the contents of said .html file and converts it into one big string so that I can pass it into a JavaScript function to parse it. As I understand it, HTML5 implemented a file API but I'm not entirely sure if it's capable of doing what I want, or how to really use it for all that matter.
You can use the following :
$filename = "yourhtmlfile.html";
$handle = fopen($filename, "r");
// Retrieve the content of HTML file, and stocks it into $contents var
$contents = fread($handle, filesize($filename));
fclose($handle);

Categories

Resources