duanchouyi6730 2016-07-18 10:04
浏览 66
已采纳

为什么我的图像文件不能用PHP上传?

I want my users to be able to upload images to their account (my MySQL database). However, when I try to encode it and upload it, it appears that the file was never uploaded and is empty. I have checked the maximum upload size etc. in my PHP settings. Thanks in advance!!

$data = "";
if(isset($_FILES["up"])) {
   $data = file_get_contents($_FILES['up']['tmp_name']);
   $data = base64_encode($data);
   $data = $connection->real_escape_string($data);
} else {
   echo '<div style="position:absolute;height:100px;top:0px;left:0px;
                     border-top-right-radius:20px;border-top-left-radius:20px;
                     width:100%;background:white;z-index:100;"
          >
            <font style="color:#BB0000;font-size:2.2vw;">'.$_FILES['up']['error'].'</font>
        </div>';
   die('');
}

My HTML is: (And the form submits correctly)

 <input type="file" accept=".jpg,.png,.jpeg" name="up" id="up"/>
  • 写回答

2条回答 默认 最新

  • douyi1197 2016-07-18 10:38
    关注

    Suggestion: store the images in a directory.

    why not DB you ask?

    1. read/write to a DB is always slower than a filesystem
    2. your DB backups will become more time consuming

    so, here is my solution.

    STEP 1: create a directory userPhotos

    STEP 2: create a form

    <form action="upload.php" method="post" enctype="multipart/form-data">
     Select your profile picture:
     <input type="file" name="fileToUpload" id="fileToUpload">
     <input type="Upload" value="Upload Image" name="submit">
    </form>
    

    STEP 3: create a file called upload.php which handles file uploads.

    <?php
    $target_dir = "userPhotos/";
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
    $uploadOk = 1;
    $newfilename = ;//assign unique user ID
    $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"] . ".";
        if (move_uploaded_file($_FILES["fileToUpload"][$newfilename.$imageFileType], $target_file)) {
          echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";$uploadOk = 1;
    } 
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
    }
    if($uploadOK==1){
     store the path of image in DB as "/userPhotos/".$newfilename
     echo "uploaded photo : <img src='userphotos/".$newfilename."'">
    }
    //to display the image fetch the path using user ID as put it in src of img tag. 
    ?>
    

    Let me know if anyone has a better solution. thanks and Good luck.

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

报告相同问题?

悬赏问题

  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能