图片上传无效

时间:2017-06-07 10:30:30

标签: php html

所以大家..我想将图片上传到某个文件夹名称“uploads”并在数据库中插入该图片的名称,问题是他没有上传图片..

所以我有这个基本的 HTML 代码:

   distributionUrl=http\://services.gradle.org/distributions/gradle-3.3-all.zip

这是我的 PHP 页面

- (optional)  File -> Invalidate Caches / Restart`
- Give a clean build.

正如你所看到我有一个有效的连接,并试图将productimage插入数据库表但由于某种原因,它正在输入else {}并使我的productimage在数据库上为空,这意味着上面的上传是'工作。

P.S(原谅我任何不好的英语)

2 个答案:

答案 0 :(得分:1)

尝试添加这些

     <form action="newimage.php" method="POST" enctype="multipart/form-data">
    </form

将您的脚本更改为

    <?php
    $target_dir = "uploads/";
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
    $uploadOk = 1;
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    // Check if image file is a actual image or fake image
    if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
    }
    // 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($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != 
     "jpeg"
    && $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
     }
    // Check if $uploadOk is set to 0 by an error
    if ($uploadOk == 0) {
    echo "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.";
    }
    }
    ?>

https://www.w3schools.com/php/php_file_upload.asp

答案 1 :(得分:0)

你缺少enctype =&#34; multipart / form-data&#34;

<form action="/process.php" method="post" enctype="multipart/form-data"></form>