Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // In response to: http://answers.yahoo.com/question/index?qid=20130607135748AAiqlu7
- function uploadErrorMsg($num, $debug=false){
- $msg = Array(
- 1 => 'The uploaded file exceeds the Server\'s Maximum Allowable File Size',
- 2 => 'The uploaded file exceeds the Form\'s Maximum Allowable File Size.',
- 3 => 'The uploaded file was only partially uploaded, then interrupted or the connection was dropped.',
- 4 => 'No file was uploaded.',
- 6 => 'Missing a temporary folder. The server requires a temporary folder for file uploads.', // Internal Operations Error
- 7 => 'Failed to write file to disk.', // Internal Server Error
- 8 => 'A PHP extension stopped the file upload.' // PHP Extension Library stopped the upload.
- );
- return $msg[(int)$num];
- }
- if( isset($_FILES['file']) ){
- $f = $_FILES['file'];
- if( $f['error'] === UPLOAD_ERR_OK){ // UPLOAD_ERR_OK is a PHP Upload Constant
- $allowedExts = Array("jpg", "jpeg","gif","png");
- $allowedTypes = Array('image/jpeg','image/pjpeg','image/jpg','image/gif','image/png');
- $extension = pathinfo($f['name'], PATHINFO_EXTENSION);
- $newPath = 'upload/' . $f['name'];
- if( in_array($f['type'], $allowedTypes) && in_array($extension, $allowedExts)){
- echo "Upload: " . $f["name"] . "<br>";
- echo "Type: " . $f["type"] . "<br>";
- echo "Size: " . ($f["size"] / 1024) . " kB<br>";
- echo "Temp file: " . $f["tmp_name"] . "<br>";
- if( file_exists($newPath) ){
- echo $f['name'] . ' already exists.';
- }else if( move_uploaded_file( $f['tmp_name'], $newPath) ){
- echo $f['name'] . ' uploaded successfully to <b>' . $newPath . '</b>.<br />';
- echo '<img src="' . $newPath . '" style="max-width: 200px;" />';
- }
- }else{
- echo 'Invalid File';
- }
- }else{
- echo uploadErrorMsg($f['error']);
- }
- }else{
- echo 'Nothing To Upload';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement