首页 文章

如果图像没有上传到服务器,如何调试

提问于
浏览
0

我想将图像上传到服务器并将文件名保存在MySql中 . 我从Android应用程序的图像上传,我从Android应用程序发送所有必需的参数但在服务器中的复制图像仍然有问题 . 它回应“上传失败” .

<?php

$uploaddir = 'DocumentClient/';
$cid=$_POST['cid'];
$type = $_POST['type'];
$filetype = $_POST['filetype'];

include('../Config.php');

$conn = mysqli_connect("localhost","$username","$password","$db");
// Check connection
if ($conn->connect_error) 
{
    die("Connection failed: " . $conn->connect_error);
}
else
{
        $_FILES["uploadedfile"]["name"]=$cid.$type.".jpg";

        $uploadfile = $uploaddir . basename($_FILES['uploadedfile']['name']);

        if (copy($_FILES['uploadedfile']['tmp_name'], $uploadfile)) 
        {
        $filename="DocumentClient/".$_FILES["uploadedfile"]["name"];  
        if($type == "Address"){
            $sql = "Update `Clients` SET `DocAddress`='$filename' Where CID='$cid'";
        }else if($type == "ID"){
            $sql = "Update `Clients` SET `DocIdProof`='$filename' Where CID='$cid'";
        }else if($type == "GramPanchayat"){
            $sql = "Update `Clients` SET `DocGrampanchyat`='$filename' Where CID='$cid'";
        }

            if ($conn->query($sql) === TRUE) 
            {

            } 
            else 
            {
            echo "Error: " . $sql . "<br>" . $conn->error;    
            }
        $conn->close();
    }
    else 
    {
    echo "Upload failed";
    }

}
?>

1 回答

  • 0

    我们从这里开始吧 . 我会将 copy() 函数更改为 move_uploaded_file() 函数 .

    其次,您需要检查以确保下载目标的路径存在 . 如果没有,那么我们需要创建它 .

    我们还需要确保文件夹/文件具有能够写入的权限 . 如果没有,则更改文件夹/文件权限 .

    完成后,将文件夹/文件权限更改回原来的状态 .

    像这样:

    $uploaddir = 'DocumentClient/';
    $cid=$_POST['cid'];
    $type = $_POST['type'];
    $filetype = $_POST['filetype'];
    
    include('../Config.php');
    
    $conn = mysqli_connect("localhost","$username","$password","$db");
    // Check connection
    if ($conn->connect_error) 
    {
        die("Connection failed: " . $conn->connect_error);
    }
    else
    {
            $_FILES["uploadedfile"]["name"]=$cid.$type.".jpg";
    
            $uploadfile = $uploaddir . basename($_FILES['uploadedfile']['name']);
    
            //Check to see if the folder exist.
            if(!file_exists($uploaddir)) {
    
            mkdir($uploaddir, 0777, true); //If it does not exist then create it and set permissions to be able to write.
    
            }
    
            //The folder does exist at this point. Check to see if it's writable.
            if(!is_writable($uploaddir)){
    
              chmod($uploaddir, 0777); //The folder is not writeable. Set the folder to be able to write.
    
            }
    
            if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $uploadfile)) 
            {
            $filename="DocumentClient/".$_FILES["uploadedfile"]["name"];  
            if($type == "Address"){
                $sql = "Update `Clients` SET `DocAddress`='$filename' Where CID='$cid'";
            }else if($type == "ID"){
                $sql = "Update `Clients` SET `DocIdProof`='$filename' Where CID='$cid'";
            }else if($type == "GramPanchayat"){
                $sql = "Update `Clients` SET `DocGrampanchyat`='$filename' Where CID='$cid'";
            }
    
                if ($conn->query($sql) === TRUE) 
                {
    
                } 
                else 
                {
                echo "Error: " . $sql . "<br>" . $conn->error;    
                }
            $conn->close();
    
            //Reset the permissions back to what you need them to be.
            //You probably want 0755
            chmod($uploaddir, 0755);
            chmod($uploadfile, 0644); //Sets your new file to read only.
    
            //Add this code below to test.
            if(file_exists($uploadfile)) {
    
            echo 'Your file exist.';
    
            }else{
    
              echo 'You file does not exist.';
    
              }
    
        }
        else 
        {
        echo "Upload failed";
        }
    
    }
    

    一些资源供您阅读 .

    move_uploaded_file()

    is_writeable()

    chmod()

    您还需要阅读文件权限以及如何正确设置它们 . 如果没有正确执行此操作,可能会导致不必要的安全问题 .

    希望这可以帮助 .

相关问题