PDA

View Full Version : need help understanding how to allow uploaded files to replace existing ones


nanny
04-22-2009, 04:19 AM
Hi I would like to understand this code to try and allow files to be replaced instead of having to change the name of it everytime and gathering multiple files on the server not needed.
These are pdf, xls etc. file types:
the upload code I don't understand:

$folder = $docs_path;
$maxlimit = $doc_maxlimit;
$DOMAIN = $FULLDOMAIN.$SETUP_FOLDER.$docs_uploaded_to;
$textid = $_REQUEST['wysiwyg'];
$allowed_ext = "pdf,doc,xls,swf,pps";
$match = "";
$message = "";

if(isset($_REQUEST['addpdf'])){
$filesize = $_FILES['picture']['size'];
if($filesize > 0){
$filename = strtolower($_FILES['picture']['name']); // Get file name; make it all lowercase
$filename = str_replace(" ", "_", "$filename");
$filename = str_replace("+", "_", "$filename");
if($filesize < 1){
$errorList[] = "File size is empty.";
}
if($filesize > $maxlimit){
$errorList[] = "File size is too big.";
}
$file_ext = preg_split("/\./",$filename);
$allowed_ext = preg_split("/\,/",$allowed_ext);
foreach($allowed_ext as $ext){
if($ext==$file_ext[1]){
$match = "1"; // File is allowed
$DOC_EXT = $icon_folder.$file_ext[1].".gif";
$save = $folder.$filename;
if(file_exists($save)){
$errorList[]= "Cannot Move file it Already Exists";
}
}
}
}
if(!$match){
$errorList[]= "File type isn't allowed: $filename";
}
if (sizeof($errorList) == 0){
move_uploaded_file($_FILES['picture']['tmp_name'], $folder.$filename);
$URL = $DOMAIN.$filename;
$DISPLAYNAME = stripslashes($_REQUEST['displayname']);
$BODYTAG = '<body bgcolor="#EEEEEE" marginwidth="0" marginheight="0" topmargin="0" leftmargin="0" onLoad="retrieveWYSIWYG();insertPDF();">';
$success = "1";
}else{
$message = "<b style='color: red;'>The following errors were encountered:<ul style='padding: 0 0 0 0; margin: 0 0 0 0;'>";
for ($x=0; $x<sizeof($errorList); $x++){
$message .= "<li style='padding: 2 0 2 0; margin: 2 0 2 15;'>$errorList[$x]</li>";
}
$message .= "</ul>";
$BODYTAG = '<body bgcolor="#EEEEEE" marginwidth="0" marginheight="0" topmargin="0" leftmargin="0" onLoad="retrieveWYSIWYG();">';
}
}


This is the actual part I am not understanding - why the 1

$match = "1"; // File is allowed
$DOC_EXT = $icon_folder.$file_ext[1].".gif";
$save = $folder.$filename;
if(file_exists($save)){
$errorList[]= "Cannot Move file it Already Exists";
}


If I remove this will it work?

$save = $folder.$filename;
if(file_exists($save)){
$errorList[]= "Cannot Move file it Already Exists";
}


Thanks for your help...

davidj
04-22-2009, 06:56 AM
This is the actual part I am not understanding - why the 1Its a flag which is being set and is there just for error handling


if($ext==$file_ext[1]){
$match = "1"; // File is allowed



if(!$match){
$errorList[]= "File type isn't allowed: $filename";
}


You could use a copy() function in the right place which would just overwrite any file which exists with the same name and type