作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
今天我第一次尝试上传文件到我的网站,但是好像不行。我希望有人能帮助我,我希望我的代码是可读的:)
问题是当我选择文件并提交表单时,它不被接受,因为 $_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>
";
}
}
最佳答案
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" />
关于php - 使用 $_FILES 和 PHP 上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44983516/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!