Jump to content

Recommended Posts

Hi,

 

Ive had a website for some time that allows people to upload pictures to certain directories, and each directory had its own upload script, I decided as Im slowley getting better at php to combine it all into 1 file and include it, all works great, but now I notice its a lot of repeated code as the only thing that changes for each function is the directory name,

 

here is the script as it is:

<?php 
// James Davies, Revised 2010
// Create the main NSFW upload function 
function demotivate() { 
//configure site specific variables
$website = 'http://www.thenudedeer.com';
//what is the folder name
$phpbbdir = '/demotivate/';
// Create an array containing all valid upload file types for this script 
$allowed_types = array( 
        "image/gif" => "gif", 
        "image/pjpeg" => "jpg", 
        "image/jpg" => "jpg",
        "image/jpeg" => "jpg",
        "image/png" => "png",
	"image/x-png" => "png",
// Add more types here if you like 
    ); 
// Check to see if the file type is in the allowed types array 
    if(!array_key_exists($_FILES['userfile']['type'], $allowed_types)) { 
        die("Invalid file type."); 
    } 
// Set the maximum uploadable file size => 512000 = 500kb 
    $maxfilesize = 512000; 
// Is the file larger than it is allowed to be? 
    if($_FILES['userfile']['size'] > $maxfilesize) { 
        die("File too large"); 
    }
// Where will the file be uploaded to? 
    $uploaddir = $_SERVER['DOCUMENT_ROOT'] . $phpbbdir; 
// What is the files temporary name? 
    $file = $_FILES['userfile']['tmp_name']; 
// What is the files actual name? 
    $filename = $_FILES['userfile']['name']; 
// Does this file already exist on the server? 
    if(file_exists($uploaddir . $filename)) 
{ 
//die("A file with that name already exists on this server."); 
die("File already existed, [img=http://" . $website . $phpbbdir . $filename ."]"); 
}
else { 
// This file does not already exist, so copy it. 
        copy($file, $uploaddir.$filename) or die("Could not copy file."); 
    } 
// All done! :-) 
    echo "<br />";
    echo "<font color=red><b>";
    echo "Upload successful";
    echo "<br />";
    echo "Copy and Paste the following Code";
    echo "</font><br/>";
    echo "<font color='green'><b>";
    echo "<br />";
    echo "[img]http://" . $website . $phpbbdir . $filename;
    echo "[/img]";
    echo "<br/>";
    echo "<br/>";
    echo '<img class="img" src="' .$phpbbdir .$filename.'">';
    echo "<br />";
    echo "<br />";
    echo "</font>";
    echo "<br /><br />";
}
function nsfw() { 
//configure site specific variables
$website = 'http://www.thenudedeer.com';
//what is the folder name
$phpbbdir = '/nsfw/';
// Create an array containing all valid upload file types for this script 
$allowed_types = array( 
        "image/gif" => "gif", 
        "image/pjpeg" => "jpg", 
        "image/jpg" => "jpg",
        "image/jpeg" => "jpg",
        "image/png" => "png",
	"image/x-png" => "png",
// Add more types here if you like 
    ); 
// Check to see if the file type is in the allowed types array 
    if(!array_key_exists($_FILES['userfile']['type'], $allowed_types)) { 
        die("Invalid file type."); 
    } 
// Set the maximum uploadable file size => 512000 = 500kb 
    $maxfilesize = 512000; 
// Is the file larger than it is allowed to be? 
    if($_FILES['userfile']['size'] > $maxfilesize) { 
        die("File too large"); 
    }
// Where will the file be uploaded to? 
    $uploaddir = $_SERVER['DOCUMENT_ROOT'] . $phpbbdir; 
// What is the files temporary name? 
    $file = $_FILES['userfile']['tmp_name']; 
// What is the files actual name? 
    $filename = $_FILES['userfile']['name']; 
// Does this file already exist on the server? 
    if(file_exists($uploaddir . $filename)) 
{ 
//die("A file with that name already exists on this server."); 
die("File already existed, [img=http://" . $website . $phpbbdir . $filename ."]"); 
}
else { 
// This file does not already exist, so copy it. 
        copy($file, $uploaddir.$filename) or die("Could not copy file."); 
    } 
// All done! :-) 
    echo "<br />";
    echo "<font color=red><b>";
    echo "Upload successful";
    echo "<br />";
    echo "Copy and Paste the following Code";
    echo "</font><br/>";
    echo "<font color='green'><b>";
    echo "<br />";
    echo "[img]http://" . $website . $phpbbdir . $filename;
    echo "[/img]";
    echo "<br/>";
    echo "<br/>";
    echo '<img class="img" src="' .$phpbbdir .$filename.'">';
    echo "<br />";
    echo "<br />";
    echo "</font>";
    echo "<br /><br />";
}
function fail() { 
//configure site specific variables
$website = 'http://www.thenudedeer.com';
//what is the folder name
$phpbbdir = '/fail/';
// Create an array containing all valid upload file types for this script 
$allowed_types = array( 
        "image/gif" => "gif", 
        "image/pjpeg" => "jpg", 
        "image/jpg" => "jpg",
        "image/jpeg" => "jpg",
        "image/png" => "png",
	"image/x-png" => "png",
// Add more types here if you like 
    ); 
// Check to see if the file type is in the allowed types array 
    if(!array_key_exists($_FILES['userfile']['type'], $allowed_types)) { 
        die("Invalid file type."); 
    } 
// Set the maximum uploadable file size => 512000 = 500kb 
    $maxfilesize = 512000; 
// Is the file larger than it is allowed to be? 
    if($_FILES['userfile']['size'] > $maxfilesize) { 
        die("File too large"); 
    }
// Where will the file be uploaded to? 
    $uploaddir = $_SERVER['DOCUMENT_ROOT'] . $phpbbdir; 
// What is the files temporary name? 
    $file = $_FILES['userfile']['tmp_name']; 
// What is the files actual name? 
    $filename = $_FILES['userfile']['name']; 
// Does this file already exist on the server? 
    if(file_exists($uploaddir . $filename)) 
{ 
//die("A file with that name already exists on this server."); 
die("File already existed, [img=http://" . $website . $phpbbdir . $filename ."]"); 
}
else { 
// This file does not already exist, so copy it. 
        copy($file, $uploaddir.$filename) or die("Could not copy file."); 
    }
// All done! :-) 
    echo "<br />";
    echo "<font color=red><b>";
    echo "Upload successful";
    echo "<br />";
    echo "Copy and Paste the following Code";
    echo "</font><br/>";
    echo "<font color='green'><b>";
    echo "<br />";
    echo "[img]http://" . $website . $phpbbdir . $filename;
    echo "[/img]";
    echo "<br/>";
    echo "<br/>";
    echo '<img class="img" src="' .$phpbbdir .$filename.'">';
    echo "<br />";
    echo "<br />";
    echo "</font>";
    echo "<br /><br />";
}
?> 

there must be a way to stop repeating all the same code for each function?
many thanks

Link to comment
https://forums.phpfreaks.com/topic/209335-multiple-directory-file-uploader/
Share on other sites

There is still many more improvements you could make. However, this should be slightly better. It uses a single function to process the file upload.

 

I haven't actually tested this.

 

<?php
define('DS', DIRECTORY_SEPARATOR);

function error_handler($level, $message, $file, $line, $context) {
  if ($level === E_USER_ERROR || $level === E_USER_WARNING || $level === E_USER_NOTICE) {
    echo '<strong>Error:</strong>' . $message;
    return TRUE;
  }
  return FALSE;
}

function custom_error($message, $level) {
  $call = next(debug_backtrace());
  trigger_error($message . ' in <strong>' . $call['file'] . '</strong> on line <strong>' . $call['line'] . '</strong>', $level);
}

function upload_file($file, $dir) {
  if (is_array($file)) {
    $config = array();
    $config['website_addr'] = 'http://www.thenudedeer.com';
    $config['types']        = array('image/gif' => 'gif', 
                                    'image/pjpeg' => 'jpg', 
                                    'image/jpg' => 'jpg', 
                                    'image/jpeg' => 'jpg', 
                                    'image/png' => 'png', 
                                    'image/x-png' => 'png');
    $config['max_filesize'] = 512000;
    
    if (!array_key_exists($file['type'], $config['types'])) {
      die('Invalid file type specified.');
    }
    
    if ($file['size'] > $config['max_filesize']) {
      die('The file is too large.');
    }
    
    $up_dir = $_SERVER['DOCUMENT_ROOT'] . DS . $dir . DS;
    
    if (file_exists($up_dir . DS . $file['name'])) {
      die ('File already existed, [img=http://'. $config['website_addr'] . DS . $dir . DS . $file['name'] . ']');
    }
    else {
      copy($file, $up_dir . $file['name']) or die('Could not copy file.');
    }
    
    // All done! :-) 
    echo "<br />";
    echo "<font color=red><b>";
    echo "Upload successful";
    echo "<br />";
    echo "Copy and Paste the following Code";
    echo "</font><br/>";
    echo "<font color='green'><b>";
    echo "<br />";
    echo "[img]http://" . $config['website_addr'] . DS . $dir . DS . $file['name'];
    echo "[/img]";
    echo "<br/>";
    echo "<br/>";
    echo '<img class="img" src="' . $dir . DS . $filename.'">';
    echo "<br />";
    echo "<br />";
    echo "</font>";
    echo "<br /><br />";
  }
  else {
    custom_error('upload_file() expects parameter 1 to be an array.', E_USER_ERROR);
  }
}
?>

 

Example:

<?php
$files = $_FILES['userfile'];
upload_file($files, 'dirname');
?>

oh wait, where am I defining the directory name? and I still have to repeat all the code for each funtion dont I?

You define the directory name when you call the function..

 

For instance

nsfw("someDirectory");

 

I put that in the function call, it doesnt work :( can you help?

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.