php图片上传检查上传的照片大小不起作用

时间:2014-03-16 22:21:36

标签: php html mysql forms security

<?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的图片并且不会阻止他们上传。 我还需要获得图像格式和图像格式。上传时更改名称并检查重复项,但需要先执行此操作。

2 个答案:

答案 0 :(得分:0)

您的代码存在一些问题。

这打破了你的代码echo '<a href='';一个缺少的引用,应该看起来像:

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

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

您应该指定哪些列应该接收我更改的VALUES。

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

&#34;我还需要获得图像格式&amp;在上传时更改名称并检查重复项,但需要先执行此操作。&#34;

您可以在此之后打开一个新问题。我相信我的回答涵盖了原始问题的重叠问题。

答案 1 :(得分:0)

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