【问题标题】:Why won't the image upload using PHP?为什么不能使用 PHP 上传图片?
【发布时间】:2021-01-22 00:55:28
【问题描述】:

所以我有一个apache2 网络服务器,我已经制作了它以便它可以运行 PHP 文件,但它似乎根本不起作用。

我的图片上传PHP代码

<?php
$target_dir = "~/pictures/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["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["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;
}


// 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 ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded.";
  } else {
    echo "Sorry, there was an error uploading your file.";
  }
}
?>

我的查看代码

<!DOCTYPE html>
<html>
    <head>
        <title>  War Thunder Social </title>
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
    </head>
    <body onload="setInterval('chat.update()', 1000)">
        <style>
            body {
                margin: 0;
                color: #f4976c;
                background: #fbe8a6;
                scrollbar-color: black;
                scroll-behavior: smooth;
                scroll-snap-type: mandatory;
                scrollbar-base-color: #303c6c;
                scrollbar-shadow-color: aquamarine;
            }
            .topnav {
                width: 100%;
                height: 50px;
                overflow: hidden;
                top: 0;
                position: sticky;
                background: #303c6c;
            }
            .topnav a {
                width: 10%;
                color: black;
                height: 50px;
                text-decoration: none;
                padding-top: 10px;
                text-align: center;
                float: left;
                background: #f4976c;
            }
            .home {
                width: 100%;
                margin-top: 0;
                background: #f4976c;
                height: 400px;
                clip-path: polygon(0 0, 100% 0, 100% 70%, 0 100%);
            }
            .home h1 {
                font-size: 800;
                transition: 0.5s;
                text-shadow:  -2px 1px 3px black;
            }
            .home h1:hover {
                transition: 0.5s;
                mix-blend-mode: color-dodge;
            }
            h1 {
                text-align: center;
                margin-top: 0;
                padding-top: 80px;
                font-size: 800;
                padding-left: 100px;
            }
            
        </style>
        <style>
        .parallax {
            height: 500px;
            background-attachment: fixed;
            background-position: center;
            background-repeat: no-repeat;
            background-size: cover;
            scroll-behavior: smooth;
            scrollbar-color: black;
          }
          </style>
          
          
        <style>

.prev {
    width: 100%;
    height: 800px;
    background: #b4d5e5;
    text-rendering: optimizeLegibility;
}

    p {
        padding: 10px;
        padding-left: 30px;
        padding-right: 30px;
        font-size: 800;
        color: #f4976c;
    }
    .got-image-upload {
        width: 100%;
        height: 300px;
        background: #b4d5e5;
    }

</style>



        <div class="topnav">
            <a href="begginers-guide.html"> Home </a>   

        </div>
        <div class="home">
            <div class="parallax">
            <h1> War Thunder Social </h1>
        </div>
        </div>
        <div class="got-image-upload">
            <form action="upload.php" method="post" enctype="multipart/form-data">
                Select image to upload:
                <input type="file" name="fileToUpload" id="fileToUpload">
                <input type="submit" value="Upload Image" name="submit">
              </form>
              
        </div>
    </body>




</html>

但我得到了这个错误

'文件是图像 - image/png.抱歉,上传您的文件时出错 文件'

但我不明白为什么会收到该错误。我有一个pictures/ 文件夹,但它没有任何反应,那里没有图像。我对代码做错了什么?我怎么可能解决它?

感谢您的任何建议。

谢谢

【问题讨论】:

  • 你遇到了什么错误?
  • “文件是图像 - image/png” 这不是错误消息。当上传的文件被识别为图像时会显示此消息。您可以从if 语句中删除echo,此消息将消失。

标签: php html


【解决方案1】:

问题是您的变量$target_dir 指向“~/pictures/”,它是“/home/user_name/pictures/”的 unix talk。

如果改为将其更改为指向 apache/PHP 可写的位置:

$target_dir = __DIR__ . '/';

您的表单 /should/ 工作(假设 Apache 可以写入提供代码的位置!)。

这部分与 Apache 配置有关,因为我会冒险猜测您的主目录不可写/服务器配置不知道(这很好,允许 apache/php 在主目录)。

如果您想了解更多信息,请查看以下区域:

【讨论】:

    【解决方案2】:

    您好,您可以使用phpUpload

    $pUp = new phpUpload($_FILES['file']);
    
    //$pUp->maxSize('1024');
    
    //$pUp->allowedType(["image/jpeg", "image/png"]);
    
    //$pUp->newName("New_name");
    
    $pUp->run('destination/folder', true);
    

    【讨论】:

    • 这是哪个图书馆的?
    • 这个依赖使用标准的php函数
    • 你能把我链接到 PHP STL 记录这个的地方吗?我看得很清楚,看不出来。 php.net
    猜你喜欢
    • 2016-11-20
    • 2016-07-11
    • 1970-01-01
    • 1970-01-01
    • 2015-10-25
    • 2022-01-20
    • 1970-01-01
    • 2011-07-09
    • 1970-01-01
    相关资源
    最近更新 更多