douna2014 2015-04-13 07:03
浏览 70
已采纳

使用php上传图片时出错

I am trying to upload image as shown here w3schools But it always shows the error

Undefined index: file 

This is the code

HTML

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

PHP

<?php
    if(!isset($_POST["submit"])){
        die('Error');
    }
    $target_dir = "uploads/";
    $target_file = $target_dir . basename($_FILES["file"]["name"]);
    $uploadOk = 1;
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    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;
        }
    }
    if (file_exists($target_file)) {
        echo "Sorry, file already exists.";
        $uploadOk = 0;
    }
    if ($_FILES["file"]["size"] > 500000) {
        echo "Sorry, your file is too large.";
        $uploadOk = 0;
    }
    if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType !=   "jpeg"
    && $imageFileType != "gif" ) {
        echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
        $uploadOk = 0;
    }
    if ($uploadOk == 0) {
        echo "Sorry, your file was not uploaded.";
    } else {
        if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
            echo "The file ". basename( $_FILES["file"]["name"]). " has been     uploaded.";
        } else {
            echo "Sorry, there was an error uploading your file.";
        }
    }
?>
  • 写回答

4条回答 默认 最新

  • dongpengyu1363 2015-04-13 07:08
    关注

    If you are using WAMP or XAMPP they have file upload limit set.If the file size is greater than 2mb the upload will not work.Try uploading images of size less than 2mb and see if it works. To change file upload limit open your php.ini file and modify this value

    upload_max_filesize=2M

    Replace 2M with limit you wish to provide eg 6M or 8M

    The following link explains about changing the file upload limit PHP change the maximum upload file size

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真