websponge Posted July 30, 2010 Share Posted July 30, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/209335-multiple-directory-file-uploader/ Share on other sites More sharing options...
Zane Posted July 30, 2010 Share Posted July 30, 2010 Something like this should shorten you code substantially function nsfw($dirname) { //configure site specific variables $website = 'http://www.thenudedeer.com'; //what is the folder name $phpbbdir = '/$dirname/'; Quote Link to comment https://forums.phpfreaks.com/topic/209335-multiple-directory-file-uploader/#findComment-1093058 Share on other sites More sharing options...
websponge Posted July 30, 2010 Author Share Posted July 30, 2010 Brilliant! I totally forgot that you can assign variable to the same function, im learning all the way on this site, you guys are awesome. Thankyou, Im giving it a whirl now. Quote Link to comment https://forums.phpfreaks.com/topic/209335-multiple-directory-file-uploader/#findComment-1093066 Share on other sites More sharing options...
websponge Posted July 30, 2010 Author Share Posted July 30, 2010 oh wait, where am I defining the directory name? and I still have to repeat all the code for each funtion dont I? Quote Link to comment https://forums.phpfreaks.com/topic/209335-multiple-directory-file-uploader/#findComment-1093068 Share on other sites More sharing options...
Wolphie Posted July 30, 2010 Share Posted July 30, 2010 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'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/209335-multiple-directory-file-uploader/#findComment-1093073 Share on other sites More sharing options...
Zane Posted July 30, 2010 Share Posted July 30, 2010 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"); Quote Link to comment https://forums.phpfreaks.com/topic/209335-multiple-directory-file-uploader/#findComment-1093075 Share on other sites More sharing options...
websponge Posted July 30, 2010 Author Share Posted July 30, 2010 sorry guys im lost, thats too much for me to understand at this moment! thanks anyway Quote Link to comment https://forums.phpfreaks.com/topic/209335-multiple-directory-file-uploader/#findComment-1093087 Share on other sites More sharing options...
websponge Posted July 30, 2010 Author Share Posted July 30, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/209335-multiple-directory-file-uploader/#findComment-1093193 Share on other sites More sharing options...
Zane Posted July 30, 2010 Share Posted July 30, 2010 show the code where you are calling the function Quote Link to comment https://forums.phpfreaks.com/topic/209335-multiple-directory-file-uploader/#findComment-1093224 Share on other sites More sharing options...
websponge Posted August 2, 2010 Author Share Posted August 2, 2010 show the code where you are calling the function if($_POST['action'] == "demotivate") { upload("demotivate"); } else if($_POST['action'] == "nsfw") { upload("nsfw"); } else if($_POST['action'] == "fail") { upload("fail"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/209335-multiple-directory-file-uploader/#findComment-1094026 Share on other sites More sharing options...
websponge Posted August 4, 2010 Author Share Posted August 4, 2010 It now works with the above code, many thanks all, especially zanus Quote Link to comment https://forums.phpfreaks.com/topic/209335-multiple-directory-file-uploader/#findComment-1095060 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.