From: Alessandro P. (T. / J578) <al...@ti...> - 2002-05-14 13:29:23
|
Hi all, Shaun Murray sent me this additions to our upload code in phpWS 0.8.x, i'm fwd'ing it so the list so code owners could eventually merge his additions. Bye, Alessandro --- Fwd starts here --- Hi, I was just browsing through the developers mailing list for phpwebsite and came across a post of yours about file uploads. I hacked the old calendar picture upload code out some time ago and perhaps this is useful. I have two versions, one for uploading images which produces an image tag so our 'normal' non-techy journos can paste in images and another that produces a link to a PDF file including an icon. Typically we sit with two windows open. One to post images and the other writing stories. I add links to the two upload tools in the admin menus. Not fancy but it works for us. I'm sure there's a better way of doing it in one file with icon selecting and image sizes in the tags etc. I still don't allow uploads for users. <? /************************************************************************************************************************* ** uploadimage.php - uploads a file to the server ** ************************************************************************************************************************** ** Unfortunately, I do not know the originator of this code. For the calendar, it assists with transferring ** a graphic from the admin's hard drive to the server. It also allows you to pick whether all admin ** options have access to these graphics. I was planning to have multiple layers of admin status but ** I have not coded yet. The graphic limits were put in to facilitate this. ** ** Author: Matthew McNaney ** **************************************************************************************************************************/ if(!isset($mainfile)) { include("mainfile.php"); } include("open_session.php"); include("auth.inc.php"); include("header.php"); include("config.php"); require("fileupload.class"); if($admintest) echo "<div style=\"text-align : center\"><a href=\"admin.php\">Back to Admin Menu</a></div><br />"; ?> <table cellspacing="1" cellpadding="3" border="0" width="100%"> <tr><td class="type4">Upload File to Image directory (ie. /i) </td></tr><tr> <td class="type5">Once uploaded, image can be inserted into stories as <b><img src="../i/pic.jpg>"</b><br> <? #--------------------------------# # Variables #--------------------------------# // The name of the file field in your form. $FILENAME = "userfile"; // ACCEPT mode - if you only want to accept // a certain type of file. // possible file types that PHP recognizes includes: // // OPTIONS INCLUDE: // text/plain // image/gif // image/jpeg // image/png // $ACCEPT = "image/gif"; // If no extension is supplied, and the browser or PHP // can not figure out what type of file it is, you can // add a default extension - like ".jpg" or ".txt" $EXTENSION = ""; // SAVE_MODE: if your are attempting to upload // a file with the same name as another file in the // $PATH directory // // OPTIONS: // 1 = overwrite mode // 2 = create new with incremental extention // 3= do nothing if exists, highest protection $SAVE_MODE = 2; #--------------------------------# # PHP #--------------------------------# function print_file($file, $type, $mode) { if($file) { if(ereg("image", $type)) { echo " <img src=\"" . $file . "\" border=\"0\" alt=\"\">"; } else { $userfile = fopen($file, "r"); while(!feof($userfile)) { $line = fgets($userfile, 255); switch($mode){ case 1: echo $line; break; case 2: echo str_replace("\n", "<br />", (ereg_replace("\t", " ", htmlentities($line)))); break; } } } } } $upload = new uploader; $upload->max_filesize(100000); $upload->max_image_size(400,400); if($upload->upload("$FILENAME", "$ACCEPT", "$EXTENSION")) { while(list($key, $var) = each($upload->file)){ echo $key . " = " . $var . "<br />"; } if($upload->save_file("./i/", $SAVE_MODE)) { print("<p>Saved as: ." . $upload->new_file . "<p>"); print_file($upload->new_file, $upload->file["type"], 2); } } if($upload->errors) { while(list($key, $var) = each($upload->errors)){ echo "<br />" . $var . "<br />"; } } if ($NEW_NAME) { print("<p>Insert it into your story as<b><img border=\"1\" src=\"../i/$NEW_NAME\"></b></p>"); } #--------------------------------# # HTML FORM #--------------------------------# if (!$NEW_NAME) { echo " <form enctype=\"multipart/form-data\" action=\"$PHP_SELF\" method=\"POST\"> <input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"100000\" /> Send this file: <input name=\"userfile\" type=\"file\" /><br /><br /> <input type=\"submit\" value=\"Send this file\" /> </form> <br /> "; } else { echo " <a href=\"./uploadimage.php\">Upload Another File</a> "; } ?> <? if ($ACCEPT) { print("This form only accepts <span class=\"boldtext\">" . $ACCEPT . "</span> files\n"); } echo "</td></tr></table>"; include("footer.php"); ?> <? /************************************************************************************************************************* ** uploadpdf.php - uploads a file to the server ** ************************************************************************************************************************** ** Unfortunately, I do not know the originator of this code. For the calendar, it assists with transferring ** a graphic from the admin's hard drive to the server. It also allows you to pick whether all admin ** options have access to these graphics. I was planning to have multiple layers of admin status but ** I have not coded yet. The graphic limits were put in to facilitate this. ** ** Author: Matthew McNaney ** **************************************************************************************************************************/ if(!isset($mainfile)) { include("mainfile.php"); } include("open_session.php"); include("auth.inc.php"); include("header.php"); include("config.php"); require("fileupload.class"); if($admintest) echo "<div style=\"text-align : center\"><a href=\"admin.php\">Back to Admin Menu</a></div><br />"; ?> <table cellspacing="1" cellpadding="3" border="0" width="100%"> <tr> <td class="type4">Upload PDF File to docs directory (ie. /docs) </td> </tr><tr> <td class="type5">Once uploaded, doc can be inserted into stories as <b><a href="../docs/file.pdf"><img align="middle" border="0" src="../images/icons/pdficon.gif"</b>>Description</a><br> <? #--------------------------------# # Variables #--------------------------------# // The name of the file field in your form. $FILENAME = "userfile"; // ACCEPT mode - if you only want to accept // a certain type of file. // possible file types that PHP recognizes includes: // // OPTIONS INCLUDE: // text/plain // image/gif // image/jpeg // image/png // $ACCEPT = "image/gif"; // If no extension is supplied, and the browser or PHP // can not figure out what type of file it is, you can // add a default extension - like ".jpg" or ".txt" $EXTENSION = ""; // SAVE_MODE: if your are attempting to upload // a file with the same name as another file in the // $PATH directory // // OPTIONS: // 1 = overwrite mode // 2 = create new with incremental extention // 3= do nothing if exists, highest protection $SAVE_MODE = 2; #--------------------------------# # PHP #--------------------------------# function print_file($file, $type, $mode) { if($file) { if(ereg("image", $type)) { echo "<img src=\"" . $file . "\" border=\"0\" alt=\"\">"; } else { $userfile = fopen($file, "r"); while(!feof($userfile)) { $line = fgets($userfile, 255); switch($mode){ case 1: echo $line; break; case 2: echo str_replace("\n", "<br />", (ereg_replace("\t", " ", htmlentities($line)))); break; } } } } } $upload = new uploader; $upload->max_filesize(500000); if($upload->upload("$FILENAME", "$ACCEPT", "$EXTENSION")) { while(list($key, $var) = each($upload->file)){ echo $key . " = " . $var . "<br />"; } if($upload->save_file("./docs/", $SAVE_MODE)) { print("<p>Saved as: ." . $upload->new_file . "<p>"); } } if($upload->errors) { while(list($key, $var) = each($upload->errors)){ echo "<br />" . $var . "<br />"; } } if ($NEW_NAME) { print("<p>Access it as <b><a href="../docs/$NEW_NAME"><img align="middle" border="0" src=\"../images/icons/pdficon.gif\"</b>>$NEW_NAME</a></p>"); } #--------------------------------# # HTML FORM #--------------------------------# if (!$NEW_NAME) { echo " <form enctype=\"multipart/form-data\" action=\"$PHP_SELF\" method=\"POST\"> <input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"500000\" /> Send this file: <input name=\"userfile\" type=\"file\" /><br /><br /> <input type=\"submit\" value=\"Send this file\" /> </form> <br /> "; } else { echo " <a href=\"./uploadpdf.php\">Upload Another File</a> "; } ?> <? if ($ACCEPT) { print("This form only accepts <span class=\"boldtext\">" . $ACCEPT . "</span> files\n"); } echo "</td></tr></table>"; include("footer.php"); ?> -- Alessandro "TXM" Pisani - alextxm at tin dot it - ICQ: #2209087 "I will carry you through, kicking and screaming, and in the end you will thank me" - Tyler Durden [from "Fight Club"] |