PHP上传表单不起作用

时间:2017-01-14 16:30:11

标签: php html file-upload upload filesize

此代码运行index.php,用户输入元数据和文件:



<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="robots" content="noindex, nofollow" />
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Admin Page</title>
	<!--[if lt IE 9]>
		  <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
		  <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
		<![endif]-->
  </head>
  <body>
    <h2>Welcome!</h2>
    <h5><a href="/login0/logout.php">Logout</a></h5>
    <fieldset><legend>Upload Photo</legend><form name="photo" action="upload.php" method="POST" enctype="multipart/form-data">
      <input type="text" name="title" placeholder="Photo Title">
      <br /><br />
      <textarea placeholder="Description" name="desc"></textarea>
      <br /><br />
      <select name="scale">
        <optgroup label="Nikon">
        	<option value="0" >Nikon Landscape</option>
      	    <option value="1">Nikon Portrait</option>
        </optgroup>
        <optgroup label="iPhone">
        	<option value="2">iPhone Landscape</option>
        </optgroup>
      	<optgroup label="Irregular and Cropped">
      		<option value="3">Irregular Landscape</option>
      		<option value="4">Irregular Portrait</option>
      	</optgroup>
      	<optgroup label="Legacy">
      		<option value="_VOID_">None</option>
      	</optgroup>
      </select>
      <br /><br />
      <input type="text" name="adv" id="adv" placeholder="Advanced Options">
      <label for="adv" class="warn">WARNING: Do not use!</label>
      <br /><br />
      <textarea name="gal" disabled>Gallery control from upload coming soon...</textarea>
      <br /><br />
      
      <b>Photo:</b> <input type="file" name="img" enctype="multipart/form-data">
      <b>Thumbnail:</b> <input type="file" name="thumb" enctype="multipart/form-data">
      <br /><br />
      <input type="submit" value="Upload" name="upload" id="upload"> <label for="upload" class="warn">WARNING: This cannot be undone!</label> 
    </form></fieldset>
	  
  	<fieldset>
  	    <legend>Edit a Photo</legend>
		<form action="edit.php" method="post" name="edit">
          <input type="number" name="id" placeholder="Photo ID">
          <input type="text" name="title" placeholder="Photo Title">
          <br /><br />
          <textarea placeholder="Description" name="desc"></textarea>
          <br /><br />
          <select name="scale">
        <optgroup label="Nikon">
        	<option value="0" >Nikon Landscape</option>
      	    <option value="1">Nikon Portrait</option>
        </optgroup>
        <optgroup label="iPhone">
        	<option value="2">iPhone Landscape</option>
        </optgroup>
      	<optgroup label="Irregular and Cropped">
      		<option value="3">Irregular Landscape</option>
      		<option value="4">Irregular Portrait</option>
      	</optgroup>
      	<optgroup label="Legacy">
      		<option value="_VOID_">None</option>
      	</optgroup>
      </select>
      <br /><br />
      <input type="text" name="adv" id="adv" placeholder="Advanced Options">
      <label for="adv" class="warn">WARNING: Do not use!</label>
      <br /><br />
      <input type="submit" value="Edit" name="edits" id="edits"> <label for="upload" class="warn">WARNING: This cannot be undone!</label> 
        </form>
	  </fieldset>
	  <fieldset>
	    <legend>Re-Upload a Photo</legend>
	  	<form action="re.php" method="post" name="re">
	  		<input type="number" name="id" placeholder="Photo ID"><br /><br />
	  		
      		<b>Photo:</b> <input type="file" name="img" enctype="multipart/form-data">
      		<b>Thumbnail:</b> <input type="file" name="thumb" enctype="multipart/form-data">
      		<br /><br />
      		<input type="submit" value="Re-Upload" name="res" id="res"> <label for="upload" class="warn">WARNING: This cannot be undone!</label>
	  	</form>
	  </fieldset>
  </body>
</html>
&#13;
&#13;
&#13;

这是在upload.php找到的PHP代码,它处理文件和元数据:

<?php
  $r = $_REQUEST;
  $p = file_get_contents("../data/photo.json");
  $d = json_decode($p, true);
  $o = array();
  $o['title'] = $r['title'];
  $o['desc'] = $r['desc'];

  $a = $r['adv'];
  $c = explode(" ",$a);

  $ext = ".jpg";
  $o['ext'] = $ext;

  $o['date'] = date("F j, Y"); // For reference only, removed from homepage

  $o['user'] = "*******"; // Unused by the system, since the system supports only one user

  $o['v'] = 2.0; // The version of the JSON data; used mostly for 'iocus' id check system

  $o['adv'] = $a; // For developer ref only, since all advanced options are precompiled and executed as photo uploads

  $o['auth'] = "Confedential"; // Ensures legit login

  $o['ssl'] = ($_SERVER['HTTPS'] != "" && $_SERVER['HTTPS'] != "off" && $_SERVER['HTTPS'] != NULL); // Verifies HTTPS

  $o['ip'] = $_SERVER['REMOTE_ADDR']; // Look at the name

  $o['scale'] = $r['scale'];

  $id = $d['nxt'];
  $o['id'] = $id;
  $o['iocus'] = "Confidential"; // Nobody knows what it does, but it's there...

  $encoded = json_encode($o);

  $d['nxt'] = $d['nxt'] + 1; // The next photo ID
  $d['photos'][$id] = $o;

  for($i=0;$i<count($c);$i++){
      switch($c[$i]){
          case "-blank":
              $d['photos'][$id] = [];
              break;
          case "-purge":
              echo "Fight the purge!  (it isn't functional yet)";
              break;
          case "":
              break;
          default:
              break;
      }
  }

  $n = json_encode($d,JSON_PRETTY_PRINT);

  file_put_contents("../data/photo.json",$n); // Submit JSON

  $_FILES['img'];
  $thumb = $_FILES['thumb'];

  $dir = str_replace("admin","",$_SERVER['DOCUMENT_ROOT']);
  $dir .= "photos/";

  // Upload photos
  if(move_uploaded_file($_FILES['img']['tmp_name'], $dir.$id.$ext)){
      echo "Photo: Success<br />";
      echo '<div style="color:green;font-size:32pt">GOOD</div>';
  }
  else{
      echo "Photo: Upload Error (Contact your server admin or developer)<br />";
      echo '<div style="color:red;font-size:32pt">ERROR</div>';
  }

  if(move_uploaded_file($thumb['tmp_name'], $dir."thumbnail/".$id.$ext)){
      echo "Thumbnail: Success<br />";
      echo '<div style="color:green;font-size:32pt">GOOD</div>';
  }
  else{
      echo "Thumbnail: Upload Error (Contact your server admin or developer)<br />";
      echo '<div style="color:red;font-size:32pt">ERROR</div>';
  }

  echo "<br />";
  echo "Photo ID (if you lose it, it's easy to find again): ".$id."<br />";
  echo 'To leave this page: <a href="/">go back</a>'; // Does not show up on page.  Odd...
?>

为了安全起见,一些代码被遗漏了,但我已经证明这些方面不是问题。该页面能够完美地处理较小的文件,它无法上传任何实际大小的内容。发生这种情况时,会保存photo.json上的元数据,而不是实际文件。

1 个答案:

答案 0 :(得分:1)

您似乎必须修改upload_max_filesizepost_max_sizephp.ini的值,以允许包含更大文件大小的文件:

; Maximum allowed size for uploaded files.
upload_max_filesize = 40M

; Must be greater than or equal to upload_max_filesize
post_max_size = 40M

修改php.ini文件后,需要重启HTTP服务器以使用新配置。