PDA

View Full Version : Big ask this one - php + javascript view files that have been uploaded


nanny
01-30-2009, 04:06 AM
HI I have the search and insert or delete for images - how to do with files e.g. pdf, excel, word.

This is the upload for file:

<?php

include("../config/config.php");

$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 = "";

// Dynamic BODY tag to submit back to editor on success
$BODYTAG = '<body bgcolor="#EEEEEE" marginwidth="0" marginheight="0" topmargin="0" leftmargin="0" onLoad="retrieveWYSIWYG();">';
?>
<?php
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 = $_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();">';
}
}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Select File</title>
</head>
<script language="JavaScript" type="text/javascript">
var qsParm = new Array();
/* ---------------------------------------------------------------------- *\
Function : retrieveWYSIWYG()
Description : Retrieves the textarea ID for which the image will be inserted into.
\* ---------------------------------------------------------------------- */
function retrieveWYSIWYG() {
var query = window.location.search.substring(1);
var parms = query.split('&');
for (var i=0; i<parms.length; i++) {
var pos = parms[i].indexOf('=');
if (pos > 0) {
var key = parms[i].substring(0,pos);
var val = parms[i].substring(pos+1);
qsParm[key] = val;
}
}
}

/* ---------------------------------------------------------------------- *\
Function : insertImage()
Description : Inserts image into the WYSIWYG.
\* ---------------------------------------------------------------------- */
function insertPDF() {
var image = '<a href="' + document.getElementById('doc_url').value + '"><img src="' + document.getElementById('doc_type').value + '" height="16" width="16" border="0" />&nbsp;' + document.getElementById('displayname').value + '</a>';
window.opener.insertHTML(image, qsParm['wysiwyg']);
window.close();
}

</script>
<?php echo $BODYTAG; ?>
<table border="0" cellpadding="0" cellspacing="0" style="padding: 10px;">
<tr>
<td><span style="font-family: arial, verdana, helvetica; font-size: 11px; font-weight: bold;">Allowed Document types: PDF, DOC, XLS, PPL, SWF:</span>
<?php if($success < 1){ ?>
<form id="uploader" action="insert_pdf.php?wysiwyg=<?php echo $textid; ?>&" method="post" name="uploader" enctype="multipart/form-data" style="display: inline;">
<table width="380" border="0" cellpadding="0" cellspacing="0" style="background-color: #F7F7F7; border: 2px solid #FFFFFF; padding: 5px;">
<tr>
<td style="padding-bottom: 2px; padding-top: 0px; font-family: arial, verdana, helvetica; font-size: 11px;" valign="top" width="80">Select File:</td>
<td style="padding-bottom: 2px; padding-top: 0px; font-family: arial, verdana, helvetica; font-size: 11px;" width="300"><?php echo $message; ?>
<input type="file" name="picture" size="16" style="font-size: 10px; width: 100%;">
</td>
</tr>
<tr>
<td style="padding-bottom: 2px; padding-top: 0px; font-family: arial, verdana, helvetica; font-size: 11px;" valign="top" width="80">Display Name:</td>
<td style="padding-bottom: 2px; padding-top: 0px;" width="300"><input type="text" name="displayname" value="" style="font-size: 10px; width: 100%;"></td>
</tr>
</table>
<div align="right" style="padding-top: 5px;"><input type="submit" name="addpdf" value=" Submit " style="font-size: 12px;" >&nbsp;<input type="submit" value=" Cancel " onClick="window.close();" style="font-size: 12px;" ></div>
</form>
<?php }else{
?>
<input type="hidden" name="displayname" value="<?php echo $DISPLAYNAME; ?>" id="displayname">
<input type="hidden" name="doc_url" value="<?php echo $URL; ?>" id="doc_url">
<input type="hidden" name="doc_type" value="<?php echo $DOC_EXT; ?>" id="doc_type">
<?php } ?>
</td>
</tr>
</table>

</body>

</html>

nanny
01-30-2009, 04:15 AM
<?php

$folder = $_REQUEST['folder'];
$next = $_REQUEST['next'];

function galleryPreview($gallery_path, $nextFolders = null){
sleep(1);
include("../config/config.php");
$top_folder = $gallery_path;
if(strlen($nextFolders) > 1){
$pathArray = explode("/",$nextFolders);
}else{
$pathArray = "";
}
$path_info = "";
if(is_array($pathArray) && sizeof($pathArray) > 0){
$num_folders = (sizeof($pathArray)-1);
if($num_folders > 0){
for($x = 0; $x < $num_folders; $x++) {
//if($pathArray[$x] == "")
$path_info .= $pathArray[$x]."/";
}
}
}
$current_folder = $top_folder.$nextFolders;
$dirArray = array();
$fileArray = array();
$allowed_ext = "jpg,jpeg,gif";
$theseext = explode(",",$allowed_ext);
if($handle = @opendir($current_folder)){
while(false !== ($file = @readdir($handle))){
$source = explode('.', $file);
$name = current($source);
$ext = strtolower(end($source));
if($name == $ext){
$ext = "";
}
if(!in_array($ext, $theseext) && $ext != ""){
$ext = "VOID";
}

if($ext == "" && @file_exists($current_folder.$file) && $name != ""){
$dirArray[] = $file;
}elseif(@file_exists($current_folder.$file) && $name != ""){
$fileArray[] = $file;
}
}
@closedir($handle);
}
if(strlen($nextFolders)>1){
if(is_array($pathArray) && sizeof($pathArray) > 0){
$num_folders = (sizeof($pathArray)-2);
if($num_folders > 0){
for($x = 0; $x < $num_folders; $x++) {
//if($pathArray[$x] == "")
$step_up .= $pathArray[$x]."/";
}
}
}
echo '<div id="picviewerfolderpic"><a href="#" onClick="ajaxRequestURL(\'getimages.php\', \'folder='.$top_folder.'&next='.$step_up.'\', \'0\'); clearPreview();"><img src="../icons/parent.png" alt="" height="16" width="16" border="0"></a></div>';
}else{
echo '<div id="picviewerfolderpic"><img src="../icons/directory.png" alt="" height="16" width="16" border="0"></div>';
}
echo '<div id="picviewerfolderinfo">'.$current_folder.'</div>';
echo '<div id="picviewerfiles">';
foreach($dirArray as $dir){
echo '<img src="../icons/directory.png" alt="" height="16" width="16" border="0">&nbsp;&nbsp;<a href="#" onClick="ajaxRequestURL(\'getimages.php\', \'folder='.$top_folder.'&next='.$path_info.$dir.'/\', \'0\'); clearPreview();">'.$dir.'</a><br>';
}
if(sizeof($fileArray) > 0){
foreach($fileArray as $fil){
$uploadPath = explode("../", $current_folder.$fil);
$abpath = "";
for($x=0; $x<sizeof($uploadPath); $x++){
if(!in_array("../", $uploadPath)){
$abpath .= $uploadPath[$x];
}
}
$URL = $FULLDOMAIN.$SETUP_FOLDER.$abpath;
echo '<a href="#" onClick="if(confirm(\'Are you sure you wish to delete this image?\')){ ajaxDelPicURL(\'delimages.php\', \'image='.$current_folder.$fil.'\'); ajaxRequestURL(\'getimages.php\', \'folder='.$top_folder.'&next='.$path_info.'\', \'0\'); }else{ return false; }"><img src="../icons/del.gif" alt="" height="16" width="16" border="0"></a><img src="../icons/image.png" alt="" height="16" width="16" border="0">&nbsp;&nbsp;'."<a href='#' onClick='imageSwap(\"".$current_folder.$fil."\"); imageurlAdd(\"".$URL."\");'>".$fil."</a><br>";
}
}else{
echo "No Images to List.";
}
echo '</div>';
}

echo galleryPreview($folder, $next);

?>


Can I do the same for the file uploads or won't it work.

This is the delete the images:

<?php

$image = $_REQUEST['image'];

if(@unlink($image)){
echo "1";
}else{
echo "2";
}

?>


I hope someone can help - I asked the person who made this editor several times and paid money but no reply - no help - just money taken.

I would like to understand why echo 1 else echo 2 is?

Corrosive
01-30-2009, 07:27 AM
I asked the person who made this editor several times and paid money but no reply - no help - just money taken.

No way, that sucks :(