2017-05-25 5 views
1

私はそれが入っているディレクトリにアップロードするフォームを持っていますが、selectからサブディレクトリのサブディレクトリまたはサブディレクトリにアップロードする必要があります。しかし、どのように選択情報を使用するか分からない。PHPは選択したサブディレクトリにファイルをアップロードします

マイフォルダ構造は、フォルダの4層

Produce 
Produce/Meat 
Produce/Meat/Beef 
Produce/Meat/Beef/Portions 
Produce/Meat/Beef/Packaged 
Produce/Vegtables 
Produce/Vegetables/Fresh 
Produce/Vegetables/Fresh/Local etc,. 

これは私のフォーム

<form id="Upload" action="upload_file.php" method="post" enctype="multipart/form-data"> 
<br> 
     <label for="fileSelect"><strong>There is a 10MB limit Filesize limit.</strong> Allowed file types are pictures, MSWord, MSExcel, PDF, and plain text. Navigate and choose:</label> 
     <input type="file" name="file" id="fileSelect"><br><br> 
     Upload to: 
     <select name="folder"><option value="this" selected>This folder</option><option value="BBB">Meat</option><option value="CCC">Meat/Beef</option><option value="DDD">Meat/Beef/Portions</option><option value="EEE">Meat/Beef/Packaged</option><option value="FFF">Vegetables</option><option value="GGG">Vegetables/Fresh</option><option value="HHH">Vegetables/Fresh/Local</option><option value="III">Vegetables/Fresh/Packaged</option></select> 
     <input class="button" type="submit" name="action" value="Upload to Shared Folder"><br><br> 
    </form> 

はその後upload_file.phpは、このPHPコード

<?php 

if(isset($_FILES["file"]["error"])){ 
    if($_FILES["file"]["error"] > 0){ 
     echo "Error: " . $_FILES["file"]["error"] . "<br>"; 
    } else{ 
     $allowed = array("jpg" => "image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png", "doc" => "application/msword", "docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "xls" => "application/vnd.ms-excel", "xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "pdf" => "application/pdf", "txt" => "text/plain"); 
     $filename = $_FILES["file"]["name"]; 
     $filetype = $_FILES["file"]["type"]; 
     $filesize = $_FILES["file"]["size"]; 

     // Verify file extension 
     $ext = pathinfo($filename, PATHINFO_EXTENSION); 
     if(!array_key_exists($ext, $allowed)) die("Error: This file is not an accepted file type.</br></br>"); 

     // Verify file size - 10MB maximum 
     $maxsize = 200000 * 60; 
     if($filesize > $maxsize) die("Error: File size is larger than the allowed 10MB limit.</br></br>"); 

     // Verify MYME type of the file 
     if(in_array($filetype, $allowed)){ 
      // Check whether file exists before uploading it 
      if(file_exists("general/" . $_FILES["file"]["name"])){ 
       echo $_FILES["file"]["name"] . " already exists. Go back and choose another file or rename the original.</br></br>"; 
      } else{ 
       move_uploaded_file($_FILES["file"]["tmp_name"], "" . $_FILES["file"]["name"]); 
       echo "The file was uploaded successfully.</br></br>"; 
      } 
     } 
     else{ 
      echo "Error: There was a problem uploading the file - please try again."; 
     } 
    } 
} else{ 
    echo "Error: Invalid parameters - something is very wrong with this upload."; 
} 
?> 

を持っている方法はあります私は選択値を取って、選択したサブディレクトリにアップロードできますか?

+0

'と等しい '値で' isset'を使用し、ファイルを選択した選択肢に移動するか、より良いスイッチ/ケースhttp://php.net/manual/en/control-構造体.switch.php –

+0

@ Fred-ii-これを行う方法がわかりません、それは選択値ですか?選択値を実際のフォルダ名にすることができます。フォルダ名は変更されません。 – Kilisi

+0

私はあなたのために以下のものを投稿しました。コメントは長すぎるでしょう。 –

答えて

1

これを実行する方法はたくさんありますので、ここでは一つの方法です:POSTの配列が設定されている場合、それは値に等しいかどうかを確認するために、チェックの内側に、(ドロップダウンを選択)を参照するには

チェックがオプションから選択したフォルダ内にファイルを移動します。

if(isset($_POST['folder'])){ 

    if($_POST['folder'] == 'this'){ 

    // move_uploaded_file to said folder 

    } 

    if($_POST['folder'] == 'BBB'){ 

    // move_uploaded_file to said folder 

    } 

    // ... do the same for the others below 


} 

デフォルトのアップロードフォルダを設定することもできます。


編集:

注:// My snippet START内部の私の元のスニペット--- // My snippet ENDを探してください。

<?php 

if(isset($_FILES["file"]["error"])){ 
    if($_FILES["file"]["error"] > 0){ 
     echo "Error: " . $_FILES["file"]["error"] . "<br>"; 
    } else{ 
     $allowed = array("jpg" => "image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png", "doc" => "application/msword", "docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "xls" => "application/vnd.ms-excel", "xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "pdf" => "application/pdf", "txt" => "text/plain"); 
     $filename = $_FILES["file"]["name"]; 
     $filetype = $_FILES["file"]["type"]; 
     $filesize = $_FILES["file"]["size"]; 

     // Verify file extension 
     $ext = pathinfo($filename, PATHINFO_EXTENSION); 
     if(!array_key_exists($ext, $allowed)) die("Error: This file is not an accepted file type.</br></br>"); 

     // Verify file size - 10MB maximum 
     $maxsize = 200000 * 60; 
     if($filesize > $maxsize) die("Error: File size is larger than the allowed 10MB limit.</br></br>"); 

     // Verify MYME type of the file 
     if(in_array($filetype, $allowed)){ 
      // Check whether file exists before uploading it 
      if(file_exists("general/" . $_FILES["file"]["name"])){ 
       echo $_FILES["file"]["name"] . " already exists. Go back and choose another file or rename the original.</br></br>"; 
      } else{ 

// My snippet START 
    if(isset($_POST['folder'])){ 

     if($_POST['folder'] == 'this'){ 

     // move_uploaded_file to said folder 

     $uploaddir = '/var/www/uploads/Produce/'; 
     $uploadfile = $uploaddir . basename($_FILES['file']['name']); 

     move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile); 
        echo "The file was uploaded successfully.</br></br>"; 

     } 

     if($_POST['folder'] == 'BBB'){ 

     // move_uploaded_file to said folder 

     $uploaddir = '/var/www/uploads/Produce/Meat/'; 
     $uploadfile = $uploaddir . basename($_FILES['file']['name']); 

     move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile); 
        echo "The file was uploaded successfully.</br></br>"; 

     } 

     // ... do the same for the others below 


    } 

// My snippet END 

      } 
     } 
     else{ 
      echo "Error: There was a problem uploading the file - please try again."; 
     } 
    } 
} else{ 
    echo "Error: Invalid parameters - something is very wrong with this upload."; 
} 
?> 
+0

私はこれを行うことができますが、上記の私のコードにどこに置くべきか、「//アップロードファイルを上記のフォルダに移動してください」のどこで使うべきかは分かりません。 – Kilisi

+0

@Kilisi 'move_uploaded_file'がある' else { move_uploaded_file ....} 'の中にそのスニペット全体を入れると、基本的にはそのメソッドをコピーして、それに応じてアップロードフォルダを変更します。 –

+1

@Kilisiは私の答えをリロードし、** Edit:**を見てください。あなたは他のフォルダを使い続け、私がやった方法にちょうど従うことができます。 '/ var/www/uploads/Produce /'を使用しましたが、サーバのシステムパスに合わせて変更する必要があります。 –

関連する問題