PHP图片上传 - 错误

时间:2018-01-09 03:40:10

标签: php image-uploading

我一直在使用此PHP图片上传代码获得以下错误"文件是图片 - image / jpeg.Sorry,上传文件时出错。"

这是我的HTML表单

<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="file" id="file">
<input type="submit" value="Upload Image" name="submit">
</form>

这是我的PHP代码

  <?php  
  $sql="INSERT INTO pictures (file, name) 
  VALUES
  '$_POST[file]', '$_POST[name]' ";


  $target_dir = "uploads/";
  $target_file = $target_dir . basename($_FILES["file"]["name"]);
  $uploadOk = 1;
   $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
  // Check if image file is a actual image or fake image
  if(isset($_POST["submit"])) {
   $check = getimagesize($_FILES["file"]["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["file"]["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["file"]["tmp_name"], $target_file)) {
    echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been 
   uploaded.";
     } else {
      echo "Sorry, there was an error uploading your file.";
         }
             }
         ?> 

我认为这是由于图片大小,但无论大小图像大小,我都会收到相同的错误消息&#34;文件是图片 - image / jpeg。如果上传文件时出错。 &#34;

我的数据库设置如下
id - int(11)
文件 - mediumblob
name - varchar(255)

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:1)

好像你的代码在这一行失败了,

if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file))

您在写入指定文件夹时遇到问题,因为权限或文件夹不存在或上传过程中出错。

您是否尝试启用error_reporting并检查显示的特定php错误代码?

此消息&#34;文件是图像 - image / jpeg.Sorry,上传文件时出错。&#34;您假设错误消息实际上是代码中的一行。

参考:http://php.net/manual/en/function.move-uploaded-file.php

答案 1 :(得分:0)

<?php

//in your code here is syntax error $_POST[file]

$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);
$uploadOk = 1;
 $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
 $check = getimagesize($_FILES["file"]["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["file"]["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["file"]["tmp_name"], $target_file)) {
  echo "The file ". basename( $_FILES["file"]["name"]). " has been
 uploaded.";//here also syntax error 
   } else {
    echo "Sorry, there was an error uploading your file.";
       }
           }
       ?>

答案 2 :(得分:0)

PHP 中的图片上传代码

    $dbconnect = mysqli_connect('localhost','root','');
    
        if(!$dbconnect)
        {
            die("Connection Error");
            $err =  mysqli_errno($dbconnect);
            echo $err;
        }
    else
    {
        //Select database 
        $db = mysqli_select_db($dbconnect,"employee");
        
        //Random String Generation for concate with image filename
        $random =  substr(str_shuffle(str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789',5)),0,8);
        
        //Concate filename with random string
        $userpic = $random.$pic;
        
        //Upload Image function 
        move_uploaded_file($_FILES['photo']['tmp_name'],'userphotos/'.$userpic); 
        
        //Insert query for insert records
        $insert = "insert into emp_details(Name,Gender,Birthdate,Phone,Email,City,Username,Password,Photo) values('$fname','$gender','$bdate','$phone','$email','$city','$uname','$pass','$userpic')";
    
        //Execution of query using this function
        $insquery = mysqli_query($dbconnect,$insert);
            
        //Check if record Inserted or Not using this function
            if(mysqli_affected_rows($dbconnect)==1)
            {
            ?>
                <script type="text/javascript">
                    //alert('Inserted');
                    window.location='Display_Records.php';
                </script>
            <?php
            }
            else
            {
                $err =  mysqli_errno($dbconnect);
                echo $err;
            ?>
                <script type="text/javascript">
                    alert('Error While Inserting Record');
                </script>
            <?php   
            }
                //echo "Database Connection Successful";
    }

JAVASCRIPT 图像验证

//get image filename from html element
var pic    = document.getElementById('photo').value;

//get extension from filename
var picimg = pic.substring(pic.lastIndexOf('.') + 1);

//check image filename empty or not
if(pic =="")
{
    alert("Please Select Image");
}
    //check image extensions
else if(picimg != "JPG" && picimg != "jpg" && picimg != "PNG" && picimg != "png" && picimg != "GIF" && picimg != "gif" && picimg != "JPEG" && picimg != "JPEG" && picimg != "BMP" && picimg != "bmp")
{
        alert('Please Select Valid Image');
        document.getElementById('photo').value="";
        document.getElementById('photo').style.background="lightblue";
        document.getElementById('photo').focus();
}
else
{
    return true;
}