Page 1 of 1

PHP OpenDir in dropdown

Posted: Mon Jan 29, 2007 6:45 am
by gourangie
Hi all,

I've got myself a bog standard php file uploader and was just wondering if, rather than just style up the php open directory file is it possible to generate a dynamic list form (or similar style drop down [preferably not javascript]) that updates according to files uploaded. Thus all functionality- uploads and downloads can be kept within the same page.

Any ideas welcome. - cheers all

Posted: Mon Jan 29, 2007 8:29 am
by superdezign
Like... Just display the same information in a <select>...</select>? Just use the same loop you're using. but put <select>...</select> around it and <option>...</option> around each item. I'm hoping you came to the PHP code or something a bit more complex than that though.

If so, give a little more detail as to what you're attempting to accomplish.

Posted: Mon Jan 29, 2007 8:29 am
by feyd
Updating a form field without Javascript requires refreshing the page. Is this suitable? I'm not sure what you're asking for beyond that information.

Posted: Mon Jan 29, 2007 8:37 am
by superdezign
I'm not sure what "standard php uploader" you have, but most pre-programmed upload scripts contain a list of items which each listed element as a link to the object it represents (your download) and a small upload form at the bottom of the list (your upload) and on upload, the list is automatically updated after the file is upload due to an automatic refresh at the submission of the upload form. Is this what you're after?

I can't see javascript making that process feel any different to a user.

Posted: Wed Jan 31, 2007 4:20 am
by gourangie
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hey peeps,
Thanks for quick responses. Let me apologise and try to clarify. The upload script is for a clients website - they simply wish to be able to open any browser on any computer and upload a file to his website - upload script :

up_down.html

[syntax="html"]<html>
<head>
<title>File upload</title>
</head>
<body>
<h1>File Uploads and Downloads</h1>
<form enctype="multipart/form-data" action="upload.php" method="php">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
Upload your file: <input name="userfile" type="file">
<input type="submit" value="Send File">
</form>
</body>
</html>
upload.php[/syntax]

Code: Select all

<html>
<head>
<title>Uploading...</title>
</head>
<body>
<h1>Uploading File...</h1>
<?php

if ($_FILES['userfile'] ['error'] > 0)
{
echo 'Problem: ';
switch ($_FILES['userfile'] ['error'])
{
case 1: echo 'File exceeded upload_max_filesize'; break;
case 2: echo 'File exceeded max_file_size'; break;
case 3: echo 'File only partially uploaded'; break;
case 4: echo 'No file uploaded'; break;
}
exit;
}

// Does the file have the correct MIME type?
if ($_FILES['userfile'] ['type'] != 'text/plain')
{
echo 'Problem: file is not plain text';
exit;
}

// copy the file to uploads directory
$upfile = '/uploads/' .$_FILES['userfile'] ['name'] ;

if (is_uploaded_file ($_FILES['userfile'] ['tmp_name'] ))
{
if ( !move_uploaded_file($_FILES['userfile'] ['tmp_name'], $upfile))
{
echo 'Problem: Could not move file to destination directory';
exit;
}
}
else
{
echo 'Problem: Possible file upload attack. Filename: ';
echo $_FILES['userfile'] ['name'];
exit;
}

echo 'File uploaded successfully<br/><br/>';

// reformat the file contents
$fp = fopen($upfile, 'r');
$contents = fread ($fp, filesize ($upfile));
fclose ($fp);

$contents = strip_tags($contents);
$fp = fopen($upfile, 'w');
fwrite($fp, $contents);
fclose($fp);

// show what has been uploaded
echo 'Preview of uploaded file contents:<br/><hr/>';
echo $contents;
echo '<br/><hr/>';
?>
</body>
</html>
------------------------
phew, right now to view the uploads directory I'm currently using opendir, reddir and closedir

however whilst writing this I came across this

Code: Select all

<?php
/* 
  ABOUT: 
  This snippet will list all the files in the directory of your 
  choice, truncate the file extension, capitalize the first letter and 
    put it all in a drop down menu. The script will not list subdirectories so  
    all you see are the files in your directory. 
     
    Feel free to use or modify to your liking. 
     
    USAGE: 
    Change the $dirpath variable the directory you want to list. 

    AUTHOR: 
  Written by shockingbird 
  Visit http://www.glomer.net for more information 
  tate at tatenations dot com for any questions 
*/ 
    echo "<form>"; 
//Looks into the directory and returns the files, no subdirectories 
    echo "<select name='yourfiles'>"; 
//The path to the style directory 
    $dirpath = "/path/to/your/directory"; 
    $dh = opendir($dirpath); 
       while (false !== ($file = readdir($dh))) { 
 //Don't list subdirectories 
          if (!is_dir("$dirpath/$file")) { 
//Truncate the file extension and capitalize the first letter 
           echo "<option value='$file'>" . htmlspecialchars(ucfirst(preg_replace('/\..*$/', '', $file))) . '</option>'; 
   } 
} 
     closedir($dh);  
//Close Select 
echo "</select>"; 
echo "</form>"; 
?>
have yet to test it but it looks like it could work, certainly a good starting point.

Don't worry superdezign - I do try to utilise the full capabilities of php and I realise that this is quite a basic problem however I just hadn't ever found anything about the php code to place within the <option> tag - I just never needed to before. All examples I'd experimented with just failed.

the reasoning behind no javascript was simply just client brief spec.

Cheers all


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]