2015-03-25 90 views
1

我有一個頁面上有兩種形式,應該給用戶posibility加載文件到服務器(從網址或從用戶的PC)加載文件到服務器

<form method="post" action="bigorder.php" name="photourl"> 
    <label for="photoorig">URL</label> 
    <input type="url" name="photoorig" placeholder=""> 
    <input type="submit" value="Load" name="photoload"> 
    <br> 
</form> 

<form method="post" action="bigorder.php" name="photofile" enctype="multipart/form-data"> 
    <label for="photoloc">Load own file</label> 
    <input type="file" name="photoloc" id="photoloc"> 
    <input type="submit" value="Load" name="photoload2"> 
</form> 

和PHP

<?php 
    $tmpname=rand().".jpg"; 
    if ($_POST['photoorig']) { 
    $file=file_get_contents($_POST['photoorig']); 
    $fp = fopen("/var/www/html/uploads/tmp/".$tmpname, "w"); 
    fwrite($fp, $file); 
    fclose($fp); 
    } 
    if ($_POST['photoloc']) { 
    $tmpFile = $_FILES['photoloc']['tmp_name']; 
    $newFile = "/var/www/html/uploads/tmp/".$_FILES['photoloc']['name']; 
    $result = move_upload_file($tmpFile, $newFile); 
    echo $_FILES['photoloc']['name']; 
     if ($result) { 
     echo ' was uploaded<br />'; 
     } else { 
     echo ' failed to upload<br />'; 
    } 
?> 

第一種形式加載文件很好,但第二個完全不起作用。我甚至沒有收到任何錯誤信息。

我在做什麼錯?或缺少什麼?

+0

使用的功能是'move_uploaded_file()以',不'move_upload_file()' – D4V1D 2015-03-25 10:56:25

+0

另外,如果沒有輸出,也許是因爲病情'如果($ _ POST [ 'photoloc'])'是永遠見過面嗎? – D4V1D 2015-03-25 10:57:33

+0

@ D4V1D,謝謝!改變右邊的'move_uploaded_file()'有助於解決問題! – Anatoly 2015-03-25 12:18:26

回答

0

這裏是正確的代碼,它正在工作。在你的代碼中第二種形式缺少花括號,isset()函數應該用來檢查發佈的數據集。

<?php 
     $tmpname=rand().".jpg"; 
     if (isset($_POST['photoload'])) { 
     echo '1st'; 
     $file=file_get_contents($_POST['photoorig']); 
     $fp = fopen("/var/www/html/uploads/tmp/".$tmpname, "w"); 
     fwrite($fp, $file); 
     fclose($fp); 
     } 
     if (isset($_POST['photoload2'])) { 
     echo '2nd'; 
     $tmpFile = $_FILES['photoloc']['tmp_name']; 
     $newFile = "/var/www/html/uploads/tmp/".$_FILES['photoloc']['name']; 
     $result = move_upload_file($tmpFile, $newFile); 
     echo $_FILES['photoloc']['name']; 
      if ($result) { 
      echo ' was uploaded<br />'; 
      } else { 
      echo ' failed to upload<br />'; 
       } 
     } 
    ?> 
+0

現在,它的工作原理!同樣,我將@ move_upload_file()更改爲'move_uploaded_file()'@D4V1D。 – Anatoly 2015-03-25 12:16:51

相關問題