I have folders that have one file in each.
I am trying to read the name of that file and pass to variable in php.
foreach(glob('photos/folder_name/thumbs') as $filename)
{echo $filename;}
Using the above script, no data is returned for $filename. Any thoughts?
Thanks.
You need to use wildcards in the string you give to glob.
<?php
foreach (glob("*.txt") as $filename) {
echo "$filename size " . filesize($filename) . "\n";
}
?>
Related
I have been trying to reach some files thru glob with the extension .vic thru php file_get_contents.
The problem I ran into is that it does not work.
My code:
<?php
$fileList = glob('*.vic');
echo $_SERVER['DOCUMENT_ROOT'] .'/server/main/';
foreach($fileList as $filename) {
echo file_get_contents($_SERVER['DOCUMENT_ROOT'].'/server/main/' . $filename) . "\r\n";
}
When I run it echo's the folder I need but the file content doesn't show up.
There is a .vic file in the folder with file contents.
Did I do something wrong or is this not posible
PHP Output:
C:/xampp/htdocs/server/main/
Try:
$fileList = glob($_SERVER['DOCUMENT_ROOT'] .'/server/main/*.vic');
I am reading a folder with glob() function in php
foreach (glob("folder/*.*") as $filename) {
echo $filename."<br />"
}
My folder contains three kind of files pdf, image, videos. What I need here is to make a link for each file and open them on click.
How can I achieve this, any ideas?
This should work for you:
(This creates you a list of all files which you can click on it and you get it opened in the new tab)
$files = glob("folder/*.*");
foreach($files as $file)
echo "<a href='" . $file . "' target='_blank'>" . $file . "</a><br />";
again , sorry for asking so many questions.
I currently have this code:
foreach (glob("black/*") as $filename)
problem is that it doesnt include folders inside the folder "black".
Any way to work that out?
Full code:
foreach (glob("black/*") as $filename)
if(ftp_put($conn, $ftpFolder . basename($filename) , $filename, FTP_BINARY)) {
and then goes the response,
Thanks in advance :)
P.S
My goal is that I want the script to upload whole bunch of files,folders and sub folders.
You can try a recursive solution:
function print_files($dir){
foreach (glob("$dir/*.txt") as $filename) {
echo "$filename size " . filesize($filename) . "\n";
}
// call recursively for every sub directory
foreach (glob('$dir/*', GLOB_ONLYDIR) as $subdirs) {
print_files($subdirs);
}
}
print_files("black");
Disclaimer:
It was written with Notepad - I can't test it...
I want to show my visitors the images in a folder and after then have seen it, I want all those files deleted!
This is what I tried, but It won't work. I think it's because PHP is generating a html file which tells the browser it must first get an image from a different place but the html file was already removed.
<?php
foreach (glob("files/*.*") as $prevpic) {
echo '<img src="' . $prevpic . '" />';
}
foreach (glob("files/*.*") as $file) {
unlink($file);
}
move_uploaded_file($_FILES["file"]["tmp_name"], "files/" . $_FILES["file"]["name"]);
?>
You can do something like so ...
<?php
foreach (glob("files/*.*") as $file) {
echo '<img src="data:image/' . pathinfo($file, PATHINFO_EXTENSION) . ';base64,' . base64_encode(file_get_contents($file)) . '" />';
unlink($file);
}
?>
... which is basically writing the image data into the html, and then discarding the image.
I would handle this by simply managing your images through a download script (php).
You track sessions and simply don't display the images requested, but fail gracefully with a response, or let your app handle it via session based tracking.
That way no images are deleted 'onview'.
im trying to do this :
$filename = "/destination/destination2/file_*";
" * " = anything
destination2 files :
file_somethingrandom
and since it $filename contains * so it should be selecting file_somethingrandom
how to do it?
Use the glob() function like this:
$filename = "/destination/destination2/file_*";
foreach (glob($filename) as $filefound)
{
echo "$filefound size " . filesize($filefound) . "\n";
}
Try this
foreach (glob("/destination/destination2/file_*") as $filename) {
echo $filename;
}
Cheers!
You can use different functions other than echo, but it does what it does, randomly picks a filename in a group of files inside "/destination/destination2/" whose name contains "file_".
<?php
$filea = glob('/destination/destination2/file_*');
echo $filea[rand(0,count($filea)-1];
?>