Home > other >  Why PHP can't see an uploaded file?
Why PHP can't see an uploaded file?

Time:10-28

I have an index.php and upload.php. In index.php is a form action="upload.php" with input type="file" id="file" name="file" tag in it. PHP code is:

<?php
$file = $_FILES['file'];
print_r($file);
echo "test";
?>

For some reason it shows echo but print_r() doesn't work.

I've tried rewriting the code. Adding other identificators for the input tag inside index.php but it still doesn't work. What am I doing wrong?

CodePudding user response:

maybe you are forgot enctype="multipart/form-data"

try this version


<form action="upload.php" method="post" enctype="multipart/form-data">
  <p><input type="file" name="file">
  <p><button type="submit">Submit</button>
</form>
</body>
</html>



CodePudding user response:

Assuming that you can already upload the file, such as using form like the following:

<form action="upload.php" method="post" enctype="multipart/form-data">
  <p><input type="file" name="file">
  <p><button type="submit">Submit</button>
</form>
</body>
</html>

Then if you want to display the image at the receiving "upload.php", you may use:

If the image is JPG/JPEG type:

<?php
$string1= base64_encode(file_get_contents($_FILES["file"]["tmp_name"]));

echo '<img src="data:image/jpeg;base64, ' .  $string1  . '"/>';

?>

If the image is PNG type:

<?php
$string1= base64_encode(file_get_contents($_FILES["file"]["tmp_name"]));

echo '<img src="data:image/png;base64, ' .  $string1  . '"/>';

?>

Note: actually we usually save the uploaded file in the server (for later display), or store the binary data into a BLOB field in a db table, but the above code is to demonstrate how one can do to immediately display the uploaded image.

CodePudding user response:

You did not mention what type of file is being uploaded. Here is an example of a image upload. With multiple image selections allowed.

<form action="upload.php" method="post" enctype="multipart/form-data">
Get a Photo from your device <br>
<input type="file" name="image1[]" multiple accept="image/png, image/jpeg, image/gif, image/webp" /><br>
<button type="submit">Upload Photo</button>
</form>

And the upload PHP:

foreach ($_FILES["image1"]["error"] as $key => $error) {
    if ($error == 0 ) {
        $tmp_name = $_FILES["image1"]["tmp_name"][$key];
        $name = basename($_FILES["image1"]["name"][$key]);
        $result = move_uploaded_file($tmp_name, "$uploads_dir/$name");
        if($result == false){echo "not moved\n";}
    }
    if($error > 0){echo "error $error\n";}
}

To ascertain the type of image uploaded:

 switch(strtolower($_FILES['image1']['type'])){
  case 'image/jpeg':
    $image = @imagecreatefromjpeg($_FILES['image1']['tmp_name']);
    if ($image !== false){$save = true;break;}
  case 'image/png':
    $image = @imagecreatefrompng($_FILES['image1']['tmp_name']);
    if ($image !== false){$save = true;break;}
  case 'image/gif':
    $image = @imagecreatefromgif($_FILES['image1']['tmp_name']);
    if ($image !== false){$save = true;break;}
  case 'image/webp':
    $image = @imagecreatefromwebp($_FILES['image1']['tmp_name']);
    if ($image !== false){$save = true;break;}
  default:
    $img = @getimagesize($_FILES['image1']['tmp_name']);
    switch(strtolower($img['mime'])){
    case 'image/jpeg':
      $image = @imagecreatefromjpeg($_FILES['image1']['tmp_name']);
      if ($image !== false){$save = true;break;}
    case 'image/png':
      $image = @imagecreatefrompng($_FILES['image1']['tmp_name']);
      if ($image !== false){$save = true;break;}
    case 'image/gif':
      $image = @imagecreatefromgif($_FILES['image1']['tmp_name']);
      if ($image !== false){$save = true;break;}
    default:
      $filename = $_FILES['image1']['name'];
      $ext = substr($filename,-3);
      switch(strtolower($ext)){
      case 'jpg':
        $image = @imagecreatefromjpeg($_FILES['image1']['tmp_name']);
        if ($image !== false){$save = true;break;}
      case 'ebp':
        $image = @imagecreatefromwebp($_FILES['image1']['tmp_name']);
        if ($image !== false){$save = true;break;}
      case 'gif':
        $image = @imagecreatefromgif($_FILES['image1']['tmp_name']);
        if ($image !== false){$save = true;break;}
      case 'png':
        $image = @imagecreatefrompng($_FILES['image1']['tmp_name']);
        if ($image !== false){$save = true;break;}
      default:
        $image = @imagecreatefromjpeg($_FILES['image1']['tmp_name']);
        if ($image !== false){$save = true;break;}
        $image = @imagecreatefrompng($_FILES['image1']['tmp_name']);
        if ($image !== false){$save = true;break;}
        $image = @imagecreatefromgif($_FILES['image1']['tmp_name']);
        if ($image !== false){$save = true;break;}
      }
    }
  }
  • Related