未定义的索引fileToUpload in ....在线

时间:2015-02-28 09:48:53

标签: php html image-size

我必须为类项目执行此操作,并且我之前从未使用PHP编程,所以如果问题非常基本,请原谅。我试图接受用户输入的图像,调整大小并将其显示在我的PHP页面上。我一直得到未定义的索引错误,我试着看了很多论坛,但我再也看不到代码之间的区别了。所以我想也许这里需要专家意见。我在这里发布我的HTML和PHP代码请让我知道我做错了什么。

<html>
<title>Milestone 0</title>
<link rel="stylesheet" href="index.css" type="text/css"/>
<body>
<div class="head">
   <h1>MILESTONE 0</h1>
</div>
<div class="nav"></div>
<div class="navRight"></div>
<div class="content">
   <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>

我的Php文件如下:

<?php

echo "php file";
$target_dir = "public_html/";

$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"] . ".";

        $filename = $target_file;
        list($width, $height) = getimagesize($filename);
        $image_p = imagecreatetruecolor(500, 500);
        $image = imagecreatefromjpeg($filename);
        imagecopyresampled($image_p, $image, 0, 0, 0, 0, 500, 500, $width, $height);
        header("Content-type: image/jpg");
        imagejpg($image_p);
        imagedestroy($image_p);
        imagedestroy($image);
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
?>

0 个答案:

没有答案