2015-05-19 51 views
0

我正在寻找一些帮助我的PHP脚本为我的网站,我正在教一小群学生,有人问,我们究竟是如何把它放在互联网上?所以我做了一个php上传脚本,以便他们可以上传到我的网站与他们的朋友分享。PHP防止上传文件的种类,删除上传文件中的空格

我有两个问题:

1)究竟我如何使它所以当有人上传了一个文件,它会删除所有的空间中上传的文件名? 2)我究竟如何做到这一点,我可以防止人们上传某些类型的文件? (如JPG文件,PNG文件EXT)

继承人我的代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> 
    <head> 
     <meta http-equiv="X-UA-Compatible" content="IE=edge" /> 
     <meta name="viewport" content= 
     "width=device-width, initial-scale=1" /> 
     <title> 
     File Upload 
     </title> 
     <link rel="icon" type="image/png" href="icons/favicon.png" /> 
     <link href="css/bootstrap.min.css" rel="stylesheet" type= 
     "text/css" /> 
     <script src="js/bootstrap.min.js" type="text/javascript"></script> 
     <script src="js/jquery-1.11.3.min.js" type="text/javascript"></script> 
     <script src="js/bootstrap-filestyle.js" type="text/javascript"></script> 
     <script src= 
     "https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js" 
     type="text/javascript"></script> 
     <script src= 
     "https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js" 
     type="text/javascript"></script> 
    </head> 
    <body> 
     <div class="container" style= 
     "margin-top: 30px;margin-bottom: 30px"> 
     <div class="row"> 
      <div class="span12"> 
       <h2 style="text-align: center;"> 
        <strong>Alydus.net File Upload</strong> 
       </h2> 
       <div class="well"> 
        <form role="form" enctype="multipart/form-data" method= 
        "post"> 
        <h3> 
         Upload a file 
        </h3> 
        <hr /> 
        <div class="row"> 
         <div class="col-xs-6"> 
          <div class="form-group"> 
           <input type="file" name="example" id="input01" 
           class="filestyle" data-buttonbefore="true" /> 
           <?php 
           $dir_upload = '/usr/local/nginx/html/upload/uploads/'; 
           $max_size = 200000000; 

           $file = $_FILES['example']; 

           if ($file['size'] <= $max_size && $file['size'] > 0) { 
             $new_name = time() . '-' . $file['name']; 
             $copied = copy($file['tmp_name'], $dir_upload . $new_name); 

             if ($copied) { 
               print("Successfully uploaded to server, you can download/view the uploaded file now."); 
             } else { 
               print("An unknown error has occurred."); 
             } 
           } else { 
             print(""); 
           } 
           ?> 
          </div> 
         </div> 
         <div class="col-xs-2"> 
          <div class="form-group"> 
           <button type="submit" class= 
           "btn btn-primary">Upload File</button> <a href= 
           "http://alydus.net/upload/uploads/<?=$new_name; ?>" 
           class="btn btn-primary" role= 
           "button">Download/view uploaded file</a> 
          </div> 
         </div> 
        </div> 
        </form> 
       </div> 
       <div class="container" style= 
        "margin-top: 30px;margin-bottom: 30px"> 
        <div class="row"> 
        <div class="span12"> 
         <div class="well"> 
          <form role="form" enctype="multipart/form-data" 
           method="post"> 
           <h3> 
           Instructions 
           </h3> 
           <hr /> 
           <div class="row"> 
           <div class="col-xs-6"> 
            <div> 
             <ul> 
              <li>Click choose file 
              </li> 
              <li>Select your file you'd like to 
              upload 
              </li> 
              <li>Click upload file once 
              </li> 
              <li>When the file is finished you 
              should see a complete message, then 
              click view/download uploaded file 
              </li> 
              <li>You can then copy the link and send 
              it to anyone so they can view your 
              images, your html files, and even 
              download your files! 
              </li> 
             </ul> 
            </div> 
           </div> 
           </div> 
          </form> 
         </div> 
        </div> 
        </div> 
        <div class="container" style= 
        "margin-top: 30px;margin-bottom: 30px"> 
        <div class="row"> 
         <div class="span12"> 
          <div class="well"> 
           <form role="form" enctype="multipart/form-data" 
           method="post"> 
           <h3> 
            Files Uploaded 
           </h3> 
           <hr /> 
           <div class="row"> 
            <div class="col-xs-6"> 
             <div style= 
              'width: 1015px; height: 2500px; overflow: hidden;'> 
              <center> 
              <iframe style= 
               'width: 1015px; height: 2500px;' src= 
               'http://alydus.net/upload/uploads/index.php'> 
              </iframe> 
              </center> 
             </div> 
            </div> 
            <div class="col-xs-2"></div> 
            <center> 
             © <?php echo date("Y"); ?> Copyright 
             Alydus.net 
            </center> 
           </div> 
           </form> 
          </div> 
         </div> 
        </div> 
        </div> 
       </div> 
      </div> 
     </div> 
     </div> 
    </body> 
</html> 

您可以在行动,在http://alydus.net/upload

+0

有太多可能的答案,或者对这种格式太好的答案太长。请添加详细信息以缩小答案集或隔离可以在几个段落中回答的问题。我建议您找到一个开发论坛(可能是[reddit](https://www.reddit.com/)?)找出共性。然后,当您遇到特定的编码问题时,请回到StackOverflow,我们很乐意提供帮助。 –

+0

对不起周杰伦,我并不是在寻找'一般'帮助,我只是需要任何方法,我可以做到这一点。我现在更新我的帖子。谢谢。 – Alydus

回答

1

1)究竟我如何使它所以当有人上传查看一个文件它会 删除上传的文件名中的所有空格?

这可以通过使用preg_replace("/\s/g", "", $new_name); 实施例它取代由T每一个空间来实现。 https://regex101.com/r/tR8uJ7/1

2)究竟是如何让我的,所以我可以防止有人上传某些 类型的文件? (如jpg文件,png文件分机)

在移动上传的文件之前,您可以通过正则表达式检查文件扩展名。 preg_match("/^(.*)\.(png|jpeg|exe)$/", $filename);这个正则表达式匹配任何以任何字符开头并以.png,.jpg或.exe结尾的字符串。 https://regex101.com/r/tJ7tH6/1

您可以根据自己的需要进行调整。如果你不熟悉正则表达式在网上搜索它 - 那里有很多教程。 请记住,文件扩展名可以很容易伪造。

在这里,我们去一个样本这不是测试,但应该工作,否则,告诉我什么是不工作的。

      <?php 
           $dir_upload = ''; 
           $max_size = 200000000; 
           $regexAllowedFileExtensions = "/^(.*)\.(png|jpeg|exe)$/"; 

           $file = $_FILES['example']; 

           if ($file['size'] <= $max_size && $file['size'] > 0) { 
            if (preg_match($regexAllowedFileExtensions, $file['name'])) 
            { 
              $filename_temp = str_replace(" ", "", $file['name']); 
              $new_name = time() . '-' . $filename_temp; 

              $copied = move_uploaded_file($file['tmp_name'], __DIR__ . '/' . $new_name); 

              if ($copied) { 
               print("Successfully uploaded to server, you can download/view the uploaded file now."); 
              } else { 
               print("An unknown error has occurred."); 
              } 
            } 
            else{ 
             print("The file type is not allowed."); 
            } 
           } else { 
            print(""); 
           } 
          ?> 
+0

我应该在哪里放preg_replace(“/ \ s/g”,“”,$ new_name);在我的PHP代码?我试过如果($ file ['size'] <= $ max_size && $ file ['size']> 0){和其他一些地方,但它仍然使上传的文件有空格,如下所示:http:/ /alydus.net/upload/uploads/1432060176-Homemade%20Grenade.png – Alydus

+0

我已经更新了该脚本,并且应该能够正常工作。看到你的共享的HTML/PHP与上面的PHP片段:http://www.codeshare.io/kWEnT – AMartinNo1

+0

如果你去http://alydus.net/upload我更新了代码共享。io脚本,上传后当你去查看文件,它下载这真的很奇怪的文件,没有文件扩展名,只有tmp号码和破折号? – Alydus