Current location: Hot Scripts Forums » Programming Languages » PHP » file order problem


file order problem

Reply
  #1 (permalink)  
Old 06-01-09, 04:06 PM
knolly knolly is offline
Newbie Coder
 
Join Date: Apr 2009
Posts: 14
Thanks: 2
Thanked 0 Times in 0 Posts
file order problem

Having the weirdest problem. I'm using this script:

<?php
$g = array_key_exists ('g', $_GET) ?
$_GET['g'] : p1;
$selected = $g;

//Open images directory
$dir = dir($g);

//List files in images directory
while (($file = $dir->read()) !== false)
{
$ext = substr(strrchr($file, '.'), 1);

if ($ext==jpg)echo "<img src=\"$g/" . $file . "\" alt=\"\" />";
}


$dir->close();
?>

Script works fine and when I upload the images to the server myself they display correctly (in numerical order). But when the owner of the site uploads pictures from her computer they display on the site in backwards order! I have tried everything to make sure she's doing things the same way I am but this keeps happening. Ever heard of this before? What could possibly cause it?

Thanks!
Reply With Quote
  #2 (permalink)  
Old 06-02-09, 04:58 AM
=OTS=G-Man =OTS=G-Man is offline
Newbie Coder
 
Join Date: Jun 2009
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
One thing I can think of that I've always done when displaying a folder list is to read the folder into an array first then sort the array, then loop through the array for displaying. Hope that helps

Also, watch your code, I don't see any protection on your dir() command, someone could pass anything into the 'g' var in the URL and open any directory on your system.

Last edited by =OTS=G-Man; 06-02-09 at 05:01 AM.
Reply With Quote
  #3 (permalink)  
Old 06-02-09, 02:57 PM
knolly knolly is offline
Newbie Coder
 
Join Date: Apr 2009
Posts: 14
Thanks: 2
Thanked 0 Times in 0 Posts
oh, thanks for the tip. how would i protect the dir command? and what harm could they do by opening a directory?

thanks!
Reply With Quote
  #4 (permalink)  
Old 06-04-09, 03:43 AM
=OTS=G-Man =OTS=G-Man is offline
Newbie Coder
 
Join Date: Jun 2009
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
Ive don a check to see if the realpath() is in the area I allow, using something like

stristr(realpath($_GET['folder']), '/var/www/public/') === FALSE)
{
//exit because they are accessing a folder outside my public area
}

the problem with allowing people to browse your drive is they could then view your personal information or view password files by passing in folders like "./../../etc/" that would then allow them to download anything in that folder. using realpath() will turn that ./../../ into a final path statement, in our case /etc on a linux box
Reply With Quote
  #5 (permalink)  
Old 06-08-09, 05:11 AM
knolly knolly is offline
Newbie Coder
 
Join Date: Apr 2009
Posts: 14
Thanks: 2
Thanked 0 Times in 0 Posts
Wow, ok, that's really good information, thanks! So I'm really new to this, where exactly would that fit into my bit of code?
Reply With Quote
  #6 (permalink)  
Old 06-08-09, 07:34 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Quote:
stristr(realpath($_GET['folder']), '/var/www/public/') === FALSE)
{
//exit because they are accessing a folder outside my public area
}
I believe in the above code, $_GET['folder'] would be your $g.
And '/var/www/public/' would be the realpath of your image folder.

And it should fit into your code, something like this:
PHP Code:

<?php
$g 
array_key_exists ('g'$_GET) ?
$_GET['g'] : p1;
$selected $g;

$folder "see code below";
stristr(realpath($g), $folder) === FALSE)
{
    die(
"<b>You bad boy, you don't have permission to access that folder.</b>");
}

//Open images directory
$dir dir($g);

//List files in images directory
while (($file $dir->read()) !== false)
{
$ext substr(strrchr($file'.'), 1);

if (
$ext==jpg)echo "<img src=\"$g/" $file "\" alt=\"\" />";
}


$dir->close();
?>
And to get the correct path for $folder, you can run this code and then put the results in $folder where "images" is the value you would put in $_GET["g"].
PHP Code:

<?php
$g 
"images";
echo 
realpath($g);
?>
__________________
Jerry Broughton

Last edited by job0107; 06-08-09 at 07:37 AM.
Reply With Quote
  #7 (permalink)  
Old 06-08-09, 04:38 PM
knolly knolly is offline
Newbie Coder
 
Join Date: Apr 2009
Posts: 14
Thanks: 2
Thanked 0 Times in 0 Posts
thanks! that's really helpful
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
file listing problem jonnekke PHP 5 06-08-07 10:25 AM
PHP file upload problem websmart PHP 1 07-19-06 03:44 AM
Server open file altafingar ASP.NET 1 10-20-04 12:12 AM
Getting the created file (fopen/fwrite) Programme PHP 5 02-14-04 03:09 PM
Aggghhh - Problem writing a file to server DAL Perl 11 11-23-03 03:33 PM


All times are GMT -5. The time now is 08:36 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.