[英]Can't get data from file input / html php
我試圖了解輸入類型文件的工作原理,如何使用 php 格式存儲文件,然后將其插入到我的數據庫中。 這是我的表格:
<form action="../src/add.php" class="addform" method="post" enctype="multipart/form-data">
<div class="addform__group mb-20">
<input type="text" placeholder="Nom de l'artiste" name="artist" class="form-control mr-30">
<input type="text" placeholder="Titre" name="title" class="form-control">
</div>
<div class="addform__group mb-20">
<input type="text" placeholder="Label" name="label" class="form-control mr-30">
<input type="text" placeholder="Vocaliste" name="vocal" class="form-control">
</div>
<div class="addform__group mb-20">
<select name="genre" id="genre" class="form-control mr-30" style="width: 282px;">
<?php
$result = $stmt->get_result();
while($row = $result->fetch_assoc()) { ?>
<option value="<?php echo $row["id"] ?>"><?php echo $row["name"] ?></option>
<?php } ?>
</select>
<input type="date" id="date-picker" class="form-control" name="created-at">
</div>
<!-- Upload audio and image -->
<div class="addform__group mb-20" style="display: flex; justify-content: space-between;">
<div>
<input type="file" id="audio" name="audio">
<label for="audio"><i class="fas fa-music mr-10"></i> Audio (jpg)</label>
<label id="audio-name"></label>
</div>
<div>
<input type="file" id="image" name="image">
<label for="image"><i class="fas fa-image mr-10"></i> Image (jpg)</label>
<label id="image-name"></label>
</div>
</div>
<div class="addform__group">
<button type="submit" name="submit">Ajouter</button>
</div>
</form>
這是我在 php (add.php) 中使用的過程:
if(isset($_POST["submit"])) {
// Get the data from the form
$artiste = $_POST["artist"];
$title = $_POST["title"];
$label = $_POST["label"];
$vocal = $_POST["vocal"];
$genre = $_POST["genre"];
$date = $_POST["created-at"];
$audio = $_FILES["audio"]["name"];
$image = $_FILES["image"]["name"];
// Query to insert a new music
$sql = "INSERT INTO `music` (`artist`, `title`, `label`, `vocal`, `genre_id`, `created_at`, `audio`, `image`) VALUES ('$artiste', '$title', '$label', '$vocal', '$genre', '$date', '$audio', '$image')";
$result = mysqli_query($conn, $sql);
// Check result and errors
if($result) {
echo 'Musique ajoutée';
} else {
echo 'Erreur: ' . mysqli_error($conn);
}
// Upload image file in a directory
$imageDir = "../upload/img/";
$imageFile = $imageDir . basename($_FILES["image"]["name"]);
if(move_uploaded_file($_FILES["image"]["tmp_name"], $imageFile)) {
echo "Image uploaded";
} else {
echo "Image not uploaded";
}
// Upload audio file in a directory
$audioDir = "../upload/audio/";
$audioFile = $audioDir . basename($_FILES["audio"]["name"]);
if(move_uploaded_file($_FILES["audio"]["tmp_name"], $audioFile)) {
echo "Audio uploaded";
} else {
echo "Audio not uploaded";
}
}
問題是當我發送表格時,我得到一個空白頁。 我試圖測試所有輸入,“音頻”是問題所在,$_FILES["audio"]["name"] 返回空。
圖像一切正常,我可以獲得圖像的名稱,甚至可以將圖像保存在指定的文件夾中。
這就是我在僅上傳圖像時使用 var_dump($_FILES) 得到的結果,如果我上傳音頻,我會像往常一樣看到空白頁。
array (size=2)
'audio' =>
array (size=6)
'name' => string '' (length=0)
'full_path' => string '' (length=0)
'type' => string '' (length=0)
'tmp_name' => string '' (length=0)
'error' => int 4
'size' => int 0
'image' =>
array (size=6)
'name' => string 'koven-lionsvip.jpg' (length=18)
'full_path' => string 'koven-lionsvip.jpg' (length=18)
'type' => string 'image/jpeg' (length=10)
'tmp_name' => string 'C:\wamp64\tmp\php2C50.tmp' (length=25)
'error' => int 0
'size' => int 50412
上傳一個帶有空“音頻”的新項目效果很好並存儲在數據庫中,音頻輸入使其無法正常工作
有人有想法嗎? 我想我在代碼中犯了一個錯誤,但經過幾個小時的搜索我找不到它。 謝謝!
對於遇到同樣問題的人,解決方案是增加 php.ini 文件中允許上傳文件的大小。
“upload_max_filesize”和“post_max_size”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.