繁体   English   中英

PHP的图像上传检查上传的照片大小无法正常工作

[英]php image upload checking size of photo uploaded not working

<?php 
 include 'dbconnect.php';
 //This is the directory where images will be saved 
  $target = "images/"; 
  $target = $target . basename( $_FILES['photo']['name']); 
 $ok=1;


  $name=$_POST['name']; 
 $champname=$_POST['champname']; 
 $cat=$_POST['category']; 
$username=$_POST['username']; 
$pic=($_FILES['photo']['name']); 

 if ($photo_size > 350000)
  {
 echo "Your file is too large.<br>"; 
  $ok=0;
} 


  mysql_query("INSERT INTO `submissions` VALUES ('$fake','$name','$champname', '$cat',      '$username', '$pic')") ; 


 if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
  { 


   echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been succesfully      uploaded to return click below."; 
  echo '<a href='';
  } 
   else { 

  //Gives and error if its not 
 echo "Sorry, there was a problem uploading your file."; 
  } 
?> 

我的add.php

我的表单的底部触发了上面的php:

Username: <input type="text" name = "username"><br> 
Image: <input name="photo" type="file"><br> 
<input type="submit" value="Add"> 

     </form>

我上传的图片超过350kb,并没有阻止它们上传。 我还需要获取图像格式并在上载时更改名称并检查重复项,但首先需要进行此操作。

您的代码存在一些问题。

这破坏了您的代码echo '<a href=''; 缺少的报价应该看起来像这样:

echo "<a href='page.php'>Click here</a>";

$photo_size是一个杂散变量,现已分配,请进一步阅读以获取更多详细信息。

您应该指定哪些列应接收哪些值(我已更改)。

使用if ($photo_size > 350000)检查文件大小不是正确的方法。

采用:

$photo_size=$_FILES["photo"]["size"]; if ($photo_size > 350000){...}

我不确定您如何使用这些$ok=0; 并且$ok=1; 查看代码中的注释。

您的数据库插入时缺少一些变量。

以下使用基于mysqli_*的函数为我工作,其中我在一个文件中使用整个代码。 您可以将其更改为适合。

旁注:整个代码中都有注释供您阅读。

我添加了一个exit; 对于echo "Your file is too large.<br>"; 否则代码将继续执行。

这是完整的代码:( 我测试并在服务器上工作

<?php
DEFINE ('DB_USER', 'xxx');
DEFINE ('DB_PASSWORD', 'xxx');  
DEFINE ('DB_HOST', 'xxx');
DEFINE ('DB_NAME', 'xxx');

$mysqli = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) 
OR die("could not connect");

if(isset($_POST['submit'])){
 //This is the directory where images will be saved 
  $target = "images/"; 
  $target = $target . basename( $_FILES['photo']['name']); 
 $ok=1; // I don't know what you're using this for.

// modify to suit
$fake=mysqli_real_escape_string($mysqli,$_POST['fake']);
$name=mysqli_real_escape_string($mysqli,$_POST['name']);
$champname=mysqli_real_escape_string($mysqli,$_POST['champname']);
$cat=mysqli_real_escape_string($mysqli,$_POST['category']);
$username=mysqli_real_escape_string($mysqli,$_POST['username']);

$pic=($_FILES['photo']['name']);
$photo_size=$_FILES["photo"]["size"];

 if ($photo_size > 350000)

  {
 echo "Your file is too large.<br>";

  $ok=0; // I don't know what you're using this for.

echo "<a href='upload.php'>Click here to try again.</a>";

exit;
} 

mysqli_query($mysqli,"INSERT INTO `submissions` (fake, name, champname, cat, username, pic) VALUES ('$fake','$name','$champname', '$cat', '$username', '$pic')");

    if($mysqli != false) {
        echo "success!";
    } else {
        echo "an error occured saving your data!";
    }


 if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
  { 


   echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been succesfully uploaded to return click below."; 

   // I don't know what you want to do here, but it's not correct. There's a missing quote.
   //  echo '<a href='';
  } 
   else { 

  //Gives and error if its not 
 echo "Sorry, there was a problem uploading your file."; 
  }

} // if(isset($_POST['submit'])){
?>


<form action="" method="post" enctype='multipart/form-data'>
Name: <input type="text" name = "name"><br>
Username: <input type="text" name = "username"><br>
Champ name: <input type="text" name = "champname"><br>
Category: <input type="text" name = "category"><br>
Fake: <input type="text" name = "fake"><br>
Image: <input name="photo" type="file"><br> 
<input type="submit" value="Add" name="submit"> 
</form>

“我还需要获取图像格式并在上载时更改名称并检查重复项,但首先需要进行此工作。”

您可以在此问题后打开一个新问题。 我相信我的回答已经涵盖了您最初提出的问题的基本内容。

您可能需要仔细检查与Wamp服务器捆绑在一起的http.conf文件,并检查上传限制

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM