Tomtat Posted July 23, 2008 Share Posted July 23, 2008 hey i'm kinda new to php, but i'm okay with html, css and some javascript/actionscript. anyway i'm currently making my first proper website and need some help. one of the main functions of this website is that the user must be able to upload their own files (that bit i'm okay with), but i was wondering how if at all possible i would go about showing people in a nice pretty list the content that they have uploaded. Preferably also with options to delete/view selected uploaded content. any help is greatly appreciated Quote Link to comment https://forums.phpfreaks.com/topic/116284-new-noob-needs-help-displaying-uploaded-files/ Share on other sites More sharing options...
SamVaughton Posted July 24, 2008 Share Posted July 24, 2008 <table align="center" cellpadding="5" cellspacing="5" border="0"> <tr> <td align="center" class="gTxt"><b>Image Name</b></td> <td align="center" class="gTxt"><b>Image Size</b></td> </tr> <?php $dir = 'uploads'; $files = scandir($dir); foreach ($files as $image) { if (substr($image, 0, 1) != '.') { $image_size = getimagesize("$dir/$image"); $file_size = round ( (filesize("$dir/$image")) / 1024) . "kb"; echo "<tr> <td>$image</a></td> <td>$file_size</td> </tr>"; } } echo '</table>'; ?> This code creates a table in HTML, and then populates it with uploaded files in a upload directory. It displays the name and the size of the file. (This code is modified from my original script and I haven't tested it but it should work fine.) PS. Sorry its not commented, you should be able to understand it easy enough: $dir variable is the directory = uploads $files variable is an array, the scandir() function scans all files in the directory ($dir) It then does a foreach loop, which is basically saying transfer one variable from the array into $image until there are no files left, it then cuts the string of $image, gets the size and locates it and then puts it in a table. I think it is right. Sorry for bad grammer. Quote Link to comment https://forums.phpfreaks.com/topic/116284-new-noob-needs-help-displaying-uploaded-files/#findComment-598795 Share on other sites More sharing options...
Tomtat Posted July 24, 2008 Author Share Posted July 24, 2008 thanks alot man would this still work if i put the files in a drop down box so you could select one rather than a html table. and then two buttons afterwards one to delete the selected file from the directory and another to load it in a new window? thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/116284-new-noob-needs-help-displaying-uploaded-files/#findComment-598848 Share on other sites More sharing options...
Tomtat Posted July 25, 2008 Author Share Posted July 25, 2008 Okay this is what i got by modifying the script to delete a selected file from the files displayed. Showedit.php: <?php session_start() ?><center> <form name="videoform" method="post" action="deletefile.php"> <select name="selectupload" id="selectupload" size="10"> <?php $dir = 'Games/'. $_SESSION['nameuser']; $files = scandir($dir); foreach ($files as $image) { if (substr($image, 0, 1) != '.') { $image_size = getimagesize("$dir/$image"); $file_size = round ( (filesize("$dir/$image")) / 1024) . "kb"; echo " <option value=$image> File: $image Size: $file_size </option>"; } } echo '</select>'; ?> <BR><hr width="30"> <input type="submit" name="submit" value="Delete File"><BR><hr width="30"><br></center> Deletefile.php: <?php session_start() ?> <?php $file=$_POST['selectupload']; unlink ('Games/'. $_SESSION['nameuser'] . '/' . $file); echo 'file deleted: ' . $file; ?> I mite combine the two files later so they can be deleted faster. Can any of you spot any immediate weaknesses in the script? thanks Quote Link to comment https://forums.phpfreaks.com/topic/116284-new-noob-needs-help-displaying-uploaded-files/#findComment-599375 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.