I have an index.php
and upload.php
. In index.
is a form action="upload.php"
which contains input type="file" id="file" name="file"
Label. The 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 tried rewriting the code. Adding additional identifiers for input
tags inside index.php
but still doesn't work. What did i do wrong?
P粉0806439752024-01-17 15:55:50
You did not mention the file type being uploaded. Here is an example of image upload. Allows selection of multiple images.
<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 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 determine the type of image to upload:
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;} } } }
P粉3216766402024-01-17 00:50:31
You 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>