2014-02-23 55 views
0

目前我正在使用type="file"來上傳文件。但我的用例是從給定的完整文件路徑從文本框本身上傳。使用表格上傳文件文本輸入類型

<form action="upload_file.php" method="post" enctype="multipart/form-data"> 
<label for="file">Filename:</label> 
<input type="file" name="file" id="file"><br> 
<input type="submit" name="submit" value="Submit"> 
</form> 

在提交表單頁面:

<?php move_uploaded_file($_FILES["file"]["tmp_name"], 
     "upload/" . $_FILES["file"]["name"]); 
     echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; ?> 

我要指定在文本文件路徑,從那裏我要上傳圖片。我該如何實現它?

+1

你有沒有想過** **漏洞的通路,包括'..'和正在上傳可執行文件? – mpyw

+0

@CertaiN:我會在上傳文件之前檢查文件類型 – logan

+0

@CertaiN:你有這樣的想法嗎?其實我會從Excel中獲取文件路徑。與那個參考我將上傳文件。所以現在我想知道它是否可以通過文本框實現。如果是的話,我可以上傳文件路徑中提到的excel – logan

回答

2

據我所知,您不能使用客戶端計算機上的文本框上傳文件。否則,從客戶端竊取任何文件將非常容易,因爲文本框可以用JavaScript進行編輯。我希望我能正確理解你的問題。

編輯:你的意思是從你的電腦或從網址上傳?第二個可以完成。

+0

我不明白。那麼每個網站在當今世界都是風險的,因爲每個網站都有文本框。 – logan

+1

不,我的意思是**如果文本框能夠從客戶端的計算機上傳文件...但他們不能。 – iCore

+0

我在問文本框上傳。我的提交表單頁面將做到這一點。我只需要通過文本框的路徑 – logan

-1

試試這個:

<?php 

function h($str) { 
    return htmlspecialchars($str, ENT_QUOTES, 'UTF-8'); 
} 

if (
    isset($_POST['path'], $_FILES['upfile']['error']) && 
    is_int($_FILES['upfile']['error']) && 
    is_string($_POST['path']) 
) { 

    try { 

     $deep = 0; 
     foreach (explode('/', $_POST['path']) as $i => $hierarchy) { 
      if ($deep > 9) { 
       throw new RuntimeException('Hierarchy is too deep'); 
      } 
      if ($hierarchy === '') { 
       if ($_POST['path'] !== '' && $i === 0) { 
        throw new RuntimeException('Absolute path is not allowed'); 
       } 
       continue; 
      } 
      if ($hierarchy === '.') { 
       continue; 
      } 
      if (!preg_match('/\A(?!\.)[\w.-]++(?<!\.)\z/', $hierarchy)) { 
       throw new RuntimeException('Invalid directory name: ' . h($hierarchy)); 
      } 
      if (!is_dir($hierarchy)) { 
       if (!mkdir($hierarchy)) { 
        throw new RuntimeException('Failed to create directory: ' . h($hierarchy)); 
       } 
       $msgs[] = 'Created directory "' . h($hierarchy) . '"'; 
       chmod($hierarchy, 0777); 
      } 
      chdir($hierarchy); 
      ++$deep; 
     } 
     switch ($_FILES['upfile']['error']) { 
      case UPLOAD_ERR_OK: 
       break; 
      case UPLOAD_ERR_NO_FILE: 
       throw new RuntimeException('File is not choosed'); 
      case UPLOAD_ERR_INI_SIZE: 
      case UPLOAD_ERR_FORM_SIZE: 
       throw new RuntimeException('File is too large'); 
      default: 
       throw new RuntimeException('Unknown error occurred'); 
     } 
     if ($_FILES['upfile']['size'] > 1000000) { 
      throw new RuntimeException('File is too large'); 
     } 
     if (!$info = getimagesize($_FILES['upfile']['tmp_name'])) { 
      throw new RuntimeException('Invalid image file'); 
     } 
     if (false === array_search(
      $info['mime'], 
      array(
       'jpg' => 'image/jpeg', 
       'png' => 'image/png', 
       'gif' => 'image/gif', 
      ), 
      true 
     )) { 
      throw new RuntimeException('Unsupported image format'); 
     } 
     if (!preg_match('/\A(?!\.)[\w.-]++(?<!\.)\z/', $_FILES['upfile']['name'])) { 
      throw new RuntimeException('Invalid filename: ' . h($_FILES['upfile']['name'])); 
     } 
     if (!move_uploaded_file(
      $_FILES['upfile']['tmp_name'], 
      $_FILES['upfile']['name'] 
     )) { 
      throw new RuntimeException('Failed to save uploaded file'); 
     } 

     $msgs[] = 
      'Uploaded successfully: ' . 
      ($_POST['path'] === '' ? '.' : $_POST['path']) . 
      '/' . 
      $_FILES['upfile']['name'] 
     ; 

    } catch (RuntimeException $e) { 

     $msgs[] = $e->getMessage(); 

    } 

} 

header('Content-Type: text/html; charset=utf-8'); 

?> 
<!DOCTYPE html> 
<html> 
<head> 
    <title>Hierarchical Image Uploading</title> 
</head> 
<body> 
<?php if (isset($msgs)): ?> 
    <ul> 
<?php foreach ($msgs as $msg): ?> 
    <li><?=$msg?></li> 
<?php endforeach; ?> 
    </ul> 
<?php endif; ?> 
    <form enctype="multipart/form-data" method="post" action=""> 
    <fieldset> 
     <legend>Select file (Directory name and filename must match <strong>'/(?!\A\.*+\z)\A(?!\.)[\w.-]++(?&lt;!\.)\z/'</strong>)</legend> 
     Directory path: <input type="text" name="path" value=""><br /> 
     Filename(JPEG, PNG, GIF): <input type="file" name="upfile"><br /> 
     <input type="submit" value="Upload"> 
    </fieldset> 
    </form> 
</body> 
</html> 
+0

偉大的嘗試!它爲你工作? – logan

+0

是的,在我的XAMPP(Windows)上工作。 – mpyw

+0

讓我也試試,我的是Linux – logan