PHP图片上传脚本无法运行

发布于 2024-09-18 03:51:55 字数 1777 浏览 3 评论 0原文

我正在为图像构建一个 php 上传脚本,但它根本拒绝运行。

首先,我有一个像这样的简单帖子表单:

    <form action="variables.php" method="post" enctype="multipart/form-data" >  
  <p>Event title : <input type="text" name="name" required></p>
  <p>Description : <input type="text" name="description" required></p>
  <input type="file" name="file" id="file"/>
  <input type="submit" value="submit">
    </form>

然后将提交的字段提供给“variables.php”,如下所示:

  <?php
 require("myfunctions.php");

 $title = $_POST['name'];
 $description =$_POST['description']; 
  $img = $_FILES["file"];

 imgput($img);
 generator($title, $description);
     ?>

“imgput”和“generator”是“myfunctions.php”中的函数,但是问题不在“generator”中,所以“myfunctions.php”如下所示:

    <?php
function imgput($img) { 
 if ((($img["type"] == "image/gif")
 || ($img["type"] == "image/jpeg")
 || ($img["type"] == "image/pjpeg"))
  && ($img["size"] < 500000))
   {
   if ($img["error"] > 0)
     {
     echo "Return Code: " . $img["error"] . "<br />";
     }
   else
     {
     echo "Upload: " .$img["name"] . "<br />";
     echo "Type: " . $img["type"] . "<br />";
     echo "Size: " . $img["size"] / 1024) . " Kb<br />";
     echo "Temp file: " . $img["tmp_name"] . "<br />";

     if (file_exists("../uploads/" . $img["name"]))
       {
       echo $img["name"] . " already exists. ";
       }
     else
       {
       move_uploaded_file($img["tmp_name"],
       "../uploads/" . "event1.jpg");
       echo "Stored in: " . "../uploads/event1.jpg";
       }
     }
   }
 else
   {
   echo "Invalid file";
   }
}

?>

任何帮助都会很棒。我尝试在“imgput”开始后立即运行测试回显,但它甚至不会运行它。

I'm building a php upload script for images, and it refuses to run at all.

First off, I've got a simple post form like this:

    <form action="variables.php" method="post" enctype="multipart/form-data" >  
  <p>Event title : <input type="text" name="name" required></p>
  <p>Description : <input type="text" name="description" required></p>
  <input type="file" name="file" id="file"/>
  <input type="submit" value="submit">
    </form>

This then feeds the submitted fields to "variables.php", which looks like this:

  <?php
 require("myfunctions.php");

 $title = $_POST['name'];
 $description =$_POST['description']; 
  $img = $_FILES["file"];

 imgput($img);
 generator($title, $description);
     ?>

"imgput" and "generator" are functions from "myfunctions.php", but the problem isn't in "genrator", so here's what "myfunctions.php" looks like:

    <?php
function imgput($img) { 
 if ((($img["type"] == "image/gif")
 || ($img["type"] == "image/jpeg")
 || ($img["type"] == "image/pjpeg"))
  && ($img["size"] < 500000))
   {
   if ($img["error"] > 0)
     {
     echo "Return Code: " . $img["error"] . "<br />";
     }
   else
     {
     echo "Upload: " .$img["name"] . "<br />";
     echo "Type: " . $img["type"] . "<br />";
     echo "Size: " . $img["size"] / 1024) . " Kb<br />";
     echo "Temp file: " . $img["tmp_name"] . "<br />";

     if (file_exists("../uploads/" . $img["name"]))
       {
       echo $img["name"] . " already exists. ";
       }
     else
       {
       move_uploaded_file($img["tmp_name"],
       "../uploads/" . "event1.jpg");
       echo "Stored in: " . "../uploads/event1.jpg";
       }
     }
   }
 else
   {
   echo "Invalid file";
   }
}

?>

Any help would be great. I tried running test echos right after "imgput" begins, but it won't even run it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空‖城人不在 2024-09-25 03:51:55

您在这里缺少 (

echo "Size: " . $img["size"] / 1024) . " Kb<br />";
                                  ^^^  no matching ( available

很可能您想要这样:

echo "Size: " . ($img["size"] / 1024) . " Kb<br />";

如果您打开了 error_reporting,您会看到语法错误:

PHP Parse error:  syntax error, unexpected ')', expecting ',' or ';' in /home/marc/test.php on line 16

You're missing a ( here:

echo "Size: " . $img["size"] / 1024) . " Kb<br />";
                                  ^^^  no matching ( available

Most likely you wanted this:

echo "Size: " . ($img["size"] / 1024) . " Kb<br />";

If you had error_reporting turned on, you'd have seen the syntax error:

PHP Parse error:  syntax error, unexpected ')', expecting ',' or ';' in /home/marc/test.php on line 16
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文