I am trying to create an upload system for my website. I want it to display the size of the file as well as the percentage the file takes up of 1GB (example: 512MB would be %50 because 512 is 0.5GB). The way I chose to do this is to create a lot of if, elseif.

Here is my code:

<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "udk")
|| ($_FILES["file"]["type"] == "upk")
|| ($_FILES["file"]["type"] == "application/exe")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/bmp")
|| ($_FILES["file"]["type"] == "text/plain")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/gif"))
&& ($_FILES["file"]["size"] < 1073741824)) //1GB of storage.
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    //var_dump($_FILES["file"]["type"]);
    }
  else
    {
    if ($_FILES["file"]["size"] < 107374182.4){
    	$size = "10%";
    }
    elseif ($_FILES["file"]["size"] < 214748364.8){
    	$size = "20%;
    }
    elseif ($_FILES["file"]["size"] < 322122547.2){
    	$size = "30%";
    }
    elseif ($_FILES["file"]["size"] < 429496729.6){
    	$size = "40%";
    }
    elseif ($_FILES["file"]["size"] < 536870912){
    	$size = "50%";
    }
    elseif ($_FILES["file"]["size"] < 644245094.4){
    	$size = "60%";
    }
    elseif ($_FILES["file"]["size"] < 751619276.8){
    	$size = "70%";
    }
    elseif ($_FILES["file"]["size"] < 858993459.2){
    	$size = "80%";
    }
    elseif ($_FILES["file"]["size"] < 966367641.6){
    	$size = "90%";
    }
    elseif ($_FILES["file"]["size"] < 1063004405.76){
    	$size = "99%";
    }
    elseif ($_FILES["file"]["size"] = 1073741824){
    	$size = "100%";
    }
    
    echo "Name: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Percentage used: " . $size;
    
   
    //echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
    //var_dump($_FILES["file"]["type"]);
    if (file_exists("dropbox/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. Please rename and try again.";
      }
    else
      {
      $yes = "<a href='/dropbox/" . $_FILES["file"]["name"] . "'>" . $_FILES["file"]["name"] . "</a>";
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "dropbox/" . $_FILES["file"]["name"]);
      echo $yes;
      //var_dump($_FILES["file"]["type"]);
      }
    }
  }
else
  {
  if (($_FILES["file"]["size"] < 20000)){
  echo "Your file is too big!";
  //var_dump($_FILES["file"]["type"]);
  }
  else{
  echo "Invalid file";
  //var_dump($_FILES["file"]["type"]);
  }
  }
?>

error:

Parse error: syntax error, unexpected '"', expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/ccstudio/public_html/upload/upload_file.php on line 27

I am brand new to PHP. Are there any errors (other then that) in the code?

Dani AI

Generated

correctly spotted the syntax error (missing closing quote) that caused the parse error. Beyond that, a few logic and safety issues in the original post by are worth fixing so the uploader is both simpler and safer: an assignment used where a comparison was intended, an inverted size check that reads "too big" for very small files, reliance on client-provided MIME types, duplicate MIME checks, and unsafe filename handling (no sanitization and a malformed anchor string).

A much simpler, deterministic way to get the percentage is to calculate it from the raw byte count instead of chaining many if/elseif ranges. For example:

$bytes   = (float) $_FILES['file']['size'];
$oneGB   = 1073741824.0;
$percent = min(100, (int) round($bytes / $oneGB * 100));
$human   = $bytes >= 1048576 ? round($bytes / 1048576, 2).' MB' : round($bytes / 1024, 2).' KB';
echo "Size: $human";
echo "Percentage used: {$percent}%";

Recommended checklist of fixes and hardening:

  • Check the upload status with $_FILES['file']['error'] === UPLOAD_ERR_OK before using size or tmp_name.
  • Validate type server-side (use finfo_file() or whitelist extensions); do not trust the client MIME.
  • Sanitize filenames: basename() + preg_replace('/[^A-Za-z0-9._-]/','_', $name) and consider adding uniqid() to avoid collisions.
  • Ensure the target directory exists and is writable (is_dir() / is_writable() / mkdir() if needed).
  • Use move_uploaded_file() and check its return value; escape output with htmlspecialchars() when echoing names.
  • Fix any = used where ==/=== was intended, and correct inverted comparisons (e.g., "too big" should be > max).

A concise example to sanitize and move the file:

$name   = basename($_FILES['file']['name']);
$name   = preg_replace('/[^A-Za-z0-9._-]/', '_', $name);
$target = __DIR__ . '/dropbox/' . $name;
if (!is_dir(dirname($target))) { mkdir(dirname($target), 0755, true); }
if (move_uploaded_file($_FILES['file']['tmp_name'], $target)) {
    echo 'Uploaded: ' . htmlspecialchars($name);
} else {
    echo 'Upload failed';
}

Fix the syntax, replace the long if-chain with the percentage calculation, and apply the validation/sanitization checks above to get a more robust upload flow.

Recommended Answers

All 2 Replies

Where you have...

elseif ($_FILES["file"]["size"] < 214748364.8){
$size = "20%;

There is a missing closing double quote " .

elseif ($_FILES["file"]["size"] < 214748364.8){
$size = "20%";

Where you have...

elseif ($_FILES["file"]["size"] < 214748364.8){
$size = "20%;

There is a missing closing double quote " .

elseif ($_FILES["file"]["size"] < 214748364.8){
$size = "20%";

:P

thanks for the reply!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.