| File Size |
|
I wanted the album to display each file's file size - that way I would
know which file was taking up the most space at a quick glance. I did some searching and reading on the
internet and found that Perl has a function called 'stat' which gives staticts on a file. Just for
reference, here are all the statistics that the stat file gives -
stat($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize,
$blocks) As you can see, $size is the 7th slot in
the array so I just created a variable and stored the 7th value of stat into that variable. It turns out
that the size of the file is in bytes so I had to do some math to that variable to get it to display either
KB or MB depending on the size of the file. I also created a variable that kept track of the total size of
the directory - this is helpful since I can visually see from the webpage how big my 'pics' folder is
without having to FTP to the BW site and then choosing properties on the folder.
|
| Different File Types |
|
The more I used the photo album, the more I realized I was
uploading files that weren't neccessarily photos/pictures. Many times, I wanted my friends to see a video
or listen to a song so I would upload it on the server and then send them the link to it. However, if I was
at 'view.cgi' or 'filemaint.cgi' looking at the files in the folder, a preview of the non-picture
file(s) wouldn't show up. So I decided that I would display some sort of icon for video, music and whatever
other files I uploaded. After having the icons sized and ready to be displayed, I had to come up with a way
of parsing the file extension from the file and then from that file extension I would display the correct
icon. While writing the upload function, I remembered I used a function that reversed the file name and
then read it. Therefore, I used the same method - reverse the file, read the first four characters of the
reversed file name, and then if the first three were say '3pm', the file is an mp3 and I would display a
music icon. I ended up adding another checkpoint for the .mpeg file extension since that it a 4 string
extension. The check that I performed was if the fourth character wasn't a '.' I re-read the reverse string
and read 5 characters this time. As of right now, I have the ability to display video, music, and flash icons
for those file types. Eventually, I will have a large list of different file extensions for many different
file types and different icons will be displayed accordingly.
|
| Renaming Files |
|
Another useful feature that I thought would add more functionality to
the photo album is a renaming feature. With this, I would choose a file to rename, and then the file name
(not the extension but just the name) would be displayed in a text box where I could edit the file
name. Underneath the text box, there are two buttons - one to submit the new data and change the file name
and another one to cancel the process of renaming the file. In order to accomplish all of this, I first
needed a nice looking icon that the user would click on to rename the file. I searched for an 'abc' icon
since the letters represent renaming in a way, however I couldn't find any that I like so I ended up
creating one in Adobe Photoshop. Next I need a way of displaying the text box and the buttons after the
user
clicked on the 'abc' icon. To do that, I made the link of the 'abc' icon pass along a paramter - the
filename. So next time the page was reloaded, if the param() function has the file name in, then instead of
displaying the regular info. about the file, I would display the text box and buttons. Once again, in order
to NOT display the extension of the file, I had to parse the file in reverse until I read a '.' - then I
stored the renaming characters in a new variable and then re-reversed the reversed string to display the
filename. This may sound confusing but if you just think about for a second, it will make sense. Ok
that covers how to display everything properly in order to start the renaming process - now how I
renamed the file. I ended up creating three variables, one that gets the original file name (I passed
that along by a hidden field in the form, just gave the hidden type the name of my file) and one that
gets the extension of the file because I split all that up so the user couldn't change the
extension. My third variable grabs the parameter passed from the text box - the new file name. After I
created those variables and made them get the proper fields from the parameter passing, I did a simple check
to see if the oldfilename variable is equal to the filename that was just used to process all the
information pertaining to that file in the loop. If both match, I know I can rename it. The first thing I
had to do was join the newfilename and the extension - that was accomplished by the 'join' function. The
'join' function took the newfilename and combined it with the extension - basically the join function is the
exact opposite of the 'spilt' function.
$newfilename = join '',
$newfilename,
$extension; Now that I joined those two variables together - I had a vaild
filename. Next, I had to do some error checking to see if the newfilename contained any whitespace
characters. If I didn't do this, whenever I wanted to see the picture, the webserver would state that the
address doesn't exsist because whitespace characters don't get along with the webserver. So I used the
search feature in Perl to go through each character in the newfilename and see if finds a whitespace
character. If it did, I had it replace the whitespace character with an underscore. The exact code for
this is...
$newfilename =~ s/\s+/_/g; where
=~
specifies the
bitwise operator, s/ is the search field, s+ is multiple whitespace character (s would just mean one
whitespace character), and /_/ replaces the whitespace character with and underscore. Not sure what g means
but I'm guessing it means go to the next character. Ok, now that the filename is suitable for the web, I
have to replace the oldfilename with the new one. Lucky for me Perl has a 'rename' function built in where
you give it the oldfilename and then the newfilename and it replaces them.
rename($oldfilename,$newfilename); Well that's how I did the rename
function, if you have any questions, feel free to e-mail - a link to e-mail is at the bottom.
|
| Uploading Multiple Files At
Once |
|
Well, this was actually easier than I thought. Maybe it was because I
thought about how I would do it for a few days before I actually started writing and experimenting with the
code. Anyways, all I had to do was give each of the fields where one can upload a file a unique name. The
name that I had already when uploading one file was 'fname' which stood for filename. I therefore just
added a number at the end of it - 'fname1' - and then did the same for the others. Since I had six uplaod
fields, I had 'fname1' thru 'fname6' respectively. Now that I had that set up, the next thing to modify was
the upload.cgi file to make it so it go all six parameters that were passed when submitting the form. Since
I used numbers, it was easy to grab each parameter by using a counter. I initialized a variable 'i' to
equal 1 at the very beginning. After initialization of that variable, I created a while loop that would
loop through until 'i' was less than seven. Ok, now the loop is setup, the next thing I have to do is grab
the filenames from each of the upload fields. Right now, the way this is accomplished is by simple if
statements that check to see what 'i' equals. If it is equal to 1 say, then I give $fpath (filepath) the
path of the first filename.
if($i==1){ $fpath =
param('fname1'); } I do the same with the others when 'i' equals there
number. After that,
$fpath just uses the same code I had to upload one file and uploads its file - then goes onto the next. The
only check I had to do was if $fpath was blank.
while($fpath
ne'') Mainly, that line checks to see if the variable $fpath is blank meaning
I didn't have
a file to upload in that field. All the code to write the file on the server is inside that while
loop. That is about it besides I had to write some extra code to display what files were uploaded
successfully. I created an array called @fnames inside the loop that increments 'i' and
everytime it went through the while loop, I stored the filename in a slot of @fnames such as
$fnames[$i] = $fname; That way, I had a way of
displaying the
filenames that were upload successfully. So that is how I completed that feature on uploading multiple
files at once. I'm still gonna work on the code a little because I think it is a little redunant, I believe
I can shorten it up a bit. Also, right now six messages popup about files being uploaded successfully even
though I may have only uploaded one file - so I have to work on that. But I got the overall theme
completed.
|
| User Has
Access To Upload Files |
|
Coming soon...
|
|