{"id":49184,"date":"2024-12-02T17:46:29","date_gmt":"2024-12-02T09:46:29","guid":{"rendered":"https:\/\/fwq.ai\/blog\/49184\/"},"modified":"2024-12-02T17:46:29","modified_gmt":"2024-12-02T09:46:29","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-jquery-fileupload-%e6%8f%92%e4%bb%b6%e7%bb%93%e5%90%88-ajax-%e5%92%8c-php-%e4%b8%8a%e4%bc%a0%e6%96%87%e4%bb%b6%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/49184\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 jQuery FileUpload \u63d2\u4ef6\u7ed3\u5408 Ajax \u548c PHP \u4e0a\u4f20\u6587\u4ef6\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528 jQuery FileUpload \u63d2\u4ef6\u7ed3\u5408 Ajax \u548c PHP \u4e0a\u4f20\u6587\u4ef6\uff1f<\/h1>\n<p>\u5927\u5bb6\u597d\uff0c\u4eca\u5929\u672c\u4eba\u7ed9\u5927\u5bb6\u5e26\u6765\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u4f7f\u7528 jQuery FileUpload \u63d2\u4ef6\u7ed3\u5408 Ajax \u548c PHP \u4e0a\u4f20\u6587\u4ef6\uff1f\u300b<\/span>\uff0c\u6587\u4e2d\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span><\/span>\uff0c\u5982\u679c\u4f60\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u90a3\u5c31\u8bf7\u5404\u4f4d\u670b\u53cb\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427~\u5e0c\u671b\u80fd\u771f\u6b63\u5e2e\u5230\u4f60\u4eec\uff0c\u8c22\u8c22\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241115\/173166105067370cfae8ef1.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528 jQuery FileUpload \u63d2\u4ef6\u7ed3\u5408 Ajax \u548c PHP \u4e0a\u4f20\u6587\u4ef6\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528 jQuery FileUpload \u63d2\u4ef6\u7ed3\u5408 Ajax \u548c PHP \u4e0a\u4f20\u6587\u4ef6\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>jquery fileupload + ajax + php \u4e0a\u4f20\u6587\u4ef6\u6307\u5357<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong><\/p>\n<p>\u5982\u4f55\u4f7f\u7528 jquery fileupload \u63d2\u4ef6\u7ed3\u5408 ajax \u548c php \u6765\u4e0a\u4f20\u6587\u4ef6\uff1f<\/p>\n<p><strong>\u7b54\u6848\uff1a<\/strong><\/p>\n<p><strong>html \u8868\u5355\uff1a<\/strong><\/p>\n<pre>&lt;form id=\"file-upload-form\"&gt;\n  &lt;input type=\"file\" name=\"file\"&gt;\n  &lt;input type=\"submit\" value=\"\u4e0a\u4f20\"&gt;\n&lt;\/form&gt;<\/pre>\n<p><strong>jquery\uff1a<\/strong><\/p>\n<pre>$(function() {\n  $('#file-upload-form').fileupload({\n    url: 'upload.php',\n    datatype: 'json'\n  });\n\n  \/\/ \u4e0a\u4f20\u5b8c\u6210\u7684\u56de\u8c03\u51fd\u6570\n  $('#file-upload-form').on('fileuploadcomplete', function(e, data) {\n    console.log(data.result); \/\/ \u8f93\u51fa\u4e0a\u4f20\u7ed3\u679c\n  });\n});<\/pre>\n<p><strong>php \u5904\u7406\u811a\u672c\uff1a<\/strong><\/p>\n<pre>&lt;?php\nif (isset($_FILES['file'])) {\n  if ($_FILES['file']['error'] === 0) {\n    $target_dir = 'uploads\/';\n    $target_file = $target_dir . basename($_FILES['file']['name']);\n\n    if (move_uploaded_file($_FILES['file']['tmp_name'], $target_file)) {\n      echo json_encode(array('success' =&gt; true));\n      exit;\n    }\n  }\n}\n\necho json_encode(array('success' =&gt; false));\nexit;\n?&gt;<\/pre>\n<p><strong>\u4f7f\u7528\u793a\u4f8b\uff1a<\/strong><\/p>\n<ol>\n<li>\u5f15\u7528 jquery \u548c jquery fileupload \u63d2\u4ef6\u3002<\/li>\n<li>\u521b\u5efa html \u8868\u5355\uff0c\u5305\u542b\u6587\u4ef6\u9009\u62e9\u5668\u548c\u63d0\u4ea4\u6309\u94ae\u3002<\/li>\n<li>\u521d\u59cb\u5316 jquery fileupload \u63d2\u4ef6\uff0c\u6307\u5b9a\u4e0a\u4f20 url \u548c\u6570\u636e\u7c7b\u578b\u3002<\/li>\n<li>\u6dfb\u52a0\u4e0a\u4f20\u5b8c\u6210\u540e\u7684\u56de\u8c03\u51fd\u6570\uff0c\u5904\u7406 \u4e0a\u4f20\u7ed3\u679c\u3002<\/li>\n<li>\u5728 php \u811a\u672c\u4e2d\uff0c\u5904\u7406\u6587\u4ef6\u4e0a\u4f20\uff0c\u79fb\u52a8\u6587\u4ef6\u5230\u6307\u5b9a\u7684\u76ee\u5f55\uff0c\u5e76\u8f93\u51fa json \u54cd\u5e94\u3002<\/li>\n<\/ol>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<p>\u8bf7\u786e\u4fdd\u5df2\u5728 upload.php \u811a\u672c\u6240\u5728\u76ee\u5f55\u521b\u5efa\u540d\u4e3a uploads \u7684\u6587\u4ef6\u5939\uff0c\u7528\u4e8e\u5b58\u50a8\u4e0a\u4f20\u7684\u6587\u4ef6\u3002<\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300a\u5982\u4f55\u4f7f\u7528 jQuery FileUpload \u63d2\u4ef6\u7ed3\u5408 Ajax \u548c PHP \u4e0a\u4f20\u6587\u4ef6\uff1f\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528 jQuery FileUpload \u63d2\u4ef6\u7ed3\u5408 Ajax \u548c PHP \u4e0a\u4f20\u6587\u4ef6\uff1f \u5927\u5bb6\u597d\uff0c\u4eca\u5929\u672c\u4eba\u7ed9\u5927\u5bb6\u5e26\u6765\u6587\u7ae0\u300a\u5982\u4f55\u4f7f\u7528 jQuery FileUpload \u63d2\u4ef6\u7ed3\u5408 Ajax \u548c PHP \u4e0a\u4f20\u6587\u4ef6\uff1f\u300b\uff0c\u6587\u4e2d\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230\uff0c\u5982\u679c\u4f60\u5bf9\u6587\u7ae0\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u90a3\u5c31\u8bf7\u5404\u4f4d\u670b\u53cb\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427~\u5e0c\u671b\u80fd\u771f\u6b63\u5e2e\u5230\u4f60\u4eec\uff0c\u8c22\u8c22\uff01 jquery fileupload + ajax + php \u4e0a\u4f20\u6587\u4ef6\u6307\u5357 \u95ee\u9898\uff1a \u5982\u4f55\u4f7f\u7528 jquery fileupload \u63d2\u4ef6\u7ed3\u5408 ajax \u548c php \u6765\u4e0a\u4f20\u6587\u4ef6\uff1f \u7b54\u6848\uff1a html \u8868\u5355\uff1a &lt;form id=&#8221;file-upload-form&#8221;&gt; &lt;input type=&#8221;file&#8221; name=&#8221;file&#8221;&gt; &lt;input type=&#8221;submit&#8221; value=&#8221;\u4e0a\u4f20&#8221;&gt; &lt;\/form&gt; jquery\uff1a $(function() { $(&#8216;#file-upload-form&#8217;).fileupload({ url: &#8216;upload.php&#8217;, datatype: &#8216;json&#8217; }); \/\/ \u4e0a\u4f20\u5b8c\u6210\u7684\u56de\u8c03\u51fd\u6570 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-49184","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/49184","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=49184"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/49184\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=49184"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=49184"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=49184"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}