【问题标题】:move_upload_file not working and permissions are all theremove_uploaded_file 不起作用,权限都在那里
【发布时间】:2018-05-04 19:44:26
【问题描述】:

我正在尝试将图片添加到我的表单中,但似乎无法正常工作。我浏览了我能找到的所有资源,但我无法解决这个问题。我的文件夹是可写的,存在的。 move_uploaded_file(tmpfileName, fileDestination) 的语法也是正确的。 这是我的代码:

if($_FILES['thread_image']['name']!=""){
    $fileExtBreak = explode('.', $_FILES['thread_image']['name']);
    $fileExt = strtolower(end($fileExtBreak));
    $allowed = array('jpg','jpeg','png','pdf');
    if(in_array($fileExt, $allowed)){
        $_SESSION['errors'] = $errors;
        if($_FILES['thread_image']['error'] > 0){
            $errors[] = 'Something went wrong when trying to upload your image.';
            $_SESSION['errors'] = $errors;
            header("Location:topic.php?id=". $_GET['id'] ."");
        }else{
            if($_FILES['thread_image']['size']>=10000000){
                $errors[] = 'The file you are trying to upload is too big';
                $_SESSION['errors'] = $errors;
                header("Location:topic.php?id=". $_GET['id'] ."");
            }else{
                if(!is_dir('images')){
                   $errors[]='This shit doesn\'t exist'; 
                }else if(!is_writable('images')){
                    $errors[]='This shit isn\'t writable';
                }else{
                    $errors[]='It exists and should work!!!!';
                }
                $fileName = $date.'.'.$fileExt;
                $filePath = 'images/'.$fileName;
                $errors[]=$_FILES['thread_image']['tmp_name'];
                $errors[]=$fileName;
                $errors[]=$filePath;
                if(move_uploaded_file($_FILES['thread_image']['tmp_name'], $filePath)){
                    $errors[]="success";
                }else{
                    $errors[]="didn't work, sorry";
                }
                $errors[]=mysqli_error($link);
                $_SESSION['errors'] = $errors;
                $stmt = $link->prepare("UPDATE threads set thread_has_image='1' WHERE thread_date=?");
                $stmt->bind_param("s",$date);
                $result=$stmt->execute();
            }
        }
    }else{
        $errors[] = "You cannot upload files with that type";
        $_SESSION['errors'] = $errors;
        header("Location:topic.php?id=". $_GET['id'] ."");
    }
}

这是它吐出来的:

1) 检查文件夹是否存在,2) 吐出临时文件名 3) 吐出我创建的文件名,4) 吐出我创建的文件 Destination 5) 检查 move_uploaded_file 是否有效

这是我的表格:

<form class="makerContainer" method="post" enctype="multipart/form-data" action=<?php echo '"threadMaker_handler.php?id=' . $_GET[ 'id'] . '"'; ?>>
                    <fieldset class="good">
                        <div class="makerForm">
                            Thread Title:
                            <input type="text" class="title" title="ThreadTitle" name="thread_title" <?php echo 'value="' . $name . '"'; ?>>
                        </div>
                        <div class="makerForm">
                            Thread Description:
                            <textarea class="description" rows="8" cols="100" name="thread"><?php echo '' . $thread . ''; ?></textarea>
                            <div class="submitPanel">
                                    <input type="file" name="thread_image"/>
                                <input type="submit" class="button buttonModal buttonColor" value="+ Create Thread">
                            </div>
                        </div>
                    </fieldset>
                </form>

【问题讨论】:

  • 使用 php 的错误报告并确保您的服务器确实支持您正在使用的命名约定
  • @FunkFortyNiner 你是说mysqli_error($link)吗?因为我正在使用它。它什么也没吐出来。 “您的服务器确实支持您使用的命名约定”是什么意思?
  • @TalhaAhmed 他的意思是stackoverflow.com/questions/845021/…
  • 我的意思是文件名的冒号。表格是什么样的?

标签: php database forms image upload


【解决方案1】:

看到 OP 的这条评论:

@FunkFortyNiner 你对冒号是对的,我将文件名从 $date 更改为 uniqid('',true) ,现在它可以工作了。

我有一种感觉,就是文件名中使用的冒号。

Windows 不支持该命名约定。充其量,您需要用另一个字符替换它们。但是,正如我在查询中注意到的那样,这将对您的 mysql 约会方法造成严重破坏。

有没有办法让我的服务器接受日期命名约定?我正在使用 Windows 10。

您可以只使用另一个字符作为文件名,但不要使用$date 进行查询。如果您想使用今天的日期,您可以简单地使用 NOW() 代替它。

您需要将列类型更改为 DATETIME,因为您似乎将它们存储为 VARCHAR,这不是一个好主意。

最好使用 MySQL 的内置日期/日期时间函数,如果使用的是 RDBMS,它将使以后的查询更加容易。

或者保留您现在用于查询的内容以及日期是否匹配,但为 ​​$date 使用不同的变量,并为文件使用不同的存储名称。

【讨论】:

    猜你喜欢
    • 2013-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-09
    • 2018-07-06
    • 1970-01-01
    相关资源
    最近更新 更多