当前位置:免费学习网考试资料计算机类内容页

jQuery Mobile + PHP实现文件上传

2021-09-12 17:13:59 计算机类 访问手机版

  jQuery Mobile + PHP实现文件上传

  很简单的一个小例子 jQuery Mobile + PHP 通过超全局 $_FILES 上传,然后用move_uploaded_file方法把上传的图片移动到到本地服务器下的文件夹,就跟随小编一起去了解下吧,想了解更多相关信息请持续关注我们应届毕业生考试网!

  下面是html和php的.代码

  复制代码 代码如下:

  <!DOCTYPE html>

  <html>

  <head>

  <meta charset = "utf-8">

  <link rel="stylesheet" href="//code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css">

  <script src="//code.jquery.com/jquery-1.8.3.min.js"></script>

  <script src="//code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>

  </head>

  <body>

  <p data-role="page" id="upload" >

  <p data-role="header" >

  <h1>校园祭</h1>

  <a href="#pageone" data-rolr = button data-icon="home" class="ui-btn-left" >首页</a>

  </p>

  <p data-role="content" >

  <form action="upload_file.php" method="post" enctype="multipart/form-data" data-ajax="false">

  <input id="uploadimg" name="file" type="file" runat="server" method="post"

  enctype="multipart/form-data" data-inline="true" data-ajax="false" />

  <center><button data-inline="true" >上传</button></center>

  </form>

  </p>

  <p data-role="footer" data-position="fixed" data-fullscreen="true">

  <h1>创新实验</h1>

  </p>

  </p>

  </body>

  </html>

  复制代码 代码如下:

  <?php

  if $_FILES["file"]["error"] > 0

  echo "Return Code: " . $_FILES["file"]["error"] . "<br />";

  else

  echo "Upload: " . $_FILES["file"]["name"] . "<br />";

  echo "Type: " . $_FILES["file"]["type"] . "<br />";

  echo "Size: " . $_FILES["file"]["size"] / 1024 . " Kb<br />";

  echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

  if file_exists"upload/" . $_FILES["file"]["name"]

  echo $_FILES["file"]["name"] . " already exists. ";

  else

  move_uploaded_file$_FILES["file"]["tmp_name"],

  "upload/".$_FILES["file"]["name"];

  echo "Stored in: " ."upload/". $_FILES["file"]["name"];

  ?>

  代码很简单,但是使用过程中却发现一个问题,自己试了好久都上传不了

  询问了小伙伴后,发现问题所在是文件权限不足,从而限制了网页上传图片到文件夹中.所以解决办法就是把文件夹的权限问题解决掉.

  复制代码 代码如下:

  $ cd /var/www

  $ sudo chmod -R 777 html

  ok,现在就可以将文件上传到服务器的文件夹了.</p