2016-09-19 4 views
0

私はウェブサイト上で動作しており、小さな問題があります。 アップロードしたいファイルをサーバーにアップロードします。 私は写真の前にそのようなことをしました(profilpictureをアップロードしてください)。そのコードをExcelファイルに変更することは可能ですか?アップロードがサーバに優秀

<form enctype="multipart/form-data" action="Profil.php" method="POST">     
      <input type="hidden" name="MAX_FILE_SIZE" value="100000" /> 
      <input type="file" name="fileToUpload" id="fileToUpload"> 
      <input type="submit" id="Upload" name="submit" value="Upload"> 
     </form> 

スクリプト:

<?php 
if(isset($_POST["submit"])) 
// 
{ 

    $target_dir = "Profilbilder/"; 
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); 
    $filename = $_FILES["fileToUpload"]["name"]; 
    $file_ext = substr($filename, strripos($filename, '.')); // get file name 
    $newfilename = $_SESSION['loginUsername'].$file_ext ; 
    $uploadOk = 1; 
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 

    // Check if image file is a actual image or fake image 
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); 
    if($check !== false) 
    { 
     //echo "File is an image - " . $check["mime"] . "."; 
     $uploadOk = 1; 
    } else 
    { 
     //echo "File is not an image."; 
     $uploadOk = 0; 
    } 
    // Check file size 
    if ($_FILES["fileToUpload"]["size"] > 500000) 
    { 
     //echo "Sorry, your file is too large."; 
     $uploadOk = 0; 
    } 

    // Allow certain file formats 
    if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"&& $imageFileType != "gif") 
    { 

     //echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed."; 
     $uploadOk = 0; 
    } 

    // Check if $uploadOk is set to 0 by an error 
    if ($uploadOk == 0) 
    { 
     //echo "Sorry, your file was not uploaded."; 
    // if everything is ok, try to upload file 
    } else 
    { 
     //if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) 
     if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_dir.$newfilename)) 
     {   
      echo "The file ". basename($_FILES["fileToUpload"]["name"]). " has been uploaded."; 
     } else 
     { 
      echo "Sorry, there was an error uploading your file."; 
     } 
    } 
} 
?> 

は誰も考えていますか?

答えて

0

変更この文字列

if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"&& $imageFileType != "gif") 

は、その迅速かつ汚いこの

if($file_ext != "xls" && $file_ext != "xlsx" && $file_ext != "csv") 

ようなものにするとメタ年代をチェックし、それを行うためのより良い方法がありますが、それは動作します。あなたが.XLSに.exeファイルの名前を変更し、そのようにアップロードまたは停止何もないよう

重要な注意

は、その大規模な安全でないに注意してください。アップロードされたファイルのメタタイプを確認する方が良いでしょう。

+0

コードはアップロードするまで実行されます。しかしmove_uploaded_file($ _FILES ["fileToUpload"] ["tmp_name"]、$ target_dir。$ newfilename)は動作しません。あなたはアイデアを持っていますか? – nicoschuck

+0

getimagesizeとif check文を削除する必要があります。また、ファイル保存パスがWebサーバーによって書き込み可能であることを確認してください – Dave

関連する問題