PHP FileUpload无法正常工作

时间:2017-12-24 16:45:55

标签: php file-upload

我在Ubuntu上。我正在尝试上传小图像的用户文件。我检查了$ _FILES,它充满了数据。我尝试调试move命令,但它没有回应任何东西。

if ($_SERVER['REQUEST_METHOD'] == 'POST' ){
    //Now handle everything
    if(isset($_POST["submit"])) {
        print_r($_FILES);
        //Store the image
        if(!empty($_FILES['uploaded_file']))
        {
          $path = "/neel/public/img/";
          $path = $path.basename($_FILES['uploaded_file']['name']);
          if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path)) {

            echo 'Its working';
          } else{
              echo 'I am done!!!';
              die();
          }
        } 
    createnewEvent($conn);
    header('Location:/neel/index.php');
    }
}

4 个答案:

答案 0 :(得分:1)

您可以通过检查文件的名称来检查文件是否存在。

if(!empty($_FILES['file']['name'])) 

其中file是文件输入字段的名称。

答案 1 :(得分:0)

如果$ _SERVER ['REQUEST_METHOD']不是“POST”,那么您显示的脚本只会“不回显任何内容”。假设您对事件的描述是准确的,那么问题的形式是@halojoy要求您在此处显示。

我希望你不要将脚本重定向回自身。此外,您不应该在回声后尝试重定向。

答案 2 :(得分:0)

P上。上面的G是正确的。
而不是检查,如果$ _POST ['submit']
你应该检查一下:
if(isset($_FILES['uploaded_file']['name']))

答案 3 :(得分:0)

尝试使用此代码,使用PHP,Bootstrap

完成注册表单

HTML

<div class="container">
    <div class="row">
        <br>
            <h1 class="text-center">Create new account</h1>
            <br>
        <div class="col-lg-4 col-md-6 col-sm-12">
            <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
                <div class="form-group">
                    <input type="text" class="form-control form-control-lg" name="name" placeholder="Your Name">
                </div>
                <div class="form-group">
                    <input type="text" class="form-control form-control-lg" name="username" placeholder="Username">
                </div>
                <div class="form-group">
                    <input type="password" class="form-control form-control-lg" name="password" placeholder="Password">
                </div>
                <div class="form-group text-center">
                    <div class='file-input'>
                        <input type='file' name="profile-img">
                        <span class='button label' data-js-label>Choose your profile image</span>
                    </div>
                </div>
                <div class="form-group">
                    <input type="submit" class="btn btn-success form-control form-control-lg" name="signup" value="Signup">
                </div>
            </form>
        </div>
    </div>
</div>

PHP

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $errors;
    if (empty($_POST['name'])) {
        $errors[] = "Name field is empty";
    }
    if (empty($_POST['username'])) {
        $errors[] = "Username field is empty";
    }
    if (empty($_POST['password'])) {
        $errors[] = "Password field is empty";
    }
    if (empty($_FILES['profile-img'])) {
        $errors[] = "You should upload profile image";
    } else{
        $img = $_FILES['profile-img'];
        $ext = end(explode('.', $img['name']));
        $allowed_extensions = array('jpg', 'jpeg', 'png', 'gif');
        $max_size = 4; //MegaBytes
        if (! in_array($ext, $allowed_extensions)) {
            $errors[] = "Please , Choose a valid image";
        }
        $img_size = $img['size'] / 1000000; // By Megabyte
        if ($img_size > $max_size) {
            $errors[] = "This picture is so large";
        }
    }
    if (!empty($errors)) {
        echo '<div class="container">';
        foreach ($errors as $error) {
            echo '
                <div class="alert alert-danger" role="alert">
                ' . $error . '
                </div>
            ';
        }
        echo "</div>";
    } else {
        $username = filter_var(htmlentities(trim($_POST['username'])), FILTER_SANITIZE_STRING);
        $name = filter_var(htmlentities(trim($_POST['name'])), FILTER_SANITIZE_STRING);
        $password = sha1(filter_var(htmlentities(trim($_POST['password'])), FILTER_SANITIZE_STRING));
        // Profile Picture :-
        $imgname = uniqid(uniqid()) . @date("Y-m-d") . "." . $ext;

        $target_bath = "uploads/imgs/";
        $target_bath = $target_bath . basename($imgname);
        $orginal_img = $img['tmp_name'];

        if (move_uploaded_file($orginal_img, $target_bath)) {
            $sql = "INSERT INTO users(name, username, password,profile_picture, r_d) VALUES ('$name', '$username', '$password', '$target_bath', NOW())";
            $result = mysqli_query($conn, $sql);
            header('location: ./login.php');
        }


    }
}