【发布时间】:2016-03-19 05:42:42
【问题描述】:
我尝试了不同的文件上传系统方法。到目前为止他们都没有工作,我知道有很多类似的问题,我检查并尝试了几乎所有但他们没有工作。
如果我尝试上传文件,它会给出最后一个回声。如果文件大于最大大小,则会给出错误的回显“对不起,不允许此文件类型”
这是我的 php 代码
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$FileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($FileType == "exe" && $FileType == "dll" && $FileType == "zip" ) {
echo "Sorry, this filetype is not allowed";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo " <br>Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
这是 HTML 代码
<html>
<head> <link rel="stylesheet" href="table.css"></head>
<title>Dosya Yükleme</title>
<body>
<form action="fileupload.php" method="post" enctype="multipart/form-data">
Bir dosya seçiniz:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload" name="Yükle">
</form>
</body>
</html>
我简化了代码,将 php 和 html 放在同一个 php 文件中并删除了检查过程。
新的 PHP 文件仍然无法工作
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$FileType = pathinfo($target_file,PATHINFO_EXTENSION);
if (isset($_POST['upload'])) {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
<html>
<head> <link rel="stylesheet" href="table.css"></head>
<title>Dosya Yükleme</title>
<body>
<form action="fileupload.php" method="post" enctype="multipart/form-data">
Bir dosya seçiniz:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload" name="upload">
</form>
</body>
</html>
解决办法 chmod -R 777
【问题讨论】:
-
我发现它们似乎都没有什么不同,但解决方案并没有奏效。如果你发现了一个可以真正解决它的问题,请把链接发给我。
-
重复标签是不必要的,因为解决方案是不相关的。我在 ubuntu 论坛上找到了解决方案。问题是文件权限。曾经有一篇关于文件权限的帖子,但代码错误。在该主题上,终端代码是 chmod 0751 -R 文件夹,但工作代码是 chmod -R 777 文件夹
-
请将您的解决方案作为答案发布,而不是将其编辑到问题中。谢谢!
标签: php html file-upload