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 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100