Exploding Garrmondo Weiner Interactive Swiss Army Penis

Exploding Garrmondo Weiner Interactive Swiss Army Penis (http://www.gamingforce.org/forums/index.php)
-   Help Desk (http://www.gamingforce.org/forums/forumdisplay.php?f=36)
-   -   PHP Help (Upload Script) (http://www.gamingforce.org/forums/showthread.php?t=33795)

Zergrinch Aug 11, 2008 11:59 AM

PHP Help (Upload Script)
 
1 Attachment(s)
I'm currently using the attached upload PHP script to quickly upload files into my webspace. It has most of the features I desire, like optional password protection, ability to restrict certain file types, and multiple uploads at once.

However, there's a sorting feature I kinda want, but have absolutely no idea how to do. Currently, the script dumps everything into its current directory. Is it possible to set it up such that certain file types get put in certain folders?

For example, if you upload .PNG .GIF or .JPG, it will save the files to \pictures. If you upload .TXT .DOC .XLS .PPT, it will save the files to \documents. If you upload .RAR .ZIP, it will save it to \archives.

Is this kind of thing possible? If so, how do I shot web?

Thanks in advance :p

Secret Squirrel Aug 14, 2008 06:50 AM

It's definitely doable, and probably isn't overly difficult so it should make a good little project for you to learn php.
  • Split the string $file_name[$i] and grab everything after the period (the extension)
  • Create a big if..then...elfseif block and compare that extension name to the types of files you're allowing to be uploaded (txt, jpg, etc.)
  • In each branch of the if statement, take the variable called $folder, and set it equal to the appropriate folder name (txt,jpg,etc.)

That should be all you need to do, barring any surprises.

OmagnusPrime Aug 15, 2008 01:42 AM

What Squirrel said pretty much covers what you'll need to do. If you do your split on '.' then you should be able grab the last section to get your extension. Actually there may be a built in way of grabbing the extension, but I can't remember off the top of my head.

What I would change, is don't use an if/else block, use a switch statement and use drop through to avoid having to re-write lots of code to set the folder name unnecessarily.

Zergrinch Sep 1, 2008 08:43 AM

1 Attachment(s)
Alrighty, I've taken your suggestions and put a whole chunk of conditionals using the SWITCH command.

The files uploaded magnificently.

BUT there is one problem. After the files are uploaded, the script echoes their URL. This is the code that does so:

Code:

For($i=0; $i <= $file_uploads-1; $i++) {
        If($_FILES['file']['name'][$i]) {
                $file=$i+1;
                                Echo("<b>File #".$file.":</b> <a href=\"".$full_url.$file_name[$i]."\" target=\"_blank\">".$full_url.$file_name[$i]."</a><br /><br />\n");
        }
}

It's basically using $full_url, which is static compared to the $folder which I changed with a bunch of conditionals.

What's the best way of going about this?

The new version, plus code I've inserted, is attached.
A test implementation exists at jetUpload - Powered By phUploader

Secret Squirrel Sep 1, 2008 08:51 AM

You could just tack the foldername onto the end of the url by sticking this line in front of the Echo:

Code:

$full_url .= $folder . "/";

Zergrinch Sep 1, 2008 09:06 AM

Nada, it am fail :(

Edit: I gave up and recopied the entire SWITCH block right before the echo. It seems to work!

Thanks for the assist, Secret Squirrel.


All times are GMT -5. The time now is 02:45 AM.

Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2020, vBulletin Solutions, Inc.