php上传脚本没有上传 - 但它正在添加到数据库?

时间:2016-05-20 18:20:45

标签: php forms file-upload

当我尝试为用户个人资料制作横幅艺术品上传系统时出现一个奇怪的问题。

我已经搜索了答案,但没有人解决它。

这不是权限问题,我的chmod在uploads文件夹和upload.php文件中是755。

以下代码:

if ($_POST && !empty($_FILES)) {
$formOk = true;

//Assign Variables
$path = $_FILES['image']['tmp_name'];
$name = $_FILES['image']['name'];
$size = $_FILES['image']['size'];
$type = $_FILES['image']['type'];

if ($_FILES['image']['error'] || !is_uploaded_file($path)) {
    $formOk = false;
    echo "Error: Error in uploading file. Please try again.";
}

//check file extension
if ($formOk && !in_array($type, array('image/png', 'image/x-png', 'image/jpeg', 'image/pjpeg', 'image/gif'))) {
    $formOk = false;
    echo "Error: Unsupported file extension. Supported extensions are JPG / PNG.";
}
// check for file size.
if ($formOk && filesize($path) > 500000) {
    $formOk = false;
    echo "Error: File size must be less than 3 MB.";
}

if ($formOk) {
    // read file contents
    $content = file_get_contents($path);

    //connect to mysql database
    if ($conn = mysqli_connect('localhost', 'hidden', 'hidden', 'hidden')) {
        $user_id = mysqli_real_escape_string($conn, $_POST['user_id']);
        $username = mysqli_real_escape_string($conn, $_POST['username']);
        $content = mysqli_real_escape_string($conn, $content);
    $sql = "insert into banners (user_id, username, name, size, type, content) values ('{$user_id}','{$username}','{$name}', '{$size}', '{$type}', '{$content}')";

        if (mysqli_query($conn, $sql)) {
            $uploadOk = true;
            $imageId = mysqli_insert_id($conn);
        } else {
            echo "Error: Could not save the data to mysql database. Please try again.";
        }

        mysqli_close($conn);
    } else {
        echo "Error: Could not connect to mysql database. Please try again.";
    }
}

表格是:

        <form method="post" enctype="multipart/form-data" action="<?=$_SERVER['PHP_SELF']?>" >
      <div>
        <h3>Image Upload:</h3>
      </div>
      <div>
        <label>Image</label>
        <img src="<?php print_r($path); ?>"/>
        <?php print_r($_FILES); ?>
        <input type="text" name="user_id" value="<?php echo $user->data["user_id"]; ?>" readonly/>
        <br />
        <input type="text" name="username" value="<?php echo $user->data["username"]; ?>" readonly/>            
        <input type="hidden" name="MAX_FILE_SIZE" value="500000">
        <input type="file" name="image" />
        <input name="submit" type="submit" value="Upload">
      </div>
    </form>     

我在同一个文件upload.php中有我的包含页眉和页脚。

所以基本上它将数据存储到数据库中,但只是不会上传所有内容,我也检查了phpinfo等等,一切正常,它已打开,phpinfo中的文件限制为64mb,所以我不明白为什么不上传?

任何帮助将不胜感激,谢谢

2 个答案:

答案 0 :(得分:2)

简单:您没有将该文件移动到任何位置,如<?php // In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead // of $_FILES. $uploaddir = '/var/www/uploads/'; $uploadfile = $uploaddir . basename($_FILES['userfile']['name']); echo '<pre>'; if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) { echo "File is valid, and was successfully uploaded.\n"; } else { echo "Possible file upload attack!\n"; } echo 'Here is some more debugging info:'; print_r($_FILES); print "</pre>";

基于以下手册中的示例:

s/(/[/g

并检查错误。

答案 1 :(得分:1)

我只想发布我是如何做到这一点的,感谢弗雷德以及其他人做出的其他评论:)

                //Assign Variables
            $username = $user->data['username'];
            #mkdir("uploads/".$username,0755);
            $target_dir = "../uploads/$username-";
            $path = $target_dir . basename($_FILES['image']['tmp_name']);
            $name = $target_dir . basename($_FILES['image']['name'].$user_id);
            $size = $_FILES['image']['size'];
            $type = $_FILES['image']['type'];



                if (move_uploaded_file($_FILES['image']['tmp_name'], $name)) {
            echo "<h2>Thank you for uploading your banner art - File is valid, and was successfully uploaded.\n</h2>";
        } else {
            echo "Possible file upload attack!\n";
        }

所以我也这样做了所以它现在也在上传的图像名称前放置了一个用户名。

表单几乎相同,我现在也做了这样,以便在一个漂亮的花哨框中打开上传和更新:)

https://gyazo.com/e12a03ce01d8f503b8fa2e79f5605809

再次感谢大家:)