【问题标题】:i am receiving some unknown error after uploading image to the image folder (php)将图像上传到图像文件夹 (php) 后,我收到一些未知错误
【发布时间】:2017-12-01 11:34:40
【问题描述】:

我正在为我的游戏管创建问答系统。当我尝试将图像上传到 qstimages(图像上传路径文件夹名称)时,问题提交页面上出现了一些奇怪的问题。看看我发布的图片。

this is image 1 - this is the normal condition before upload image

this is image 2 - after uploading the image sucessfully to image folder you can see the script is repeating the form again after advertisement the banner

这是我用于图片上传功能的编码

questionstart.php(html代码)

<div class="container" id="contain">
    <div class="row"> 
    <div class="col-md-8 content">
    <div class="form-group">
    <form action="func/function.php" method="POST" enctype="multipart/form-data"> 
    <br />
    <label for="usr"> Ask anything about Games and Movies , Tv shows here : &nbsp; <span> Dont have an account , Post as a Guest </span> <?php //echo $pleasefill;?> </label>
    <input type="text" name="qst" autocomplete="off" class="form-control" id="usr" placeholder="Enter your question here " required/>

    <textarea class="form-control textarea" name="qsttextarea" rows="5" id="usr_dis" placeholder="Describe what is wrong ....."   onKeyDown="textCounter(this.form.qsttextarea,this.form.countDisplay);" onKeyUp="textCounter(this.form.qsttextarea,this.form.countDisplay);"  required></textarea>
     <hr>
      <div class="container" style="width: 100%;
                                    margin-left: -15px;
                                    border: 1px solid #e1e1e100;
                                    margin: 2px;
                                    background: #f1f1f1;
                                    padding-bottom: 10px;">
       <br />
       <input type="file" name="file" id="file" />
       <br />
       <span id="uploaded_image"></span>
      </div>
    <hr>
    <div class="col-md-12" id="div_tablesplitter"> 
    <div class="col-md-6">
    <input type="text" name="moreabout" class="form-control" id="more" placeholder="Give a short text about the questions" required/>
    </div>
    <div class="col-md-6 pull-right">
    <select>
      <option value="volvo">Action    </option>
      <option value="saab"> Act-adven  </option>
      <option value="saab"> Adventure  </option>
      <option value="saab"> Role play  </option>
      <option value="saab"> Simulation </option>
      <option value="saab"> Strategy   </option>
      <option value="saab"> Sports     </option>
      <option value="saab"> Other      </option>
    </select>
    </select>

    <input readonly type="text" class="countDisplay" name="countDisplay" size="3" maxlength="3" value="3000"> 
    </div>
    </div>      
    <input type="submit" name="submit" value="Submit question" id="submit_button">
    <input type="reset" value="Reset" onclick="clearText()" class="reset" />
    </form>
    </div>

链接到questionstart.php的js文件

$(document).ready(function(){
 $(document).on('change', '#file', function(){
  var name = document.getElementById("file").files[0].name;
  var form_data = new FormData();
  var ext = name.split('.').pop().toLowerCase();
  if(jQuery.inArray(ext, ['gif','png','jpg','jpeg']) == -1) 
  {
   alert("Invalid Image File");
  }
  var oFReader = new FileReader();
  oFReader.readAsDataURL(document.getElementById("file").files[0]);
  var f = document.getElementById("file").files[0];
  var fsize = f.size||f.fileSize;
  if(fsize > 5000000)
  {
   alert("Image File Size is very big");
  }
  else
  {
   form_data.append("file", document.getElementById('file').files[0]);
   $.ajax({
    url:"questionstart.php",
    method:"POST",
    data: form_data,
    contentType: false,
    cache: false,
    processData: false,
    beforeSend:function(){
     $('#uploaded_image').html("<label class='text-success'>Image Uploading...</label>");
    },   
    success:function(data)
    {
     $('#uploaded_image').html(data);
    }
   });
  }
 });
});

questionstart.php中包含的php代码

<?php
//upload.php
error_reporting(0);
if($_FILES["file"]["name"] != '')
{
 $test = explode('.', $_FILES["file"]["name"]);
 $ext = end($test);
 $name = rand(100, 999) . '.' . $ext;
 $location = './qstimages/' . $name;  
 move_uploaded_file($_FILES["file"]["tmp_name"], $location);
 echo '<img src="'.$location.'" height="100" width="100" class="img-thumbnail" />';
}
?>

更新(新错误)

when i refresh the page an error message shown , please check the image

【问题讨论】:

  • 抱歉,我们应该在这些图片中寻找什么?
  • 你好问题已更新,请查看@RamRider
  • 不要使用此代码,您的 PHP 代码不安全。永远不要相信 javascript 允许文件类型。
  • @Raymond Nijland 如何保护数据库。顺便问一下,我的问题能得到答案吗?
  • 好的 - 我想我可以看到发生了什么。您发布到同一页面是正确的吗?

标签: javascript php mysql image


【解决方案1】:

在我看来,服务器正在使用表单的 html 而不是预期的图像 html 响应您的 ajax 调用。看着你的 ajax 电话,我想我明白了。 url 不应该是 'upload.php' 而不是 'questionstart.php' 吗?

   $.ajax({
    url:"upload.php",
    method:"POST",
    data: form_data,
    contentType: false,
    cache: false,
    processData: false,
    beforeSend:function(){
     $('#uploaded_image').html("<label class='text-success'>Image Uploading...</label>");
    },   
    success:function(data)
    {
     $('#uploaded_image').html(data);
    }
   });

【讨论】:

  • 你说得对,有什么方法可以防止吗?
  • 是的,可能有无数的方法来防止它。但是,如果我们没有足够的信息,我们将无能为力。最新错误中的这个 image_upload.php 文件是什么?我没有看到它的代码或它的关系。
【解决方案2】:

我认为问题在于您正在(通过 ajax)发布到同一页面,但您没有将处理上传的代码部分与页面加载时运行的其余 php 脚本隔离开来ajax 查询正在加载整个页面并将所有生成的 HTML 内容作为响应发送,而您只想返回特定于图像上传的数据。

对您来说,下面代码的重要部分应该是顶部,首先有一个逻辑测试来检查请求是否为 POST(我建议也可以为其他 POST 字段添加更严格的逻辑测试)-然后刷新任何先前生成的内容的缓冲区(如果在此之前有任何内容) - 处理上传,然后 EXIT / DIE 停止加载 HTML 内容的其余部分。

<?php
    if( $_SERVER['REQUEST_METHOD']=='POST' && !empty( $_POST['submit'] ) ){
        /* 
            ensure that the response sent back to the ajax callback is 
            ONLY the data you want to send and not the entire page!
        */

        # Flush the output buffers
        ob_clean();


        /*
            This is where you would process the image upload
        */


        /* Whatever data you need to send back to the ajax callback */
        echo $response;


        #EXIT to ensure response data is only generated here
        exit();
    }
?>
<!doctype html>
<html>
    <head>
        <title> - question start - </title>
    </head>
    <body>
        <!-- html content -->
        <!--

            The html content includes the form and all
            other page elements which are being repeated
            after the ajax request has been sent.

        -->
    </body>
</html>

【讨论】:

  • 你能写下对我来说有点混乱的代码吗,我收到了一个错误,但没关系,我可以使用 error_reporting(0) 来清除错误。请写下代码,它有很大帮助。 @RamRaider 谢谢
  • 问题中发布的代码是脱节的,因为您发布了部分而不是整个页面/脚本,这反过来使得积极提供解决方案变得更加困难。您可以制作一个 pastebin 转储并共享链接或使用完整代码更新问题?本质上,如果您要发布到同一页面,那么您应该像我在这里所做的那样隔离由 ajax 处理的那部分代码。如果您要发布到不同的页面/脚本,那么它可能不是这样的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-29
  • 1970-01-01
  • 1970-01-01
  • 2013-12-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多