dou6495 2016-08-12 12:20
浏览 37
已采纳

使用PHP无法在服务器中上传大尺寸图像

In my website, I want to upload image using PHP in server. But this thing works fine with small size image but not with large size image. I want to upload only mobile clicked images. Here is my form to upload image

<form action="newupload.php" enctype="multipart/form-data" method="post">
    <div class="form-group">
        <div id="image-cropper">
            <input type="file" name="user_image" class="cropit-image-input"  style="font-weight: bold;" />
        </div>
    </div>
    <div class="form-group">
        <input type="text" name="title" class="input-round big-input" id="title-modal" placeholder="Enter your Image Title" required/>
    </div>
    <div class="form-group">
        <!-- required  -->
        <span class="required margin-three">*Please complete all fields correctly</span>
        <!-- end required  -->
        <p class="text-center">
            <button class="btn btn-black no-margin-bottom btn-medium btn-round no-margin-top" type="submit" id="submitbtn">Submit</button>
        </p>
    </div>
</form> 

Here is my PHP script for uploading:

$errors = 0;

error_reporting(E_ALL); 
require_once 'mysqli_connect.php';
session_start();

$email = $_SESSION['user_email'];
$q = "select * from user where u_email='$email'";
$qres=mysqli_query($dbc,$q);
$qrow=mysqli_fetch_array($qres,MYSQLI_ASSOC);
$u_id=$qrow['u_id'];

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (!empty($_FILES['user_image']['name']) && !empty($_POST['title'])) {
        $image = $_FILES['user_image']['name'];
        $uploadedfile = $_FILES['user_image']['tmp_name'];
        $image_title = $_POST['title'];
        $t = uniqid();
        $filenamee = 'user-'.$t;
        $path = 'uploaded/';
        $filename = $path.$filenamee.".jpg";

        if (move_uploaded_file($_FILES['user_image']['tmp_name'], $filename)) {
            $query = "insert into selfiepost (u_id,s_title,s_upload,upload_time) v       values ('$u_id','$image_title','$filename',now())";
            $res = mysqli_query($dbc, $query);
            if ($res) {
                header('Location:home.php?insert=1');
            } else{
                 header('Location:home.php?insert=0');
            }
        } else {
            echo "Sorry, File not uploaded successfully. Please try again!!";
        }
    } else {
        echo "Files should not be empty!!";
    }
}

My php.ini file contains only these five things:

upload_max_filesize 20M
post_max_size 40M
max_input_time 180
max_execution_time 60
memory_limit 48M
  • 写回答

2条回答 默认 最新

  • dqfr90979 2016-08-12 14:56
    关注

    Steps:

    1. Check if PHP returns any error
    2. Define your own max size for this app.
    3. Get actual maximum upload size (based in your own max size and server settings).
    4. Check if the file size exceeds the limit

    function convertToBytes( $value ) {
      if ( is_numeric($value) ) {
        return $value;
      } else {
        $value_length = strlen(trim($value));
        $bytes = substr($value, 0, $value_length - 1);
        $unit = strtolower(substr($value, $value_length - 1));
        switch( $unit ) {
          case 'g':
            $bytes *= 1024;
          case 'm':
            $bytes *= 1024;
          case 'k':
            $bytes *= 1024;
        }
        return $bytes;            
      }
    }
    
    function getFileUploadMaxSize($app_config_max_size) {
      return min(min(convertToBytes(ini_get('post_max_size')), convertToBytes(ini_get('upload_max_filesize'))), $app_config_max_size); 
    }
    
    
    try {
        if ( $_FILES['user_image']['error'] != UPLOAD_ERR_OK ) { // (step 1)
            throw new Exception( _FILES['user_image']['error'] );
        }
      $app_config_max_size = 10485760; // 10MB (step 2)
      $max_size = getFileUploadMaxSize($app_config_max_size); // (step 3)
        if ( $_FILES['user_image']['size'] > $max_size ) { // (step 4)
            throw new Exception( 'limit exceeded' );
        }
    
      ... rest of your upload code ...
    
    } catch (Exception $e) {
      echo $e->getMessage();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。