所以我想爲我的網頁做一個上傳腳本,我似乎無法讓它工作。在嘗試上傳文件時,地址欄會顯示「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;
}
,但是我可以用相同名稱的文件覆蓋它。
「你看到的任何缺陷這個?」我假設你問這個是有原因的。因此,不要讓社區繼續尋找某種東西,爲什麼不告訴我們什麼是錯誤的,你得到的錯誤信息是什麼,你的調試嘗試是什麼,以及你被困在哪裏。 – PeeHaa
謝謝你讓我知道。我改變了它。 – kaioker2
錯誤?什麼東西? '的error_reporting(E_ALL); ini_set('display_errors',1);'你爲什麼把它標記爲「Javascript」?我沒有看到任何JavaScript。 –