Rumah > Soal Jawab > teks badan
Saya ada tag index.php
和 upload.php
。在 index.
中是一个 form action="upload.php"
,其中包含 input type="file" id="file" name="file"
. Kod PHP ialah:
<?php $file = $_FILES['file']; print_r($file); echo "test"; ?>
Atas sebab tertentu ia menunjukkan echo
但 print_r()
tidak berfungsi.
Saya cuba menulis semula kod. Menambah pengecam lain dalam teg index.php
内为 input
tetapi masih tidak berfungsi. Apa yang saya buat salah?
P粉0806439752024-01-17 15:55:50
Anda tidak menyebut jenis fail yang anda muat naik. Berikut adalah contoh muat naik imej. Membenarkan pemilihan berbilang imej.
<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>
Dan muat naik 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";} }
Untuk menentukan jenis imej yang hendak dimuat naik:
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
Anda terlupa enctype="multipart/form-data"
Cuba versi ini
<form action="upload.php" method="post" enctype="multipart/form-data"> <p><input type="file" name="file"> <p><button type="submit">Submit</button> </form>