如何将图像文件上载到php背景脚本,即使用ajax

如何将图像文件上载到php背景脚本,即使用ajax,php,ajax,forms,upload,Php,Ajax,Forms,Upload,我正在尝试使用ajax将图像文件上载到服务器。但是,到目前为止,我只使用ajax将数据传输到后台php脚本。我还没有对文件执行此操作。但是,我正在尝试以下操作:- <form id="imageUploadForm" action="profileThis.php" method="POST" enctype="multipart/formdata"> <input type="file" id="boozeImgFile" na

我正在尝试使用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>
但是,当我试图在后台脚本中使用
$filename=$\u 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);

       }
     }
if(isset($\u请求['uploadImage']))
{ 
$name=$\u文件[“boozeImgFile”][“name”];
$tempName=$\u文件[“boozeImgFile”][“tmp\u名称”];
$size=$\u文件[“boozeImgFile”][“size”];
$type=$\u文件[“boozeImgFile”][“type”];

如果($type==“image/jpg”| |$type==“image/jpeg”| |$type==“image/pjpeg”)&&($size>0&&$size快速回答是你不能。你不能使用AJAX上传文件。你可以做的是让表单的目标是一个隐藏的iframe并将表单提交给它。结果基本相同,但技术上完全不同。大多数人仍然使用术语AJAX(包括我即将发布的url),但它绝对不是ajax。下面是一个带有简短教程的链接:


快速回答是你不能。你不能使用AJAX上传文件。你可以做的是让表单的目标是一个隐藏的iframe并将表单提交给它。结果基本相同,但技术上完全不同。大多数人仍然使用术语AJAX(包括我即将发布的url),但它绝对不是ajax。下面是一个带有简短教程的链接:


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

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

我已经编辑了它。您现在可以检查它。我看到您在开始处附近设置了
$name=…
,在中间使用
$filename
,但我找不到
$filename
的任何分配。这是错误吗?
 $tempName
分配了两次,但两次分配中的值看起来相同。请确保
$userId
$filename
中也没有“特殊字符”。(如果文件名或用户名是
。/../../../../../../../Windows/System 32/…
会有什么后果?可能不高兴。我已经编辑了它。你现在可以检查它。我看到你在开头附近设置了
$name=…
,在中间使用了
$filename
,但我找不到任何分配给
$filename
的任务。这是mis吗take?
$tempName
分配了两次,但两次分配中的值看起来相同。请确保
$userId
$filename
中也没有“特殊字符”。(如果文件名或用户ID为
。/../../../../../../Windows/System 32/…
会有什么后果?可能不高兴。您可以使用API,但请注意它仅适用于新浏览器(即IE 10+)。您可以使用API,但请注意它仅适用于新浏览器(即IE 10+)
       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);

       }
     }