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

标签 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 的图像,但这并没有阻止它们被上传。 我还需要获取图像格式并在上传时更改名称并检查重复项,但首先需要此工作。

最佳答案

您的代码存在一些问题。

这破坏了你的代码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>

"I need to also get image formats & changing the name when uploaded and checking for duplicates but need this working first."

您可以在此问题之后提出一个新问题。我相信我的回答已经涵盖了您原来问题的核心问题。

关于php图像上传检查上传照片的大小不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22443863/

相关文章:

Javascript if 检测保持在 for 循环中运行

mysql - 通过另一个事件创建事件调度程序

c# - 仅使用c# winform,不使用asp.netc#输入表单后如何检查用户是否登录

php - 如何在 mysql PHP 中插入用户登录日期时间

php - 解析和打印 PHP 代码

javascript - 如何使用 JQuery 和 Ajax 写入全局函数?

php - 从下拉列表中选择,然后可以在以后的 WHERE 中使用?

html - 我可以自动将 CSS 列分解为 'pages' 以垂直放置在视口(viewport)内吗?

javascript - PhoneGap Javascript 代码未显示

mysql - phabricator 在 dockerized php 容器中找不到 dockerized mysql