Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-06-2011, 12:27 AM   PM User | #1
eberdome
Regular Coder

 
Join Date: Dec 2010
Location: California
Posts: 193
Thanks: 28
Thanked 8 Times in 8 Posts
eberdome is an unknown quantity at this point
adding more fields to upload form

Hello,

I have a file upload script design for one upload at a time. I want to add 4 more! How can this be done?? The name that is assigned to the input tag is name="filename". how would i add filename2, filename3, filename4, filename5.

Thanks in advance!

PHP Code:
<?php

define
('DESTINATION_FOLDER','/home/rdewebde/public_html/lounge/uploads/');

define('MAX_FILE_SIZE'716800);

define('RENAME_FILE'false);

define('APPEND_STRING''');

$exts = array();


// Allow script to work long enough to upload big files (in seconds, 2 days by default)
@set_time_limit(172800);

// following may need to be uncommented in case of problems
// ini_set("session.gc_maxlifetime","10800");

function showUploadForm($message='') {
  
$max_file_size_tag '';
  if (
MAX_FILE_SIZE 0) {
    
// convert to bytes
    
$max_file_size_tag "<input name='MAX_FILE_SIZE' value='".(MAX_FILE_SIZE*1024)."' type='hidden' >\n";
  }

  
// Load form template
  
include ('/home/rdewebde/public_html/includes/upload.html');
}

// errors list
$errors = array();

$message '';

// we should not exceed php.ini max file size
$ini_maxsize ini_get('upload_max_filesize');
if (!
is_numeric($ini_maxsize)) {
  if (
strpos($ini_maxsize'M') !== false)
    
$ini_maxsize intval($ini_maxsize)*1024*1024;
  elseif (
strpos($ini_maxsize'K') !== false)
    
$ini_maxsize intval($ini_maxsize)*1024;
  elseif (
strpos($ini_maxsize'G') !== false)
    
$ini_maxsize intval($ini_maxsize)*1024*1024*1024;
}
if (
$ini_maxsize MAX_FILE_SIZE*1024) {
  
$errors[] = "<span class=\"errorMsg\">Your files exceed the max upload size</span>";
}

// show upload form
if (!isset($_POST['submit'])) {
  
showUploadForm(join('',$errors));
}

// process file upload
else {

  
  while(
true) {

    
// make sure destination folder exists
    
if (!@file_exists(DESTINATION_FOLDER)) {
      
$errors[] = "<span class=\"errorMsg\">Error! Please contact <a href=\"mailto:ryan@gorillaonemedia.com\">system admin</a> (Type 0)</span>";
      break;
    }

    
// check for upload errors
    
$error_code   $_FILES['filename']['error'];

                    
    if (
$error_code != UPLOAD_ERR_OK) {
      switch(
$error_code) {
        case 
UPLOAD_ERR_INI_SIZE
          
// uploaded file exceeds the upload_max_filesize directive in php.ini
          
$errors[] = "<span class=\"errorMsg\">Your files exceed the max upload size</span>";
          break;
        case 
UPLOAD_ERR_FORM_SIZE
          
// uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form
          
$errors[] = "<span class=\"errorMsg\">Your files exceed the max upload size</span>";
          break;
        case 
UPLOAD_ERR_PARTIAL:
          
// uploaded file was only partially uploaded.
          
$errors[] = "<span class=\"errorMsg\">Upload failed. Please try again</span>";
          break;
        case 
UPLOAD_ERR_NO_FILE:
          
// No file was uploaded
          
$errors[] = "<span class=\"errorMsg\">Please choose files to upload</span>";
          break;
        case 
UPLOAD_ERR_NO_TMP_DIR:
          
// Missing a temporary folder
          
$errors[] = "<span class=\"errorMsg\">Error! Please contact <a href=\"mailto:ryan@gorillaonemedia.com\">system admin</a> (Type 1)</span>";
          break;
        case 
UPLOAD_ERR_CANT_WRITE:
          
// Failed to write file to disk
          
$errors[] = "<span class=\"errorMsg\">Error! Please contact <a href=\"mailto:ryan@gorillaonemedia.com\">system admin</a> (Type 2)</span>";
          break;
        case 
8:
          
// File upload stopped by extension
          
$errors[] = "<span class=\"errorMsg\">Error! Please contact <a href=\"mailto:ryan@gorillaonemedia.com\">system admin</a> (Type 3)</span>";
          break;
      } 
// switch


      // leave the while loop
      
break;
    }

    
// get file name (not including path)
    
$filename = @basename($_FILES['filename']['name']);


    
// filename of temp uploaded file
    
$tmp_filename $_FILES['filename']['tmp_name']; 


    
$file_ext = @strtolower(@strrchr($filename,"."));
    if (@
strpos($file_ext,'.') === false) { // no dot? strange
      
$errors[] = "<span class=\"errorMsg\">Error! Please contact <a href=\"mailto:ryan@gorillaonemedia.com\">system admin</a> (Type 4)</span>";
      break;
    }
    
$file_ext = @substr($file_ext1); // remove dot

    // check file type if needed
    
if (count($exts)) {   /// some day maybe check also $_FILES['user_file']['type']
      
if (!@in_array($file_ext$exts)) {
        
$errors[] = "<span class=\"errorMsg\">Files of this type are not allowed for upload</span>";
        break;
      }
    }

    
// destination filename, rename if set to
    
$dest_filename $filename;
    if (
RENAME_FILE) {
      
$dest_filename md5(uniqid(rand(), true)) . '.' $file_ext;
    }
    
// append predefined string for safety
    
$dest_filename $dest_filename APPEND_STRING;

    
// get size
    
$filesize intval($_FILES["filename"]["size"]);

    
// make sure file size is ok
    
if (MAX_FILE_SIZE && MAX_FILE_SIZE*1024 $filesize) {
      
$errors[] = "<span class=\"errorMsg\">Your files exceed the max upload size</span>";
      break;
    }

    if (!@
move_uploaded_file($tmp_filename DESTINATION_FOLDER $dest_filename)) {
      
$errors[] = "<span class=\"errorMsg\">Error! Please contact <a href=\"mailto:ryan@gorillaonemedia.com\">system admin</a> (Type 5)</span>";
      break;
    }

    
// redirect to upload success url
    
$errors[] = "<span class=\"successMsgfp\">Thank You! Your files have been uploaded</span>";

    
$email "ryan@gorillaonemedia.com";

    
$subject "".$user_info['username']." just uploaded file(s)"

    
$message " ".$user_info['f_name']." ".$user_info['l_name']." has just uploaded the following files:

    $filename"
;

  
    
mail($email$subject$message,"From: ".$site_name." <".$site_email.">\n");     

    break;

  } 
// while(true)

  // Errors. Show upload form.
  
$message join('',$errors);
  
showUploadForm($message);
}

?>
eberdome is offline   Reply With Quote
Old 02-06-2011, 07:44 AM   PM User | #2
poyzn
Regular Coder

 
poyzn's Avatar
 
Join Date: Nov 2010
Posts: 265
Thanks: 2
Thanked 61 Times in 61 Posts
poyzn is on a distinguished road
how to upload multiple photos you can find in this thread http://www.codingforums.com/showthread.php?t=209571
__________________
Ushousebuilders.com
poyzn is offline   Reply With Quote
Reply

Bookmarks

Tags
files, php, upload

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:19 PM.


Advertisement
Log in to turn off these ads.