PhpSnip.com

User Stats

The works -- file upload that's simple and elegant

This is a very simple, working example of a file upload. I provide the php code to handle an arbitrary number of file uploads, and example html code.

Info

 Download  View Source (print view)
 Rating : 4.7  Views : 343

Source Code ( 44 lines )

> HTML code:
> -----------
> <FORM ENCTYPE="multipart/form-data" ACTION="submit.php" METHOD=POST>
>
> <input type=file name=files[] size=20>
>
> <input type=submit value="Upload File">
>
> </FORM>
>
> submit.php:
> -----------
> <?php
>
> $path_to_file = // enter the directory you want file uploaded to here
>
> // file upload code
> // it expects the inputs of type file to be named 'files[]'
> // it will work with an arbitrarily large number of files
> $files = $HTTP_POST_FILES['files'];
> if (!ereg("/$", $path_to_file))
>   $path_to_file = $path_to_file."/";
> foreach ($files['name'] as $key=>$name) {
>    if ($files['size'][$key]) {
>       // clean up file name
>       $name = ereg_replace("[^a-z0-9._]", "",
>         str_replace(" ", "_",
>             str_replace("%20", "_", strtolower($name)
>             )
>         )
>       );
>       $location = $path_to_file.$name;
>       while (file_exists($location))
>          $location .= ".copy";
>       copy($files['tmp_name'][$key],$location);
>       unlink($files['tmp_name'][$key]);
> //    $content .= "Uploaded File: ".$location."n";
>       $thanks .= "n<br>Successfully uploaded file: $name.";
>    }
> }
> print "<h1>File Submission</h1>n";
> print $thanks;
>
> ?>

Search

Subscribe

  Rss Feeds

Sponsors

Advertise