2015-09-26 57 views
-1

所以我想爲我的網頁做一個上傳腳本,我似乎無法讓它工作。在嘗試上傳文件時,地址欄會顯示「webpage.com/upload.php」,但無法加載頁面或上傳文件。 php.ini上傳的最大尺寸爲10240M(10G)。目標只是一個簡單的上傳腳本,不會接受2個具有相同名稱的文件。這裏是有缺陷的代碼。註釋掉CSS和包含頁眉/頁腳,如果你想嘗試它。我把這些事情放在了事情上。上傳腳本爲網頁

網頁上(位於/var/www/content/file.php):在PHP腳本

<?php session_start(); ?> 
<html> 
<head> 
    <title>Kaiocraft</title> 
    <link rel="stylesheet" type="text/css" href="/style.css"> 
</head> 
<body> 
    <?php $logo="/logo.png"; include("/var/www/header.php"); ?> 
     <form action="/upload.php" method="post" enctype="multipart/form-data"> 
      <p>file to upload:</p> 
      <input type="file" name="filetoupload" id="filetoupload"> 
      <input type="submit" value="upload" name="submit"> 
     </form> 
    <?php include("/var/www/footer.php"); ?> 
</body> 
</html> 

(位於/var/www/upload.php):

<?php 
$target_dir = "/var/www/uploads/"; 
$target_file = $target_dir . basename($_FILES["filetoupload"]["name"]); 
$uploadOk = 1; 
if (file_exists($target_file)) { 
    echo "<p>a file with that name already exists</p>"; 
    uploadOk = 0; 
} 
if ($uploadOk == 1) { 
    if (move_uploaded_file($_FILES["filetoupload"]["tmp_name"],$target_file)) { 
    echo "<p>uploaded successfully</p>"; 
} else { 
    echo "<p>error. please try again</p>"; 
    } 
} 
?> 

和/ var/www/uploads保存到的空白文件夾。

是的,我知道這是一個超級風險和不安全的方式來做到這一點,但它必須接受任何文件類型高達10G。

編輯: 當我在PHP腳本它工作註釋掉

if (file_exists($target_file)) { 
    echo "<p>a file with that name already exists</p>"; 
    uploadOk = 0; 
} 

,但是我可以用相同名稱的文件覆蓋它。

+1

「你看到的任何缺陷這個?」我假設你問這個是有原因的。因此,不要讓社區繼續尋找某種東西,爲什麼不告訴我們什麼是錯誤的,你得到的錯誤信息是什麼,你的調試嘗試是什麼,以及你被困在哪裏。 – PeeHaa

+0

謝謝你讓我知道。我改變了它。 – kaioker2

+0

錯誤?什麼東西? '的error_reporting(E_ALL); ini_set('display_errors',1);'你爲什麼把它標記爲「Javascript」?我沒有看到任何JavaScript。 –

回答

1
basename(S_FILES["filetoupload"]["name"]); 

應該

basename($_FILES["filetoupload"]["name"]); 

你在那,而不是$

還設置post_max_size設置php_ini,不僅upload_max_filesize的

這裏是你的上傳腳本改寫(它的工作原理,我已經測試過)

<?php 
// show errors, disable this on production server 
error_reporting(E_ALL); 
ini_set('display_errors', 1); 


$target_dir = "/var/www/inovision.ro/web/ddns/up/"; 
$target_file = $target_dir.basename($_FILES["filetoupload"]["name"]); 

if(!file_exists($target_file) && isset($_FILES["filetoupload"]["error"]) && $_FILES["filetoupload"]["error"] == 0) { 
    move_uploaded_file($_FILES["filetoupload"]["tmp_name"], $target_file); 
    echo "<p>uploaded successfully</p>"; 
} 
else if(file_exists($target_file)) { 
    echo "<p>a file with that name already exists</p>"; 
} 
else { 
    echo "<p>error uploading</p>"; 
} 

?> 
+0

我已糾正這兩個問題,但它仍然無法上傳 – kaioker2

+0

你可以看看錯誤日誌?或啓用錯誤報告(在php.ini或upload.php中)。它給出了什麼錯誤? – andreibaboi

+0

此外,移動您的上傳文件夾公用文件夾之外或使用.htaccess保護它,否則你的應用程序將有嚴重的問題。出於安全原因,您可以使用php來上傳文件。 – andreibaboi

0

確保您上傳的文件夾存在,並且是由您的系統上的網絡用戶可寫

例如Ubuntu的用戶是www數據和權限應該是775

+0

做到了這一點,它仍然無法正常工作。它是一個Ubuntu的 – kaioker2