如何将图像文件上传到php后台脚本,即使用ajax

发布于 2024-12-09 05:37:37 字数 2368 浏览 0 评论 0原文

我正在尝试使用ajax 将图像文件上传到服务器。然而,到目前为止我只使用ajax将数据传输到后台php脚本。我还没有对文件这样做。但是,我正在尝试如下:-

     <form id="imageUploadForm" action="profileThis.php" method="POST"
       enctype="multipart/formdata"> 

         <input  type="file" id="boozeImgFile" name="boozeImgFile" >  

         <input type="button" onclick="uploadImage()" value="Upload">

    </form>

我的ajax代码如下

      $("imageUploadForm").request(
       {
         method:'post',

         parameters:{category:$("category").value, categoryId:$("categoryId").value, userId:

         $("userInSession").value, uploadImage:'true'}, 

         onComplete:function()
         {
           alert("successfully uploaded to profilethis.php");

         },

         onSuccess:function(ajax)
         {
           alert(ajax.responseText);      

          },

         onFailure:ajaxFailure,

         onException:ajaxFailure
      }); 

但是,当我尝试使用 $filename=$_FILES["boozeImgFile 在后台脚本中检索它时"]["name"] 它不起作用

我的 profileThis.php 看起来像这样

       if(isset($_REQUEST['uploadImage']))
       { 
         $name=$_FILES["boozeImgFile"]["name"];        

         $tempName=$_FILES["boozeImgFile"]["tmp_name"];

         $size=$_FILES["boozeImgFile"]["size"];

         $type=$_FILES["boozeImgFile"]["type"];

         if(($type=="image/jpg"||$type=="image/jpeg"||$type=="image/pjpeg")&&         ($size>0&&$size<=4096000))
         {
        $userId=clearSpecialChars($_REQUEST['userId']);

                $tempName=$_FILES["boozeImgFile"]["tmp_name"];

            $category=clearSpecialChars($_REQUEST['category']);

            $categoryId=clearSpecialChars($_REQUEST['categoryId']);

               $name="img_".$userId."_".$filename;


               $rootDir="C:/xampp/htdocs";

               $dir="/boozeimg/".$category."/".$categoryId;

               $fullDirectory=$rootDir.$dir;

               $realPath=$dir."/".$name;

               $fullPath=$rootDir.$dir."/".$name;


       if(is_dir($fullDirectory))
       {
        move_uploaded_file($tempName, $fullPath);

       }
       else
       {
        mkdir($fullDirectory,0777,true);

        move_uploaded_file($tempName, $fullPath);

       }
     }

在 profileThis.php 中,if 条件内的第一行未达到,但是 if 条件本身确实达到

I am trying to upload an image file using ajax to the server. However, till now i have only transported data to the background php script using ajax. I haven't done so with files.However, I am trying as follows:-

     <form id="imageUploadForm" action="profileThis.php" method="POST"
       enctype="multipart/formdata"> 

         <input  type="file" id="boozeImgFile" name="boozeImgFile" >  

         <input type="button" onclick="uploadImage()" value="Upload">

    </form>

And my ajax code is as follows

      $("imageUploadForm").request(
       {
         method:'post',

         parameters:{category:$("category").value, categoryId:$("categoryId").value, userId:

         $("userInSession").value, uploadImage:'true'}, 

         onComplete:function()
         {
           alert("successfully uploaded to profilethis.php");

         },

         onSuccess:function(ajax)
         {
           alert(ajax.responseText);      

          },

         onFailure:ajaxFailure,

         onException:ajaxFailure
      }); 

However, when I am trying to retrieve it in my background script using $filename=$_FILES["boozeImgFile"]["name"] its not working

My profileThis.php looks like this

       if(isset($_REQUEST['uploadImage']))
       { 
         $name=$_FILES["boozeImgFile"]["name"];        

         $tempName=$_FILES["boozeImgFile"]["tmp_name"];

         $size=$_FILES["boozeImgFile"]["size"];

         $type=$_FILES["boozeImgFile"]["type"];

         if(($type=="image/jpg"||$type=="image/jpeg"||$type=="image/pjpeg")&&         ($size>0&&$size<=4096000))
         {
        $userId=clearSpecialChars($_REQUEST['userId']);

                $tempName=$_FILES["boozeImgFile"]["tmp_name"];

            $category=clearSpecialChars($_REQUEST['category']);

            $categoryId=clearSpecialChars($_REQUEST['categoryId']);

               $name="img_".$userId."_".$filename;


               $rootDir="C:/xampp/htdocs";

               $dir="/boozeimg/".$category."/".$categoryId;

               $fullDirectory=$rootDir.$dir;

               $realPath=$dir."/".$name;

               $fullPath=$rootDir.$dir."/".$name;


       if(is_dir($fullDirectory))
       {
        move_uploaded_file($tempName, $fullPath);

       }
       else
       {
        mkdir($fullDirectory,0777,true);

        move_uploaded_file($tempName, $fullPath);

       }
     }

In profileThis.php the first line inside the if condition doesn't reach, however the if condition by itself does reach

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

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

发布评论

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

评论(2

小猫一只 2024-12-16 05:37:37

那么快速的答案是你不能。您不能使用 AJAX 上传文件。您可以做的是将表单的目标设置为隐藏的 iframe,然后将表单提交到该目标。结果本质上是相同的,但技术上完全不同。大多数人仍然使用 ajax 这个术语(包括我即将发布的 url),但它绝对不是 ajax。这是一个简短教程的链接:

http://www.coursesweb.net/ajax/upload-images

Well the quick answer is you can't. You can't use AJAX to upload files. What you can do is have the target of a form be a hidden iframe and submit your form to that. It is essentially the same thing in result, but technically completely different. Most people still use the term ajax (including my forthcoming url), but it's definitely not ajax. Here's a link with a short tutorial:

http://www.coursesweb.net/ajax/upload-images

dawn曙光 2024-12-16 05:37:37

我会考虑使用一个名为 Uploadify 的很酷的 jQuery 插件。如果您不了解该过程的所有部分,它可能看起来有点复杂,但该网站有不错的文档/示例,总的来说,它是一个非常酷的插件。

I'd look into using a cool jQuery plugin called Uploadify. It might seem a little complicated if you don't understand all parts of the process, but the website has decent documentation/examples and overall it's a really cool plugin.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文