ajax php文件上传代码

 更新时间:2016年11月25日 16:27  点击:1429

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   <meta http-equiv="content-type" content="text/html; charset=utf-8" />

<style>
#container {
 margin: auto;
 width: 400px;
 border-top-width: 0px;
 border-right-width: 1px;
 border-bottom-width: 1px;
 border-left-width: 1px;
 border-top-style: solid;
 border-right-style: solid;
 border-bottom-style: solid;
 border-left-style: solid;
 border-top-color: #000033;
 border-right-color: #000033;
 border-bottom-color: #000033;
 border-left-color: #000033;
 background-color: #ffffff;
}
#container #header #header_left {
 float: left;
 background-image: url(images/header_left.gif);
 background-repeat: no-repeat;
 height: 42px;
 width: 45px;
}
#container #header #header_right {
 background-image: url(images/header_right.gif);
 background-repeat: no-repeat;
 height: 42px;
 width: 6px;
 float: right;
}
body {
 padding-top: 30px;
 background-color: #cccccc;
}
#container #content {
 padding: 5px;
 font-family: geneva, arial, helvetica, sans-serif;
 font-size: 12px;
 font-weight: normal;
 color: #666666;
}
#container #footer {
 font-family: geneva, arial, helvetica, sans-serif;
 font-size: 12px;
 color: #999999;
 text-align: right;
 border-top-width: 1px;
 border-right-width: 1px;
 border-bottom-width: 1px;
 border-left-width: 1px;
 border-top-style: solid;
 border-top-color: #999999;
 border-right-color: #000033;
 border-bottom-color: #000033;
 border-left-color: #000033;
 padding-top: 5px;
 padding-right: 10px;
 padding-bottom: 5px;
 padding-left: 5px;
}
#container #footer a {
 color: #999999;
 text-decoration: none;
 font-family: geneva, arial, helvetica, sans-serif;
 font-size: 10px;
}

#container #header #header_main {
 float: left;
 padding: 5px;
 font-family: geneva, arial, helvetica, sans-serif;
 font-size: 12px;
 font-weight: bold;
 color: #ffffff;
 margin-top: 5px;
 margin-right: 0px;
 margin-bottom: 0px;
 margin-left: 0px;
}
.sbtn    {
 background-image: url(images/button.gif);
 border: 1px solid #000033;
 height: 22px;
 width: 82px;
 font-family: geneva, arial, helvetica, sans-serif;
 font-size: 12px;
 color: #ffffff;
 font-weight: bold;
 background-position: center;
 padding: 0px;
 margin-top: 20px;
 margin-right: 20px;
 margin-bottom: 0px;
 margin-left: 20px;
}
button {
 font-family: geneva, arial, helvetica, sans-serif;
 font-size: 12px;
 font-weight: bold;
 color: #ffffff;
 height: 22px;
 width: 82px;
 background-image: url(images/button.gif);
}
#container #content #form1 legend {
 padding: 5px;
 margin: auto;
}
form {
 margin: 10px 5px 0px 5px;
}

 


#container #header {
 padding: 0px;
 margin-top: 0px;
 margin-right: 0px;
 margin-bottom: 0px;
 margin-left: 0px;
 background-image: url(images/header_bg.gif);
 background-repeat: repeat-x;
 height: 42px;
}
label {
 padding: 0px;
 text-align: center;
}

.msg {
 text-align:left;
  color:#666;
 background-repeat: no-repeat;
  margin-left:30px;
   margin-right:30px;
 padding:5px;
   padding-left:30px;
}

.emsg {
 text-align:left;
 margin-left:30px;
   margin-right:30px;
 color:#666;
 background-repeat: no-repeat;
 padding:5px;
   padding-left:30px;
}

#loader{
   visibility:hidden;
}

#f1_upload_form{
   height:100px;
}

#f1_error{
   font-family: geneva, arial, helvetica, sans-serif;
 font-size: 12px;
   font-weight:bold;
   color:#ff0000;
}

#f1_ok{
   font-family: geneva, arial, helvetica, sans-serif;
 font-size: 12px;
   font-weight:bold;
   color:#00ff00;

}

#f1_upload_form {
 font-family: geneva, arial, helvetica, sans-serif;
 font-size: 12px;
 font-weight: normal;
 color: #666666;
}

#f1_upload_process{
   z-index:100;
   visibility:hidden;
   position:absolute;
   text-align:center;
   width:400px;
}
</style>
  
<script language="网页特效" type="text/javascript">
<!--
function startupload(){
      document.getelementbyid('f1_upload_process').style.visibility = 'visible';
      document.getelementbyid('f1_upload_form').style.visibility = 'hidden';
      return true;
}

function stopupload(success){
      var result = '';
      if (success == 1){
         result = '<span class="msg">the file was uploaded successfully!</span><br/><br/>';
      }
      else {
         result = '<span class="emsg">there was an error during file upload!</span><br/><br/>';
      }
      document.getelementbyid('f1_upload_process').style.visibility = 'hidden';
      document.getelementbyid('f1_upload_form').innerhtml = result + '<label>file: <input name="myfile" type="file" size="30" /></label><label><input type="submit" name="submitbtn" class="sbtn" value="upload" /></label>';
      document.getelementbyid('f1_upload_form').style.visibility = 'visible';     
      return true;  
}
//-->
</script>  
</head>

<body>
       <div id="container">
            <div id="header"><div id="header_left"></div>
            <div id="header_main">max's ajax file uploader</div><div id="header_right"></div></div>
            <div id="content">
                <form action="upload.php" method="post" enctype="multipart/form-data" target="upload_target" onsubmit="startupload();" >
                     <p id="f1_upload_process">loading...<br/><img src="loader.gif" /><br/></p>
                     <p id="f1_upload_form" align="center"><br/>
                         <label>file: 
                              <input name="myfile" type="file" size="30" />
                         </label>
                         <label>
                             <input type="submit" name="submitbtn" class="sbtn" value="upload" />
                         </label>
                     </p>
                    
                     <iframe id="upload_target" name="upload_target" src="#" style="width:0;height:0;border:0px solid #fff;"></iframe>
                 </form>
             </div>

         </div>
     
</body>  

这款国外的php图片上传代码是一款好的文件与图片上传代码,并且还支持文件在线管理哦,是一款比较的好图片管理系统哦。
 代码如下 复制代码

 define('max', 2);
 
 mysql教程_connect('localhost', 'your mysql username', 'your mysql password');
 mysql_select_db('your mysql database');
 
 switch ($_post['action']) {
  case 'upload':
  
   $file = $_files['file']['tmp_name'];
   $filename = $_files['file']['name'];
   
   if($file) {
   
    $max = max * 1024 * 1024;
    $q = mysql_query("select * from `uploads` order by `batch` desc limit 1");
    $r = mysql_fetch_assoc($q);
    $batch = $r['batch'];
    
    if($filename == 'upload.zip') {
    
     $zip = zip_open($file);

     if ($zip) {
     
      while ($zip_entry = zip_read($zip)) {
       
       $size = zip_entry_filesize($zip_entry);
       
       $name = zip_entry_name($zip_entry);
       
       $type = substr(strrchr($name, '.'), 1);
       
       if (zip_entry_open($zip, $zip_entry, "r")) {
       
        $content = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
        zip_entry_close($zip_entry);
         
       }
    
       if ($size > $max) {
       
        header('location: ./?error=4');
        exit;
        
       }
       
       $error = true;
       
       if ($type == 'gif' && $error) {
       
        $error = false;
        
       }
       
       if ($type == 'png' && $error) {
       
        $error = false;
        
       }
       
       if ($type == 'jpg' && $error) {
       
        $error = false;
        
       }
       
       if ($type == 'jpeg' && $error) {
       
        $error = false;
        
       }
       
       if ($error) {
       
        header('location: ./?error=2');
        
       } else {
       
        $id = 1;
        $batch2 = $batch + 1;
        
        while (file_exists("uploads/$id/$name")) {
        
         $id++;
         
        }
        @mkdir("uploads/$id");
        
        $fp = @fopen("uploads/$id/$name", "w");
        
        if (@fwrite($fp, $content)) {
        
         $q = mysql_query("insert into `uploads` (`file`, `batch`) values ('uploads/$id/$name', '$batch2')");
         $id = mysql_insert_id();
         
        } else {
        
         header('location: ./?error=3');
         
        }
        
        fclose($fp);
        
       }
     
      }
      
      header('location: ./?batch=' . $batch2);
      
      zip_close($zip);
     
     }
    
    } else {
    
     if (filesize($file) > $max) {
     
      header('location: ./?error=4');
      exit;
      
     }
     
     $error = true;
     
     if (@imagecreatefromjpeg($file) && $error) {
     
      $error = false;
      
     }
     
     if (@imagecreatefromgif($file) && $error) {
     
      $error = false;
     }
     
     if (@imagecreatefrompng($file) && $error) {
     
      $error = false;
      
     }
     
     if ($error) {
     
      header('location: ./?error=2');
      
     } else {
     
      $id = 1;
      $batch = $batch + 1;
      
      while (file_exists("uploads/$id/$filename")) {
      
       $id++;
       
      }
      @mkdir("uploads/$id");
      
      if (@move_uploaded_file($file, "uploads/$id/$filename")) {
      
       $q = mysql_query("insert into `uploads` (`file`, `batch`) values ('uploads/$id/$filename', '$batch')");
       $id = mysql_insert_id();
       header('location: ./?image=' . $id);
       
      } else {
      
       header('location: ./?error=3');
       
      }
      
     }
    
    }
    
   } else {
   
    header('location: ./?error=1');
   
   }
   
   exit;
   
  break;
 }
 
 header('content-type: text/html; charset=iso-8859-1');
 ob_start('rewrite');
 function rewrite ($buffer) {
  $host = $_server['http_host'];
  $path = dirname($_server['php_self']);
  $absolute = "http://$host$path/";
  return preg_replace('#(href|src|action)="/#', "\1="$absolute", $buffer);
 }
 
?>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <title>jpegr - share photos instantly</title>
  <meta name="description" content="upload and share photos and images instantly, on jpegr.com" />
  <meta name="keywords" content="upload, upload images, share photos, photo sharing, image uploader" />
  <link href="/css教程/main.css" rel="stylesheet" />
  <script src="/mint/?js" type="text/网页特效"></script>
  <script src="/js/nb-object.js" type="text/javascript"></script>
 </head>
 <body>
  <h1><a href="http://jpeg.sn8.us/" title="goto jpegr home"><img src="/img/logo.gif" alt="jpegr" /></a></h1>
  <div id="menu">
   <form method="post" action="/" enctype="multipart/form-data" class="right">
    <input type="hidden" name="action" value="upload" />
    <label for="quick">quick upload</label>
    <input type="file" name="file" size="10" id="quick" />
    <input type="submit" value="upload" class="button" />
   </form>
   <a href="/">upload</a>
   <a href="/help/">help</a>
   <a href="/terms-of-service/">terms of service</a>
  </div>
  
<?php

 // recently uploaded query
 //$q = mysql_query('select * from `uploads` order by `id` desc limit 15');

 $q = mysql_query('select count(`id`) as `count` from `uploads`');
 $r = mysql_fetch_assoc($q);
 
?>
  <div id="main">
   <h2 class="right"><?php echo number_format($r['count']); ?> <strong>images hosted</strong></h2>
   
<?php

 if ($_get['p'] == 'help') {
 
?>
   <h2>help</h2>
   <div>
    <ul>
     <li>
      <strong>how do i upload an image?</strong><br />
      just use the quick upload form on the top, or goto the <a href="/">home page</a> to <a href="/">upload an image</a>.
     </li>
     <li>
      <strong>what does "you must select a file to upload!" mean?</strong><br />
      this means that you clicked <strong>upload</strong> without selecting an image file.
     </li>
     <li>
      <strong>what does "that is a not a valid jpeg, gif, or png image." mean?</strong><br />
      this means that you uploaded a file, but it was not a jpeg, gif, or png image.
     </li>
     <li>
      <strong>what does "there was a problem with the server, and we were unable to upload your image." mean?</strong><br />
      this means your file was accepted, but it did not get saved, you will need to <a href="/">try again</a>, or <a href="/">upload another image</a>.
     </li>
     <li>
      <strong>what does "the file you selected was too big, <em><?php echo max; ?>mb</em> is the maximum." mean?</strong><br />
      this means that you tried to upload a file that was too big.
     </li>
    </ul>
   </div>
<?php

 } elseif ($_get['p'] == 'terms-of-service') {
 
?>
   <h2>terms of service</h2>
   <div>
    <strong>when you upload to jpegr you agree to the following</strong>;
    <ul>
     <li>you will not use jpegr to upload pornographic content, any violation of this agreement may result in ban, and immediate removal of content.</li>
     <li>you will not abuse jpegr's upload form.</li>
     <li>any violation may result in permanent ban.</li>
    </ul>
   </div>
<?php

 } else {
 
  if (is_numeric($_get['image'])) {
  
   $q = mysql_query("select * from `uploads` where `id` = '$_get[image]'");
   $r = mysql_fetch_assoc($q);
   $root_ = 'http://' . $_server['http_host'] . dirname($_server['php_self']) . '/';
   
?>
   <h2>here is your image</h2>
   <div>
    <a href="/<?php echo $r['file']; ?>">click here to view your image</a><br /><br />
    <label for="direct">direct link to your image</label><br />
    <input type="text" id="direct" value="<?php echo $root_ . $r['file']; ?>" onfocus="this.select();" /><br />
    <label for="share" style="font-weight: bold;">share with your friends</label><br />
    <input type="text" id="share" value="<?php echo htmlspecialchars("$root_?image=$r[id]"); ?>" onfocus="this.select();" /><br />
    <label for="html">post link to myspace or website</label><br />
    <input type="text" id="html" value="<?php echo htmlspecialchars("<a href="$root_"><img src="$root_$r[file]" alt="visit jpegr" /></a>"); ?>" onfocus="this.select();" /><br />
    <label for="forum">post to a forum</label><br />
    <input type="text" id="forum" value="<?php echo htmlspecialchars("[url=$root_][img]$root_$r[file][/img][/url]"); ?>" onfocus="this.select();" /><br />
    if you want to <a href="/">upload another image</a>, you can go back or use the form below!<br /><br />
   </div>
<?php

  }
  
  if (is_numeric($_get['batch'])) {
  
   $q = mysql_query("select * from `uploads` where `batch` = '$_get[batch]'");
   
?>
   <h2>viewing batch #<?php echo ($_get['batch']) ? $_get['batch'] : 0; ?></h2>
   <div>
    to view an image in full size, just click it.<br /><br />
   
<?php

   while($r = mysql_fetch_assoc($q)) {

?>
    <a href="<?php echo $r['file']; ?>"><img src="<?php echo $r['file']; ?>" alt="image #<?php echo $r['id']; ?>" border="0" style="max-width: 75px;" /></a>
<?php

   }
   
?>
   <br /><br />
   
   <div id="slider">
    <h3>beta image slideshow</h3>
    <noscript>please turn on javascript to view our slideshows.</noscript>
    <div id="slide"></div>
   </div>
   
   <script type="text/javascript">
    
    <?php
     
     $qq = mysql_query("select * from `uploads` where `batch` = '$_get[batch]'");
     
     while($rr = mysql_fetch_assoc($qq)) {
 
    ?>
nb.slideshow.addimage('<?php echo $rr['file']; ?>');
    <?php
    
     }
       
    ?>
nb.slideshow.start();
    
   </script>
   
   </div>
   
   <h2>share this batch with your friends</h2>
   <div>
   
    <label for="share" style="font-weight: bold;">batch viewer</label><br />
    <input type="text" id="share" value="http://jpeg.sn8.us/?batch=<?php echo ($_get['batch']) ? $_get['batch'] : 0; ?>" onfocus="this.select();" /><br />
    if you want to <a href="/">upload another batch</a>, you can go back or use the form below!<br /><br />
   </div>
<?php

  }
 
?>
   <h2>upload an image or photo</h2>
   <form method="post" action="/" enctype="multipart/form-data">
    <input type="hidden" name="action" value="upload" />
<?php

  switch ($_get['error']) {
   case 1:
    $error = 'you must select a file to upload!<br />';
   break;
   case 2:
    $error = 'that is a not a valid jpeg, gif, or png image.<br />';
   break;
   case 3:
    $error = 'there was a problem with the server, and we were unable to upload your image.<br />';
   break;
   case 4:
    $error = 'the file you selected was too big, <strong>' . max . 'mb</strong> is the maximum.<br />';
   break;
  }
  
?>
    <font color="#ff0004"><?php echo $error; ?></font>
    you can upload a <strong>jpeg</strong>, <strong>gif</strong>, or <strong>png</strong> image. (max <strong><?php echo max; ?>mb</strong>)<br /><br />
    
    you can also upload a <strong>zip</strong> named <strong>upload.zip</strong>, containing multiple images.<br /><br />
    
    <label for="file">choose your file</label><br />
    <input type="file" name="file" size="40" id="file" /><br />
    <input type="submit" value="upload" class="button" />
   </form>
<?php

 }
 
?>
  </div>
  
  </div>
 </body>
</html>

??????
<?php
 mysql_connect('localhost', 'your mysql username', 'your mysql password');
 mysql_select_db('your mysql database');
 if ($_get['delete']) {
  $sql = "select * from `uploads` where `id` = '$_get[delete]'";
  $q = mysql_query($sql);
  $r = mysql_fetch_assoc($q);
  unlink($_server['document_root'] . '/' . $r['file']);
  $sql = "delete from `uploads` where `id` = '$_get[delete]'";
  $q = mysql_query($sql);
  header('location: ' . $_server['http_referer']);
  exit;
 }
?>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <meta http-equiv="content-type" content="text/html; charset=gb2312" />
  <title>jpegr administration</title>
  <style type="text/css">
   body {
    font-family: sans-serif;
    font-size: 12px;
    width: 800px;
    margin: 40px auto;
   }
   a {
    color: #105cb6;
    text-decoration: none;
   }
   a:hover {
    text-decoration: underline;
   }
   td {
    padding: 4px;
   }
   .head {
    font-weight: bold;
    color: #ffffff;
    background-color: #222222;
   }
   .item {
    background-color: #f2f2f2;
   }
   .one {
    width: 60px;
    text-align: center;
   }
   .three {
    width: 80px;
    text-align: center;
   }
   .page, .current {
    display: block;
    float: left;
    padding: 2px 4px;
    margin: 0px 4px 8px 0px;
   }
   .page {
    color: #000000;
    background-color: #eeeeee;
   }
   .current {
    font-weight: bold;
    color: #ffffff;
    background-color: #222222;
   }
  </style>
 </head>
 <body>
<?php
 $sql = 'select ceil(count(`id`) / 20) as `count` from `uploads`';
 $q = mysql_query($sql);
 $r = mysql_fetch_assoc($q);
 $pages = $r['count'];
 $offset = ($_get['page'] > 0 && $_get['page'] <= $pages) ? ($_get['page'] - 1) * 20 : 0;
 for ($i = 1; $i <= $pages; $i++) {
  $class = ($_get['page'] == $i || $i == 1 && !$_get['page']) ? ' class="current"' : ' class="page"';
?>
  <a href="?page=<?php echo $i; ?>"<?php echo $class; ?>><?php echo $i; ?></a>
<?php
 }
?>
  <br clear="all" />
  <table width="100%">
   <tr class="head">
    <td class="one">id</td>
    <td class="two">filename</td>
    <td class="three">action</td>
   </tr>
<?php
 $sql = "select * from `uploads` order by `id` desc limit $offset,20";
 $q = mysql_query($sql);
 while ($r = mysql_fetch_assoc($q)) {
?>
   <tr class="item">
    <td class="one"><?php echo number_format($r['id']); ?></td>
    <td class="two"><?php echo $r['file']; ?></td>
    <td class="three"><a href="/<?php echo $r['file']; ?>" target="_blank">view</a>, <a href="?delete=<?php echo $r['id']; ?>" onclick="return confirm('are you sure you want to delete &quot;<?php echo $r['file']; ?>&quot;?');">delete</a></td>
   </tr>
<?php
 }
?>
  </table>
 </body>
</html>

css??
html, body {
 font-family: sans-serif;
 font-size: 12px;
 width: 800px;
 margin: 40px auto;
}
a {
 color: #105cb6;
 text-decoration: none;
}
h1 {
 margin: 0px 0px 10px 0px;
}
h1 a {
 -moz-outline-width: 0px;
}
h1 a img {
 border: 0px;
}
h3 {
 margin: 4px;
}
a:hover {
 text-decoration: underline;
}
#menu {
 background-color: #e5f5ff;
 padding: 8px;
 border: 1px solid #0099ff;
 position: relative;
}
#menu a {
 font-weight: bold;
 margin: 0px 8px 0px 0px;
}
#menu a:hover {
 text-decoration: underline;
}
#menu .right {
 margin: 0px;
 padding: 0px;
 position: absolute;
 top: 4px;
 right: 4px;
}
#menu .input {
 background-color: #ffffff;
 padding: 2px;
 border: 1px solid #0066ff;
}
#menu .button {
 font-family: sans-serif;
 padding: 2px;
 cursor: pointer;
}
#menu label {
 cursor: pointer;
}
#ads {
 background-color: #fde5f3;
 margin: 8px 0px;
 border: 1px solid #ec008c;
}
#recent {
 background-color: #e6fec9;
 margin: 8px 0px;
 padding: 2px;
 border: 1px solid #9dca68;
}
#main {
 background-color: #fffee5;
 padding: 8px;
 border: 1px solid #fff200;
}
#main h2 {
 font-size: 16px;
 color: #222222;
 margin: 0px;
}
#main form {
 margin: 4px 8px;
}
#main label {
 cursor: pointer;
}
#main .button {
 font-family: sans-serif;
 margin: 2px 0px 0px 0px;
 padding: 2px;
 cursor: pointer;
}
#main .right {
 font-size: 14px;
 float: right;
 padding: 0px 0px 2px 0px;
 border-bottom: 1px solid #444444;
}
#main div {
 margin: 4px 8px;
}
#main div label {
 font-weight: bold;
}
#main div input {
 font-family: sans-serif;
 font-size: 12px;
 width: 680px;
 padding: 2px;
 margin: 2px 0px 4px 4px;
}
#main div li {
 margin-bottom: 8px;
}
#links {
 background-color: #e9e8e8;
 margin: 8px 0px 0px 0px;
 padding: 4px 0px;
 border: 1px solid #231f20;
}
.spacer {
 height: 4px;
 overflow: hidden;
}

#slider {
 color: #ffffff;
 background-color: #232323;
 height: 400px;
 margin: 10px;
 padding: 5px;
 border: 1px solid #121212;
}

#slider h3 {
 color: #ffffff;
 font-size: 14px;
 line-height: 20px;
 background-color: #343434;
 height: 20px;
 margin: -5px -5px 15px -5px;
 padding: 5px;
}

#slider #slide #image {
 max-height: 350px;
}

源码下载地址

http://down.111cn.net/php/2010/0927/20956.html

这是昨天在应用开发时用到的一款ajax图片上传功能了,方法很简单的就是把文件利用js给iframe来直接上专,如果上传文件成功返回1,再用js判断是否上传成功如果是就输出图片并显示预览效果。
 代码如下 复制代码

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>ajax文件上传</title>
<style type="text/css教程">
#f1_upload_process { display:none;}
</style>
<script language="网页特效" type="text/javascript">
<!--

function $(id)
{
 return document.getelementbyid(id);
}

function startupload(){
      if( $('myfile').value =='' )
   {
    alert('请选择要上传图片!');
    return false;
   }
   var array = $('myfile').value.split('.');
   var ext = array[1].tolowercase();  
   if( ext =="gif" || ext =="jpg" || ext =="png" )
   {   
    $('f1_upload_process').style.display = 'block';
    $('f1_upload_form').style.display = 'block';
    return true;
   }
   else
   {
   alert('只允许上传gif jpg png格式图像文件!');
   return false; 
   }
}

function stopupload(success,pic){
      var result = '';
      if (success ==1 ){
   result ='<img src='+pic+' />';
   $('logo').value=pic;
      }
      else {
         result = '<span class="emsg">logo图片上传失败,请联系开发人员!</span><br/><br/>';
      }
      $('f1_upload_process').style.display = 'none';
      $('f1_upload_form').innerhtml = result + '<br /><label><input name="myfile" type="file" size="30" /></label><label><input type="submit" name="submitbtn" class="sbtn" value="上传图片" /></label>';
      $('f1_upload_form').style.display = 'block';    
      return true;  
}

//-->
</script>
</head>

<body>
<form action="upload.php教程" method="post" enctype="multipart/form-data" target="upload_target" onsubmit="javascript:return startupload();" >
     <span id="f1_upload_process"><img src="loader.gif" /></span>
     <span id="f1_upload_form" align="center">
       <input name="myfile" type="file" id="myfile" size="30" />  
       <input type="submit" name="submitbtn" class="sbtn" value="上传图片" />
       
     </span>
    
     <iframe id="upload_target" name="upload_target" src="#" style="width:0;height:0;border:0px solid #fff;"></iframe>
 (可上传 gif,jpg,png)
</form>
</body>
</html>

upload.php

 代码如下 复制代码

<?php
   $destination_path = '../../upfile/jianjulogo/';//getcwd().directory_separator;

   $result = 0;
  
   $target_path = $destination_path . basename( $_files['myfile']['name']);

   if(@move_uploaded_file($_files['myfile']['tmp_name'], $target_path)) {
      $result = 1;
   }
   echo $target_path;
   sleep(1);


?>
<script language="javascript" type="text/javascript">window.top.window.stopupload(<?php echo $result; ?>,'<?=$target_path?>');</script>

这两款ajax文件上传代码其实都是利用了js的iframe或ajax post来实现的下面来看看代码吧。
 代码如下 复制代码

<html>
<body>
<h1>ajax file upload sample</h1><br/><input id="uplaod" name="btn_send" type="button" value="上传测试"/>
<div id=result></div>
<pre class=js name="code">
<script language=网页特效>
// 上传函数
function btn_send.onclick() {
data = ""
spliter = "-------7d8d733180846"
datadata = data + spliter + " "
datadata = data + "content-disposition: form-data; name="photofile"; filename="c:\a.txt" "
// datadata = data + "content-type: image/pjpeg" + vbcrlf
datadata = data + "content-type: text/plain" + " " + " "
text = "my name is wilson lin."
postlength = text.length + data.length + 2 + spliter.length + 4
package = data + text + " " + spliter + "-- "

alert(package)
// 把xml文档发送到web服务器
var xmlhttp = new activexobject("microsoft.xmlhttp");
xmlhttp.open("post","./upload.php",false);
xmlhttp.setrequestheader("content-type", "multipart/form-data; boundary=-----7d8d733180846");
xmlhttp.setrequestheader("content-length", postlength);
xmlhttp.send(package);
// 显示服务器返回的信息
result.innerhtml=xmlhttp.responsetext;
}
</script>
</pre>
</body>
</html>


<html>
<script language="javascript">
<!--
   var xmlhttp;
  function createxmlhttprequest() {
   if (window.activexobject) {
    xmlhttp = new activexobject("microsoft.xmlhttp");
   }
   else if (window.xmlhttprequest) {
    xmlhttp = new xmlhttprequest();
   }
  }
  function uploade(e){
   var fileadd
   fileadd = e;
   
    createxmlhttprequest();
    xmlhttp.onreadystatechange = handlestatechange;
    var url = "pic_upload.asp教程?add="+fileadd+"&timestamp="+new date().gettime();
    xmlhttp.open("get",url,true);
    xmlhttp.send(null);
  
  }
  function handlestatechange() {
   document.getelementbyid("content").innerhtml = "这里写进度条";
   if (xmlhttp.readystate == 4) {
    if (xmlhttp.status == 200) {
     
     document.getelementbyid("content").innerhtml = xmlhttp.responsetext;
     
    }
    else{
     //alert(xmlhttp.status);
                                                                                  alert('错误,请联系管理员!');
     
    }
   }
  }
//-->
</script>
 <body>
<input type=file name="mefile" id="fileadd" onchange="alert(document.getelementbyid('fileadd').value);">
<input type="submit" value="上传" onclick="uploade(document.getelementbyid('fileadd').value);">
<div id="content"></div>
 </body>
</html>

 

 代码如下 复制代码
<?php教程
 
class mycache
{
  private $cache;
  function  __construct()
  {
    $this->cache = new memcache();
    // you can replace localhost by memcached server ip addr and port no.
    $this->cache->connect('localhost', 10987);
  }
 
  function get_data($key)
  {
    $data = $this->cache->get($key);
    if($data != null)
      return $data;
    else
    {
      if($this->cache->getresultcode() == memcached::res_notfound)
      {
        //do the databse query here and fetch data
        $this->cache->set($key,$data_returned_from_database);
      }
      else
      {
        error_log('no data for key '.$key);
      }
    }
  }
}
 
$cache = mycache();
$cache->get_data('foo');
 
?>


memcache 在什么情况下被使用,什么情况下不要使用?
你在何时应该使用 memcache,又要在何时避免使用它?现在你已经知道了,memcahced 是被设计为减轻数据库教程端压力的。但是你最好能制定一个良好的策略,来想办法让 memcached 来尽可能的缓存那些最影响性能的查询。你可以试着为应用中的所有查询做一些执行时间日志,可以帮助你来分析哪些内容是要重点被缓存的。

现在假设你正在运营一个电子商务网站。 你可以在 memcached 中缓存产品的简介、运送信息,或者其它一些需要复杂查询的数据,等等。当一个产品页被加载的时候,上面提到的数据将会跳过数据库查询,直接从缓存中取得。缓存可以大大的改变你的网站整体性能表现,你只需要记得在后台更新产品的时候,把这些缓存一并更新就行了。

还有一些情况下,缓存数据并不是一个好主意,比如在一个数据被频繁更新的时候,每一次数据的更新,我们都需要去同时更新缓存,缓存的命中率不高,会导致一些额外的性能牺牲。这种情况下,或许直接查数据库会更好一些。

memcached 的安全性
如果你了解了 memcached 的工作流程, 你可能已经注意到了,在访问缓存的过程中,没有任何权限控制的相关流程。如果你的数据不是非常重要的,你大可不必担心这方面的安全问题。如果你需要的话,以下几点可以协助你更完全的使用它:

使用唯一的 key:因为在 memcached 中的数据是以一个大的数组形式存在的,所以你应该使用唯一的 key。访问你的数据的唯一办法就是通过你保存数据时的 key,除此之外再没有其它可查询的办法。
保证你的 memcached 器安全: 因为 memcached 本身并没有身份验证机制,所以对 memcached 的服务器查询,都应该通过防火墙进行。你可以在防火墙上设定规则,哪些服务器是允许被访问的,哪些是不允许被访问的。
加密你的数据: 你可以将数据和 key 通过加密的方式保存在 memcached 中。 这需要花费一些额外的 cpu 时间,但是为了你的数据安全,在情况允许的情况下,这个方法值得你去尝试。

[!--infotagslink--]

相关文章

  • php读取zip文件(删除文件,提取文件,增加文件)实例

    下面小编来给大家演示几个php操作zip文件的实例,我们可以读取zip包中指定文件与删除zip包中指定文件,下面来给大这介绍一下。 从zip压缩文件中提取文件 代...2016-11-25
  • Jupyter Notebook读取csv文件出现的问题及解决

    这篇文章主要介绍了JupyterNotebook读取csv文件出现的问题及解决,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2023-01-06
  • 不打开网页直接查看网站的源代码

      有一种方法,可以不打开网站而直接查看到这个网站的源代码..   这样可以有效地防止误入恶意网站...   在浏览器地址栏输入:   view-source:http://...2016-09-20
  • php 调用goolge地图代码

    <?php require('path.inc.php'); header('content-Type: text/html; charset=utf-8'); $borough_id = intval($_GET['id']); if(!$borough_id){ echo ' ...2016-11-25
  • Photoshop打开PSD文件空白怎么解决

    有时我们接受或下载到的PSD文件打开是空白的,那么我们要如何来解决这个 问题了,下面一聚教程小伙伴就为各位介绍Photoshop打开PSD文件空白解决办法。 1、如我们打开...2016-09-14
  • C#操作本地文件及保存文件到数据库的基本方法总结

    C#使用System.IO中的文件操作方法在Windows系统中处理本地文件相当顺手,这里我们还总结了在Oracle中保存文件的方法,嗯,接下来就来看看整理的C#操作本地文件及保存文件到数据库的基本方法总结...2020-06-25
  • 解决python 使用openpyxl读写大文件的坑

    这篇文章主要介绍了解决python 使用openpyxl读写大文件的坑,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-13
  • C#实现HTTP下载文件的方法

    这篇文章主要介绍了C#实现HTTP下载文件的方法,包括了HTTP通信的创建、本地文件的写入等,非常具有实用价值,需要的朋友可以参考下...2020-06-25
  • SpringBoot实现excel文件生成和下载

    这篇文章主要为大家详细介绍了SpringBoot实现excel文件生成和下载,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-02-09
  • JS基于Mootools实现的个性菜单效果代码

    本文实例讲述了JS基于Mootools实现的个性菜单效果代码。分享给大家供大家参考,具体如下:这里演示基于Mootools做的带动画的垂直型菜单,是一个初学者写的,用来学习Mootools的使用有帮助,下载时请注意要将外部引用的mootools...2015-10-23
  • JS+CSS实现分类动态选择及移动功能效果代码

    本文实例讲述了JS+CSS实现分类动态选择及移动功能效果代码。分享给大家供大家参考,具体如下:这是一个类似选项卡功能的选择插件,与普通的TAb区别是加入了动画效果,多用于商品类网站,用作商品分类功能,不过其它网站也可以用,...2015-10-21
  • JS实现自定义简单网页软键盘效果代码

    本文实例讲述了JS实现自定义简单网页软键盘效果。分享给大家供大家参考,具体如下:这是一款自定义的简单点的网页软键盘,没有使用任何控件,仅是为了练习JavaScript编写水平,安全性方面没有过多考虑,有顾虑的可以不用,目的是学...2015-11-08
  • php 取除连续空格与换行代码

    php 取除连续空格与换行代码,这些我们都用到str_replace与正则函数 第一种: $content=str_replace("n","",$content); echo $content; 第二种: $content=preg_replac...2016-11-25
  • php无刷新利用iframe实现页面无刷新上传文件(1/2)

    利用form表单的target属性和iframe 一、上传文件的一个php教程方法。 该方法接受一个$file参数,该参数为从客户端获取的$_files变量,返回重新命名后的文件名,如果上传失...2016-11-25
  • Php文件上传类class.upload.php用法示例

    本文章来人大家介绍一个php文件上传类的使用方法,期望此实例对各位php入门者会有不小帮助哦。 简介 Class.upload.php是用于管理上传文件的php文件上传类, 它可以帮...2016-11-25
  • php简单用户登陆程序代码

    php简单用户登陆程序代码 这些教程很对初学者来讲是很有用的哦,这款就下面这一点点代码了哦。 <center> <p>&nbsp;</p> <p>&nbsp;</p> <form name="form1...2016-11-25
  • php批量替换内容或指定目录下所有文件内容

    要替换字符串中的内容我们只要利用php相关函数,如strstr,str_replace,正则表达式了,那么我们要替换目录所有文件的内容就需要先遍历目录再打开文件再利用上面讲的函数替...2016-11-25
  • PHP实现清除wordpress里恶意代码

    公司一些wordpress网站由于下载的插件存在恶意代码,导致整个服务器所有网站PHP文件都存在恶意代码,就写了个简单的脚本清除。恶意代码示例...2015-10-23
  • PHP文件上传一些小收获

    又码了一个周末的代码,这次在做一些关于文件上传的东西。(PHP UPLOAD)小有收获项目是一个BT种子列表,用户有权限上传自己的种子,然后配合BT TRACK服务器把种子的信息写出来...2016-11-25
  • jQuery实现简单的文件上传进度条效果

    本文实例讲述了jQuery实现文件上传进度条效果的代码。分享给大家供大家参考。具体如下: 运行效果截图如下:具体代码如下:<!DOCTYPE html><html><head><meta charset="utf-8"><title>upload</title><link rel="stylesheet...2015-11-24