move_uploaded_file不起作用

时间:2015-12-13 19:21:44

标签: php html file-upload

我为文件上传系统尝试了不同的方法。到目前为止他们都没有工作,我知道有很多类似的问题,我检查并尝试了几乎所有这些问题,但他们没有工作。

如果我尝试上传文件,它会给出最后一个回音。如果文件超过最大大小而不是错误的回声"抱歉,此文件类型不被允许"

这是我的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

3 个答案:

答案 0 :(得分:1)

在条件分支类型中将&&更改为||

if($FileType == "exe" || $FileType == "dll" || $FileType == "zip"  ) {

答案 1 :(得分:0)

我认为问题在于提交按钮并尝试使用class Category(models.Model): name = models.CharField(max_length=10) ... def save(self, *args, **kwargs): super(Category, self).save(*args, **kwargs) Object.objects.create(name=self.name) 检查其值。似乎按钮的名称必须是if (isset($_POST['upload'])) {

查看Send value of submit button when form gets posted

答案 2 :(得分:0)

我在ubuntu论坛上找到了解决方案。问题是文件权限。一旦有关于文件权限的帖子,但代码是错误的。在该主题上,终端代码为 chmod 0751 -R 文件夹 ,但工作代码为 chmod -R 777 文件夹