繁体   English   中英

为什么不能使用PHP上传图片?

[英]Why won't the image upload using PHP?

所以我有一个apache2 web 服务器,我已经做了它可以运行 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/文件夹,但没有任何反应,那里没有图像。 我对代码做错了什么? 我怎么可能修复它?

感谢您的任何建议。

谢谢

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

相反,如果您将其更改为指向 apache/PHP 可写的某个地方:

$target_dir = __DIR__ . '/';

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

这部分与 Apache 配置有关,因为我可能会猜测您的主目录不可写/服务器配置不知道(这很好,允许 apache/php 在主目录中写入文件被认为是不好的做法) .

如果您想阅读更多内容,请查看以下区域:

嗨,您可以使用phpUpload

$pUp = new phpUpload($_FILES['file']);

//$pUp->maxSize('1024');

//$pUp->allowedType(["image/jpeg", "image/png"]);

//$pUp->newName("New_name");

$pUp->run('destination/folder', true);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM