/ Published in: PHP
Função muito boa criada por Sean Nall.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php # Written by Sean Nall <all.marx {at} gmail> # and placed into the public domain. # @function upload_file # # @param $field string the name of the file upload form field # @param $dirPath string the relative path to which to store the file (no trailing slash) # @param $maxSize int the maximum size of the file # @param $allowed array an array containing all the "allowed" file mime-types # # @return mixed the files' stored path on success, false on failure. { foreach ($_FILES[$field] as $key => $val) $$key = $val; return false; // file failed basic validation checks return false; // file is not an allowed type return $path; return false; } ?> DEMO: <?php { if ($filepath = upload_file('music_upload', 'music_files', 700000, array('audio/mpeg','audio/wav'))) echo 'File uploaded to ' . $filepath; else echo 'An error occurred uploading the file... please try again.'; } echo ' <form method="post" action="' .$_SERVER['PHP_SELF']. '" enctype="multipart/form-data"> <input type="file" name="music_upload" id="music_upload" /> <input type="submit" name="submit" value="submit" /> </form> '; ?>