Directory Listing in PHP
I have a php function that allows me to delete images from a certain directory. Now my problem is that in my code, i can see the index.php file listed, and i only want to show the images under it. Here is my full code:
$fid= $_POST['fid'];
if (("submit")&&($fid != "")) {
foreach($fid as $rfn) {
$remove = "$dir/$rfn";
unlink($remove);
}
}
$handle=opendir($dir);
while (($file = readdir($handle))!== false){
if ($file != "." && $file != "..") {
$size = filesize("$dir/$file");
$list .= '<div class="col-md-3 text-center" style="margin-top:20px;">';
$list .= '<img src="../inc/img/galeria/'.$file.'" class="rounded" width="100%" height="250px">';
$list .= '<br><br>';
$list .= '<input type="checkbox" class="form-control" name="fid" value="'.$file.'">';
$list .= '</div>';
}
}
closedir($handle);
echo $list;
Now this code works just fine, the problem is it lists everything inside the directory and i want to show only the jpg, jpeg, gif or png files inside of that directory. Thanks in advance guys.
php directory-listing
|
show 2 more comments
I have a php function that allows me to delete images from a certain directory. Now my problem is that in my code, i can see the index.php file listed, and i only want to show the images under it. Here is my full code:
$fid= $_POST['fid'];
if (("submit")&&($fid != "")) {
foreach($fid as $rfn) {
$remove = "$dir/$rfn";
unlink($remove);
}
}
$handle=opendir($dir);
while (($file = readdir($handle))!== false){
if ($file != "." && $file != "..") {
$size = filesize("$dir/$file");
$list .= '<div class="col-md-3 text-center" style="margin-top:20px;">';
$list .= '<img src="../inc/img/galeria/'.$file.'" class="rounded" width="100%" height="250px">';
$list .= '<br><br>';
$list .= '<input type="checkbox" class="form-control" name="fid" value="'.$file.'">';
$list .= '</div>';
}
}
closedir($handle);
echo $list;
Now this code works just fine, the problem is it lists everything inside the directory and i want to show only the jpg, jpeg, gif or png files inside of that directory. Thanks in advance guys.
php directory-listing
what is this firstiffeatures("submit")string for?
– Marcin Orlowski
Nov 24 '18 at 0:55
I use it to submit a form to delete the image that is listed
– Pedro Carvalho
Nov 24 '18 at 0:57
1
if ("submit")makes no sense. It's always TRUE
– Marcin Orlowski
Nov 24 '18 at 0:59
You are right about that, i will fix that and remove it, do you have any idea how can i fix the listing to a specific type or types?
– Pedro Carvalho
Nov 24 '18 at 1:02
1
stackoverflow.com/questions/3226519/…
– Pirate of Marmara
Nov 24 '18 at 1:05
|
show 2 more comments
I have a php function that allows me to delete images from a certain directory. Now my problem is that in my code, i can see the index.php file listed, and i only want to show the images under it. Here is my full code:
$fid= $_POST['fid'];
if (("submit")&&($fid != "")) {
foreach($fid as $rfn) {
$remove = "$dir/$rfn";
unlink($remove);
}
}
$handle=opendir($dir);
while (($file = readdir($handle))!== false){
if ($file != "." && $file != "..") {
$size = filesize("$dir/$file");
$list .= '<div class="col-md-3 text-center" style="margin-top:20px;">';
$list .= '<img src="../inc/img/galeria/'.$file.'" class="rounded" width="100%" height="250px">';
$list .= '<br><br>';
$list .= '<input type="checkbox" class="form-control" name="fid" value="'.$file.'">';
$list .= '</div>';
}
}
closedir($handle);
echo $list;
Now this code works just fine, the problem is it lists everything inside the directory and i want to show only the jpg, jpeg, gif or png files inside of that directory. Thanks in advance guys.
php directory-listing
I have a php function that allows me to delete images from a certain directory. Now my problem is that in my code, i can see the index.php file listed, and i only want to show the images under it. Here is my full code:
$fid= $_POST['fid'];
if (("submit")&&($fid != "")) {
foreach($fid as $rfn) {
$remove = "$dir/$rfn";
unlink($remove);
}
}
$handle=opendir($dir);
while (($file = readdir($handle))!== false){
if ($file != "." && $file != "..") {
$size = filesize("$dir/$file");
$list .= '<div class="col-md-3 text-center" style="margin-top:20px;">';
$list .= '<img src="../inc/img/galeria/'.$file.'" class="rounded" width="100%" height="250px">';
$list .= '<br><br>';
$list .= '<input type="checkbox" class="form-control" name="fid" value="'.$file.'">';
$list .= '</div>';
}
}
closedir($handle);
echo $list;
Now this code works just fine, the problem is it lists everything inside the directory and i want to show only the jpg, jpeg, gif or png files inside of that directory. Thanks in advance guys.
php directory-listing
php directory-listing
asked Nov 24 '18 at 0:52
Pedro CarvalhoPedro Carvalho
488
488
what is this firstiffeatures("submit")string for?
– Marcin Orlowski
Nov 24 '18 at 0:55
I use it to submit a form to delete the image that is listed
– Pedro Carvalho
Nov 24 '18 at 0:57
1
if ("submit")makes no sense. It's always TRUE
– Marcin Orlowski
Nov 24 '18 at 0:59
You are right about that, i will fix that and remove it, do you have any idea how can i fix the listing to a specific type or types?
– Pedro Carvalho
Nov 24 '18 at 1:02
1
stackoverflow.com/questions/3226519/…
– Pirate of Marmara
Nov 24 '18 at 1:05
|
show 2 more comments
what is this firstiffeatures("submit")string for?
– Marcin Orlowski
Nov 24 '18 at 0:55
I use it to submit a form to delete the image that is listed
– Pedro Carvalho
Nov 24 '18 at 0:57
1
if ("submit")makes no sense. It's always TRUE
– Marcin Orlowski
Nov 24 '18 at 0:59
You are right about that, i will fix that and remove it, do you have any idea how can i fix the listing to a specific type or types?
– Pedro Carvalho
Nov 24 '18 at 1:02
1
stackoverflow.com/questions/3226519/…
– Pirate of Marmara
Nov 24 '18 at 1:05
what is this first
if features ("submit") string for?– Marcin Orlowski
Nov 24 '18 at 0:55
what is this first
if features ("submit") string for?– Marcin Orlowski
Nov 24 '18 at 0:55
I use it to submit a form to delete the image that is listed
– Pedro Carvalho
Nov 24 '18 at 0:57
I use it to submit a form to delete the image that is listed
– Pedro Carvalho
Nov 24 '18 at 0:57
1
1
if ("submit") makes no sense. It's always TRUE– Marcin Orlowski
Nov 24 '18 at 0:59
if ("submit") makes no sense. It's always TRUE– Marcin Orlowski
Nov 24 '18 at 0:59
You are right about that, i will fix that and remove it, do you have any idea how can i fix the listing to a specific type or types?
– Pedro Carvalho
Nov 24 '18 at 1:02
You are right about that, i will fix that and remove it, do you have any idea how can i fix the listing to a specific type or types?
– Pedro Carvalho
Nov 24 '18 at 1:02
1
1
stackoverflow.com/questions/3226519/…
– Pirate of Marmara
Nov 24 '18 at 1:05
stackoverflow.com/questions/3226519/…
– Pirate of Marmara
Nov 24 '18 at 1:05
|
show 2 more comments
3 Answers
3
active
oldest
votes
This is how to scan a dir and only process certain files. Adapt for your use:
$handle=opendir($dir);
while ( ($file = readdir($handle)) !== false ) {
$ext = pathinfo($file, PATHINFO_EXTENSION);
if ( in_array($file, ['.', '..']) || ! in_array($ext, ['jpeg', 'jpg', 'gif', 'png']) ) {
continue;
}
// Do something with file
}
add a comment |
This fixes the issue, thank you guys for the tips!
$images = glob('/tmp/*.{jpeg,gif,png}', GLOB_BRACE);
And also:
// image extensions
$extensions = array('jpg', 'jpeg', 'png', 'gif', 'bmp');
// init result
$result = array();
// directory to scan
$directory = new DirectoryIterator('/dir/to/scan/');
// iterate
foreach ($directory as $fileinfo) {
// must be a file
if ($fileinfo->isFile()) {
// file extension
$extension = strtolower(pathinfo($fileinfo->getFilename(), PATHINFO_EXTENSION));
// check if extension match
if (in_array($extension, $extensions)) {
// add to result
$result = $fileinfo->getFilename();
}
}
}
// print result
print_r($result);
This one is even better and i have managed to make it work, shouts to @pirateofmarmara to guide me on this.
add a comment |
You can try the following. I hope there is no syntax error. I did not run it.
foreach(glob($dir . "/*.{jpg,jpeg,png}", GLOB_BRACE) as $file) {
// echo $file;
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53454286%2fdirectory-listing-in-php%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is how to scan a dir and only process certain files. Adapt for your use:
$handle=opendir($dir);
while ( ($file = readdir($handle)) !== false ) {
$ext = pathinfo($file, PATHINFO_EXTENSION);
if ( in_array($file, ['.', '..']) || ! in_array($ext, ['jpeg', 'jpg', 'gif', 'png']) ) {
continue;
}
// Do something with file
}
add a comment |
This is how to scan a dir and only process certain files. Adapt for your use:
$handle=opendir($dir);
while ( ($file = readdir($handle)) !== false ) {
$ext = pathinfo($file, PATHINFO_EXTENSION);
if ( in_array($file, ['.', '..']) || ! in_array($ext, ['jpeg', 'jpg', 'gif', 'png']) ) {
continue;
}
// Do something with file
}
add a comment |
This is how to scan a dir and only process certain files. Adapt for your use:
$handle=opendir($dir);
while ( ($file = readdir($handle)) !== false ) {
$ext = pathinfo($file, PATHINFO_EXTENSION);
if ( in_array($file, ['.', '..']) || ! in_array($ext, ['jpeg', 'jpg', 'gif', 'png']) ) {
continue;
}
// Do something with file
}
This is how to scan a dir and only process certain files. Adapt for your use:
$handle=opendir($dir);
while ( ($file = readdir($handle)) !== false ) {
$ext = pathinfo($file, PATHINFO_EXTENSION);
if ( in_array($file, ['.', '..']) || ! in_array($ext, ['jpeg', 'jpg', 'gif', 'png']) ) {
continue;
}
// Do something with file
}
answered Nov 24 '18 at 1:10
ryantxrryantxr
2,6691520
2,6691520
add a comment |
add a comment |
This fixes the issue, thank you guys for the tips!
$images = glob('/tmp/*.{jpeg,gif,png}', GLOB_BRACE);
And also:
// image extensions
$extensions = array('jpg', 'jpeg', 'png', 'gif', 'bmp');
// init result
$result = array();
// directory to scan
$directory = new DirectoryIterator('/dir/to/scan/');
// iterate
foreach ($directory as $fileinfo) {
// must be a file
if ($fileinfo->isFile()) {
// file extension
$extension = strtolower(pathinfo($fileinfo->getFilename(), PATHINFO_EXTENSION));
// check if extension match
if (in_array($extension, $extensions)) {
// add to result
$result = $fileinfo->getFilename();
}
}
}
// print result
print_r($result);
This one is even better and i have managed to make it work, shouts to @pirateofmarmara to guide me on this.
add a comment |
This fixes the issue, thank you guys for the tips!
$images = glob('/tmp/*.{jpeg,gif,png}', GLOB_BRACE);
And also:
// image extensions
$extensions = array('jpg', 'jpeg', 'png', 'gif', 'bmp');
// init result
$result = array();
// directory to scan
$directory = new DirectoryIterator('/dir/to/scan/');
// iterate
foreach ($directory as $fileinfo) {
// must be a file
if ($fileinfo->isFile()) {
// file extension
$extension = strtolower(pathinfo($fileinfo->getFilename(), PATHINFO_EXTENSION));
// check if extension match
if (in_array($extension, $extensions)) {
// add to result
$result = $fileinfo->getFilename();
}
}
}
// print result
print_r($result);
This one is even better and i have managed to make it work, shouts to @pirateofmarmara to guide me on this.
add a comment |
This fixes the issue, thank you guys for the tips!
$images = glob('/tmp/*.{jpeg,gif,png}', GLOB_BRACE);
And also:
// image extensions
$extensions = array('jpg', 'jpeg', 'png', 'gif', 'bmp');
// init result
$result = array();
// directory to scan
$directory = new DirectoryIterator('/dir/to/scan/');
// iterate
foreach ($directory as $fileinfo) {
// must be a file
if ($fileinfo->isFile()) {
// file extension
$extension = strtolower(pathinfo($fileinfo->getFilename(), PATHINFO_EXTENSION));
// check if extension match
if (in_array($extension, $extensions)) {
// add to result
$result = $fileinfo->getFilename();
}
}
}
// print result
print_r($result);
This one is even better and i have managed to make it work, shouts to @pirateofmarmara to guide me on this.
This fixes the issue, thank you guys for the tips!
$images = glob('/tmp/*.{jpeg,gif,png}', GLOB_BRACE);
And also:
// image extensions
$extensions = array('jpg', 'jpeg', 'png', 'gif', 'bmp');
// init result
$result = array();
// directory to scan
$directory = new DirectoryIterator('/dir/to/scan/');
// iterate
foreach ($directory as $fileinfo) {
// must be a file
if ($fileinfo->isFile()) {
// file extension
$extension = strtolower(pathinfo($fileinfo->getFilename(), PATHINFO_EXTENSION));
// check if extension match
if (in_array($extension, $extensions)) {
// add to result
$result = $fileinfo->getFilename();
}
}
}
// print result
print_r($result);
This one is even better and i have managed to make it work, shouts to @pirateofmarmara to guide me on this.
answered Nov 24 '18 at 1:11
Pedro CarvalhoPedro Carvalho
488
488
add a comment |
add a comment |
You can try the following. I hope there is no syntax error. I did not run it.
foreach(glob($dir . "/*.{jpg,jpeg,png}", GLOB_BRACE) as $file) {
// echo $file;
}
add a comment |
You can try the following. I hope there is no syntax error. I did not run it.
foreach(glob($dir . "/*.{jpg,jpeg,png}", GLOB_BRACE) as $file) {
// echo $file;
}
add a comment |
You can try the following. I hope there is no syntax error. I did not run it.
foreach(glob($dir . "/*.{jpg,jpeg,png}", GLOB_BRACE) as $file) {
// echo $file;
}
You can try the following. I hope there is no syntax error. I did not run it.
foreach(glob($dir . "/*.{jpg,jpeg,png}", GLOB_BRACE) as $file) {
// echo $file;
}
answered Nov 24 '18 at 1:20
koalabruderkoalabruder
1,42552434
1,42552434
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53454286%2fdirectory-listing-in-php%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
what is this first
iffeatures("submit")string for?– Marcin Orlowski
Nov 24 '18 at 0:55
I use it to submit a form to delete the image that is listed
– Pedro Carvalho
Nov 24 '18 at 0:57
1
if ("submit")makes no sense. It's always TRUE– Marcin Orlowski
Nov 24 '18 at 0:59
You are right about that, i will fix that and remove it, do you have any idea how can i fix the listing to a specific type or types?
– Pedro Carvalho
Nov 24 '18 at 1:02
1
stackoverflow.com/questions/3226519/…
– Pirate of Marmara
Nov 24 '18 at 1:05