Jump to content

Archived

This topic is now archived and is closed to further replies.

DarkWaterStudio

Make New Sub Folder When Uploading?

Recommended Posts

Ok, i want to ad a "Text" feild to my upload form so if a users imputs his or her id it will make a sub folder call "There ID" in the upload path, and if its empty it just post it to the normal upload path.
But if the "User ID" SUB FOLDER EXSISTS it will just ad the content but not overwrite the old content within it!
I need a way to give credit where its due lol. Nowmaly it works 100% but i want that new field.

Here is my php script:[a href=\"https://host412.ipowerweb.com/~darkwate/upload.php\" target=\"_blank\"]Live View[/a]

<body>
<h2>Members File Upload
<?= $uploadVersion; ?>
</h2>

<?php
///////////////////////////////////////////
// Detecting server type and domain name //
///////////////////////////////////////////

if ($uploadPath!=='') $moveToDir = $uploadPath;
else {
$domName = str_replace('www.','',$_SERVER['SERVER_NAME']);
$moveToDir = '/home/httpd/vhosts/'.$domName.'/httpdocs'.$dirName;
}

if (!file_exists($moveToDir)) die ('<div id="error"><h3>** CONFIGURATION ERROR **</h3><p>Directory location does not exist:</p><p>'.$moveToDir.'</p></div></body></html>');
if (!is_writeable($moveToDir)) die ('<div id="error"><h3>** CONFIGURATION ERROR **</h3><p>Permissions error (CHMOD).</p><p>Please set write permissions to:<br />'.$moveToDir.'</p></div></body></html>');


///////////////////////////////////////
// Collecting acceptable file types //
///////////////////////////////////////

$acceptedFileTypes = array();
$acceptedFileTypesKey = array();
if ($accept_jpg ) {
array_push($acceptedFileTypes,'image/jpeg','image/pjpeg');
array_push($acceptedFileTypesKey,'jpeg');
}
if ($accept_gif ) {
array_push($acceptedFileTypes,'image/gif');
array_push($acceptedFileTypesKey,'gif');
}
if ($accept_png ) {
array_push($acceptedFileTypes,'image/png','image/x-png');
array_push($acceptedFileTypesKey,'png');
}
if ($accept_bmp ) {
array_push($acceptedFileTypes,'image/bmp');
array_push($acceptedFileTypesKey,'bmp');
}
if ($accept_xbm ) {
array_push($acceptedFileTypes,'image/xbm','image/x-xbitmap');
array_push($acceptedFileTypesKey,'xbm');
}
if ($accept_tiff) {
array_push($acceptedFileTypes,'image/tiff');
array_push($acceptedFileTypesKey,'tiff');
}
if ($accept_ico ) {
array_push($acceptedFileTypes,'image/x-icon','image/ico');
array_push($acceptedFileTypesKey,'ico');
}
if ($accept_html) {
array_push($acceptedFileTypes,'text/html');
array_push($acceptedFileTypesKey,'html');
}
if ($accept_pdf ) {
array_push($acceptedFileTypes,'application/pdf');
array_push($acceptedFileTypesKey,'pdf');
}
if ($accept_rtf ) {
array_push($acceptedFileTypes,'text/richtext','text/rtf');
array_push($acceptedFileTypesKey,'rtf');
}
if ($accept_txt ) {
array_push($acceptedFileTypes,'text/plain');
array_push($acceptedFileTypesKey,'txt');
}
if ($accept_mp3 ) {
array_push($acceptedFileTypes,'audio/mpeg','audio/mp3');
array_push($acceptedFileTypesKey,'mp3');
}
if ($accept_wav ) {
array_push($acceptedFileTypes,'audio/wav','audio/x-wav');
array_push($acceptedFileTypesKey,'wav');
}
if ($accept_aiff) {
array_push($acceptedFileTypes,'audio/aiff','audio/x-aiff');
array_push($acceptedFileTypesKey,'aiff');
}
if ($accept_au ) {
array_push($acceptedFileTypes,'audio/basic','audio/au','audio/snd');
array_push($acceptedFileTypesKey,'au','snd');
}
if ($accept_midi) {
array_push($acceptedFileTypes,'audio/mid','audio/midi');
array_push($acceptedFileTypesKey,'mid');
}
if ($accept_avi ) {
array_push($acceptedFileTypes,'video/avi','video/x-msvideo');
array_push($acceptedFileTypesKey,'avi');
}
if ($accept_mpeg) {
array_push($acceptedFileTypes,'video/mpeg','video/mpg');
array_push($acceptedFileTypesKey,'mpeg');
}
if ($accept_zip ) {
array_push($acceptedFileTypes,'application/x-zip-compressed','application/zip');
array_push($acceptedFileTypesKey,'zip');
}
if ($accept_gzip) {
array_push($acceptedFileTypes,'application/x-gzip-compressed','application/x-gzip','application/gzip');
array_push($acceptedFileTypesKey,'gz');
}
if ($accept_doc) {
array_push($acceptedFileTypes,'application/octet-stream','application/doc');
array_push($acceptedFileTypesKey,'doc');
}
if ($accept_swf) {
array_push($acceptedFileTypes,'application/x-shockwave-flash','application/swf');
array_push($acceptedFileTypesKey,'swf');
}
if ($accept_exe) {
array_push($acceptedFileTypes,'application/x-msdownload','application/exe');
array_push($acceptedFileTypesKey,'exe');
}
// Check if acceptable file types have been defined
if (!count($acceptedFileTypes) && !$upload_ANY) die ('<div id="error"><h3>** CONFIGURATION ERROR **</h3><p>No acceptable file types have been configured.</p></div></body></html>');


/////////////////
// File Upload //
/////////////////

if ($_FILES['uploadfile'] && count($_FILES['uploadfile']['tmp_name'])>0) {

// Parsing acceptable extensions
if ($detectExtensions) {
$acceptedExtensions = str_replace('.','',$acceptedExtensions);
$acceptedExtArray = explode('|',$acceptedExtensions);
}

function echoSuccessRow($n,$f,$c){
echo <<<END
<tr class="success">
<td align="right">$n</td>
<td><strong class="successtext">SUCCESS</strong></td>
<td>"$f"</td>
<td>$c</td>
</tr>

END;
}

function echoFailedRow($n,$f,$c){
echo <<<END
<tr class="failed">
<td align="right">$n</td>
<td><strong class="failedtext">FAILED</strong></td>
<td>"$f"</td>
<td>$c</td>
</tr>

END;
}

///////////////////////////////
// Processing Uploaded Files //
///////////////////////////////
for ($k=0;$k<count($_FILES['uploadfile']['tmp_name']);$k++) {
if ($_FILES['uploadfile']['name'][$k]=='') continue;

if ($k==0) echo '<table id="uploadResults" border="0" cellspacing="0" cellpadding="2" width="100%"><tr id="uploadHeaderRow"><th nowrap="nowrap">#</th><th nowrap="nowrap">Result</th><th nowrap="nowrap">Filename</th><th nowrap="nowrap">Comment</th></tr>';
$uploadFileNum = ($k+1).'.';

// Striping tags and invalid characters from uploaded filename
$uploadFileName = str_replace(array('|','<','>','"','\'',':','\\','/','*','?'),'',strip_tags($_FILES['uploadfile']['name'][$k]));

// Checking if uploaded file contains no data - file size is 0 bytes
if (filesize($_FILES['uploadfile']['tmp_name'][$k])==0) {
echoFailedRow($uploadFileNum,$uploadFileName,'File contained no data.');
unset($_FILES['uploadfile']['tmp_name'][$k]);
continue;
}

// Detecting uploaded file for an acceptable extension
if ($detectExtensions){
$tmp = explode('.',$uploadFileName);
if (count($tmp)==1) $uploadFileExt = '';
else $uploadFileExt = $tmp[count($tmp)-1];
if (!in_array($uploadFileExt,$acceptedExtArray)){
echoFailedRow($uploadFileNum,$uploadFileName,'Not an accepted file extension: .'.$uploadFileExt);
continue;
}
}

// Uploaded file error checking
switch ($_FILES['uploadfile']['error'][$k]){
case 0:
if (is_uploaded_file($_FILES['uploadfile']['tmp_name'][$k])) {
// Checking if uploaded file is an acceptable file type
if (!$accept_ANY && !in_array($_FILES['uploadfile']['type'][$k],$acceptedFileTypes)) {
echoFailedRow($uploadFileNum,$uploadFileName,'Not an accepted file type: '.$_FILES['uploadfile']['type'][$k]);
continue 2;
}
// Moving uploaded file to the configured destination directory
$moveufile = move_uploaded_file($_FILES['uploadfile']['tmp_name'][$k],$moveToDir.'/'.$uploadFileName);

// Checking if the move was successful
if ($moveufile) {
echoSuccessRow($uploadFileNum,$uploadFileName,'Uploaded successfully.');
continue 2;
} else echoFailedRow($uploadFileNum,$uploadFileName,'Error occurred when relocating file into designated directory.');
} else echoFailedRow($uploadFileNum,$uploadFileName,'File is not an uploaded file.');
break;
case 1:
echoFailedRow($uploadFileNum,$uploadFileName,'File size exceeds the server\'s maximum file upload size of '.ini_get('upload_max_filesize').'.');
break;
case 2:
$mfs = $_POST['MAX_FILE_SIZE'];
if ($mfs >= 1048576) $fsize = number_format($mfs/1048576,2).'MB';
else $fSize = number_format($mfs).'KB';
echoFailedRow($uploadFileNum,$uploadFileName,'File size exceeds the form\'s maximum file size of '.$fSize.'.');
break;
case 3:
echoFailedRow($uploadFileNum,$uploadFileName,'File was only partially uploaded.');
break;
case 4:
break;
default:
echoFailedRow($uploadFileNum,$uploadFileName,'An error occurred, but was not a recognised problem.');
break;
}
}
echo '</table>';
}

///////////////////////////////////
// Detecting Maximum Upload Size //
///////////////////////////////////
$umf = intval(ini_get('upload_max_filesize'));
$pms = intval(ini_get('post_max_size'));
if ($umf > $pms) $maxSize = $pms;
else $maxSize = $umf;
if ($maxSize > $maxUploadSize) $maxSize = $maxUploadSize;

// Outputting HTML Form
?>
<div>
<form enctype="multipart/form-data" action="<?= $_SERVER['../Members_LoggedIn(true257)/PHP_SELF']; ?>" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="<?= $maxSize*1048576; ?>">
<p id="uploadInputs">
<?php
// Outputing the form's file inputs
for ($a=0;$a<$numberUploads;$a++){
echo 'Upload file '.str_pad($a+1,strlen($numberUploads),'0',STR_PAD_LEFT).': <input name="uploadfile[]" type="file"><br />';
}
?>
</p>
<?php
// Output acceptable file types if they are defined
if (!$accept_ANY) echo '<p><strong>Acceptable File Types</strong>: '.implode(', ',$acceptedFileTypesKey).'</p>';
?>
<input type="submit" name="submit" value="Upload Files"></form><br />
</div>

Share this post


Link to post
Share on other sites

×

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.