今天我第一次尝试将文件上传到我的网站,但似乎它不起作用。我希望有人可以帮助我,我希望我的代码可读:)
问题是当我选择文件和SUBMIT表单时,由于$_FILES
为NULL
if (move_uploaded_file($_FILES['obrazok']['tmp_name'], $uploadfile))
更新错误
Warning: move_uploaded_file(www_nekonomagic/res/img/anime/uploads/793392545Untitled.jpg): failed to open stream: No such file or directory in /home/gj020900/www_nekonomagic/admin/admin-nove-anime.php on line 107
Warning: move_uploaded_file(): Unable to move '/home/gj020900/tmp/php9izgwb' to 'www_nekonomagic/res/img/anime/uploads/793392545Untitled.jpg' in /home/gj020900/www_nekonomagic/admin/admin-nove-anime.php on line 107
表格短缺:
<form enctype="multipart/form-data" action="../admin/admin-nove-anime.php" method="post" role="form">
<div class="form-group col-md-6">
<!-- MAX_FILE_SIZE must precede the file input field -->
<input type="hidden" name="MAX_FILE_SIZE" value="3145728" />
<label for="obrazok" class=""> Obrázok anime (Max. 3MB):</label>
<input type="file" name="obrazok" id="obrazok" class="form-control" required>
</div>
<div class="form-group col-md-12">
<input type="submit" class="btn btn-success" name="pridaj_anime_submit" value="Pridať anime" style="width: 100%;">
</div>
</form>
PHP部分:
if (isset($_POST['pridaj_anime_submit'])) {
// skontroluje sa ci sa subor ulozil ak ano pokracuje ak nie napise chybu
$uploaddir = '/www_nekonomagic/res/img/anime/uploads/';
$uploadfile = $uploaddir . basename($_FILES['obrazok']['name']);
if (move_uploaded_file($_FILES['obrazok']['tmp_name'], $uploadfile)) {
// prva cast formy
$nazov = mysqli_real_escape_string($connect_to_db, $_POST['nazov']);
$rok = mysqli_real_escape_string($connect_to_db, $_POST['rok']);
$pocet = mysqli_real_escape_string($connect_to_db, $_POST['pocet']);
$preklad = mysqli_real_escape_string($connect_to_db, $_POST['preklad']);
$stav = mysqli_real_escape_string($connect_to_db, $_POST['stav']);
$sql_vloz_anime = "INSERT INTO anime (a_name, a_year, a_translated_min, a_translated_max, a_rate_min, a_rate_max, a_edit, a_condition)
VALUES ('$nazov', '$rok', '0', '$pocet', '8', '10', '$preklad', '$stav')";
mysqli_query($connect_to_db, $sql_vloz_anime);
$sql_ziskaj_a_id_pridaneho_anime = "SELECT * FROM anime WHERE a_name = '$nazov'";
$run_sql_ziskaj_a_id_pridaneho_anime = mysqli_query($connect_to_db, $sql_ziskaj_a_id_pridaneho_anime);
$a_id_ziskane = "";
while ($db_data = mysqli_fetch_assoc($run_sql_ziskaj_a_id_pridaneho_anime)) {
$a_id_ziskane = $db_data['a_id'];
}
// druha cast formy
$obrazok = mysqli_real_escape_string($connect_to_db, $uploaddir.$_FILES['obrazok']['name']);
$zaciatok = mysqli_real_escape_string($connect_to_db, $_POST['zaciatok']);
$koniec = mysqli_real_escape_string($connect_to_db, $_POST['koniec']);
$cas = mysqli_real_escape_string($connect_to_db, $_POST['cas']);
$akihabara = mysqli_real_escape_string($connect_to_db, $_POST['akihabara']);
$sql_vloz_anime_info = "INSERT INTO anime_info (a_id, a_img, a_start, a_stop, a_time_ep, a_akihabara)
VALUES ('$a_id_ziskane', '$obrazok', '$zaciatok', '$koniec', '$cas', '$akihabara')";
mysqli_query($connect_to_db, $sql_vloz_anime_info);
}
else {
echo "
<div class=\"alert alert-success alert-info\" style=\"margin: 10px 0\">
<a href=\"#\" class=\"close\" data-dismiss=\"alert\" aria-label=\"close\">×</a>
<strong>POZOR!</strong> Súbor sa nepodarilo nahrať.
</div>
";
}
}
答案 0 :(得分:2)
MAX_FILE_SIZE以字节为单位将上传文件大小限制为3M,将您的大小转换为字节。
1024 * (1024 * 3) // 3145728
然后将隐藏的输入更改为以下
<!-- MAX_FILE_SIZE must precede the file input field -->
<input type="hidden" name="MAX_FILE_SIZE" value="3145728" />
答案 1 :(得分:1)
一切都很好。你只需要改变这个:
$uploaddir = '/www_nekonomagic/res/img/anime/uploads/';
到
$uploaddir = 'www_nekonomagic/res/img/anime/uploads/';
答案 2 :(得分:0)
将您的上传目录路径更改为:
$uploaddir = '/www_nekonomagic/res/img/anime/uploads/';
您忘记在上传结束时添加反斜杠。如上所述进行更改并检查文件是否保存在目录中。感谢。
尝试检查临时文件是否上传为:
if(is_uploaded_file($_FILES['obrazok']['tmp_name'])) {
echo "Temp file uploaded. \r\n";
} else {
echo "Temp file is not uploaded. \r\n";
}