Search code examples
phphtmlfile-uploaduploadfilesize

PHP Upload Form Not Working


This code runs index.php, where the user inputs the metadata and files:

<!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>

This is the PHP code found at upload.php, which processes the files and metadata:

<?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...
?>

Some of thew code has been left out for security, but I have proven that those areas aren't the issue. The page is capable of smaller files perfectly, it just can't upload anything of any real size. When that happens, the metadata on photo.json is saved, just not the actual file.


Solution

  • Looks like you have to modify the values of upload_max_filesize and post_max_size in your php.ini to allow files with bigger filesizes:

    ; Maximum allowed size for uploaded files.
    upload_max_filesize = 40M
    
    ; Must be greater than or equal to upload_max_filesize
    post_max_size = 40M
    

    After modifying php.ini file(s), you need to restart your HTTP server to use new configuration.