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 plotBAPC画图出错
  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理