2016-08-12 165 views
0

在我的网站上,我想用PHP在服务器上传图片。但这个东西适用于小尺寸图片,但不适合大尺寸图片。我只想上传移动点击的图片。这里是我的形式上传图像大尺寸图片不在服务器上使用PHP上传

<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> 

这里是我上传的PHP脚本:

$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!!"; 
    } 
} 

php.ini文件只包含这五两件事:

upload_max_filesize 20M 
post_max_size 40M 
max_input_time 180 
max_execution_time 60 
memory_limit 48M 
+0

访问并使用http://php.net/manual/en/features.file-upload.errors.php --- http://php.net/manual/en/function.error-reporting.php和用'phpinfo()'查看你系统的信息。 –

+0

使用此链接:-https://www.sitepoint.com/upload-large-files-in-php/。将这些代码添加到您的php文件中,并检查 –

+0

您在上传时是否在网络/控制台选项卡中的浏览器(通过F12)中看到错误?你看到服务器上的apache或php日志中有任何错误吗?什么是PHP.ini中定义的错误级别? – Cagy79

回答

0

步骤:

  1. 检查PHP是否返回任何e rror
  2. 定义你自己的这个应用程序的最大尺寸。
  3. 获取实际的最大上传大小(基于您自己的最大大小和 服务器设置)。
  4. 检查文件的大小超过限制

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(); 
} 
0

的错误是由于的upload_max_filesize设置为2M,现在我将其更改为64M 感谢所有为他们的意见!