0

I am trying to have a file name generated randomly from specific ranges but be unique. I used w3schools to make the upload code and tried modifying it to change it to the generated filename but it fails to upload each time with this error

Notice: Undefined variable: tiledir in /home/rngsites/public_html/b/upload.php on line 67

Warning: move_uploaded_file(): Filename cannot be empty in /home/rngsites/public_html/b/upload.php on line 67

Warning: move_uploaded_file(): Unable to move '/tmp/phpGVMrRA' to '' in /home/rngsites/public_html/b/upload.php on line 67 Sorry, there was an error uploading your file. I assumed that running the function at the beginning of the document would define the variable by the time upload function is run.

HTML:

<html>
  <body>
    <form class='form' action="upload.php" method="post" enctype="multipart/form-data">
     Select image to upload:
      <input type="file" name="fileToUpload" id="fileToUpload">
      <input type="submit" value="Upload Image" name="submit">
    </form>
  </body>
</html>

PHP (updated):

<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
tile();

function tile(){
    $z = rand(1,3);
    if ($z === 1) {
        $x = rand(0,1);
        $y = rand(0,1);
    } elseif ($z === 2) {
        $x = rand(0,3);
        $y = rand(0,3);
    } elseif ($z === 3) {
        $x = rand(0,15);
        $y = rand(0,15);
    }
    $tile = $z.'-'.$x.'-'.$y.'.png';
    $tiledir = 'tiles/'.$tile;
    if (file_exists($tiledir)) {
    tile();
    } else {
        upload();
    }
    }

function upload(){
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
  $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
  if($check !== false) {
    echo "File is an image - " . $check["mime"] . ".";
    $uploadOk = 1;
  } else {
    echo "File is not an image.";
    $uploadOk = 0;
  }
}

// Check if file already exists
if (file_exists($target_file)) {
  echo "Sorry, file already exists.";
  $uploadOk = 0;
}

// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
  echo "Sorry, your file is too large.";
  $uploadOk = 0;
}

// Allow certain file formats
if($imageFileType != "png" ) {
  echo "Sorry, only PNG files are allowed.";
  $uploadOk = 0;
}

// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
  echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
  if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $tiledir)) {
    echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded.";
  } else {
    echo "Sorry, there was an error uploading your file.";
  }
}
}
?>

I tried this solution earlier and it didn't quite help.

I remember being able to this exact thing several years ago on a different site and it worked. I don't understand what I missed this time around.

RGB K
  • 1
  • 2
  • Add [error reporting](http://stackoverflow.com/questions/845021/) to the top of your file(s) _while testing_ right after your opening PHP tag for example. Even if you are developing on a server configured as LIVE you will now see any errors. ` – RiggsFolly Mar 25 '21 at 18:47
  • Unless I missed something in the tidy you appear to have an extra `}` – RiggsFolly Mar 25 '21 at 18:48
  • @RiggsFolly i added the error reporting. will update question with the report. but it seems i have an undefined variable – RGB K Mar 25 '21 at 18:51

2 Answers2

2

In PHP, a single equals sign (=) is assignment, not an equality check. Change your tile function to use two or three equals signs instead.

function tile(){
    $z = rand(1,3);
    if ($z === 1) {
        $x = rand(0,1);
        $y = rand(0,1);
    } elseif ($z === 2) {
        $x = rand(0,3);
        $y = rand(0,3);
    } elseif ($z === 3) {
        $x = rand(0,15);
        $y = rand(0,15);
    }
    $tile = $z.'-'.$x.'-'.$y.'.png';
    $tiledir = 'tiles/'.$tile;
    if (file_exists($tiledir)) {
        tile();
    } else {
        upload();
    }
Chris Haas
  • 53,986
  • 12
  • 141
  • 274
  • That said, I don't know why `tile()` does work on the file system but doesn't pass it on to `upload()`. I'm guessing that `tile()` is intended to be recursive until it finds a filename that doesn't exist and once it finds a new one, it should pass it to `upload` as the first parameter. The `tile()` randomization thing is eventually going to get slower and slower as files are added, too – Chris Haas Mar 25 '21 at 19:02
  • apparently in PHP variable can only be used by the function that makes them. and I have to pass them over in a class or as a global variable. trying to figure out how to do that now. – RGB K Mar 26 '21 at 01:32
0

I figured it out. My code, although very inefficient was not wrong. I failed to pass the variables from the functions so what I ended up doing is getting rid of functions and making the whole thing one big if elseif else statement.

PHP:

<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
tile();
function tile(){
    $z = rand(1,3);
    if ($z === 1) {
        $x = rand(0,1);
        $y = rand(0,1);
    } elseif ($z === 2) {
        $x = rand(0,3);
        $y = rand(0,3);
    } elseif ($z === 3) {
        $x = rand(0,15);
        $y = rand(0,15);
    }
    $tile = $z.'-'.$x.'-'.$y.'.png';
    $tiledir = 'tiles/'.$tile;
    if (file_exists($tiledir)) {
    tile();
    } else {
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
  $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
  if($check !== false) {
    echo "File is an image - " . $check["mime"] . ".";
    $uploadOk = 1;
  } else {
    echo "File is not an image.";
    $uploadOk = 0;
  }
}

// Check if file already exists
if (file_exists($target_file)) {
  echo "Sorry, file already exists.";
  $uploadOk = 0;
}

// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
  echo "Sorry, your file is too large.";
  $uploadOk = 0;
}

// Allow certain file formats
if($imageFileType != "png" ) {
  echo "Sorry, only PNG files are allowed.";
  $uploadOk = 0;
}

// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
  echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
  if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $tiledir)) {
    echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded.";
  } else {
    echo "Sorry, there was an error uploading your file.";
  }
}
}
}
?>
RGB K
  • 1
  • 2