【发布时间】:2016-08-12 04:54:32
【问题描述】:
我使用这个脚本来上传图像文件。它应该允许上传 jpeg、jpg 和 png 文件。
一切都可以在远程服务器上运行,但我无法在 XAMPP 服务器上本地上传。我尝试了此线程Why would $_FILES be empty when uploading files to PHP? 中提到的所有技巧,但没有人奏效。
感谢您的帮助
表格:
<form class="form-horizontal" action="potwierdz.php" method="POST" enctype="multipart/form-data">
<fieldset>
<!-- Form Name -->
<legend>Dodawanie produktu</legend>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="produkt_nazwa">Nazwa</label>
<div class="col-md-4">
<input id="produkt_nazwa" name="produkt_nazwa" class="form-control input-md" required="" type="text">
<span class="help-block">Podaj nazwę produktu</span>
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="producent_nazwa">Nazwa producenta</label>
<div class="col-md-4">
<input id="producent_nazwa" name="producent_nazwa" class="form-control input-md" required="" type="text">
<span class="help-block">Podaj nazwę producenta produktu</span>
</div>
</div>
<!-- Textarea -->
<div class="form-group">
<label class="col-md-4 control-label" for="producent_adres">Adres producenta</label>
<div class="col-md-4">
<textarea class="form-control" id="producent_adres" name="producent_adres" required=""></textarea>
</div>
</div>
<!-- Select Basic -->
<div class="form-group">
<label class="col-md-4 control-label" for="produkt_kategoria_nazwa">Kategoria</label>
<div class="col-md-4">
<select id="produkt_kategoria_nazwa" name="produkt_kategoria_nazwa" class="form-control">
<option> </option>
</select>
</div>
</div>
<!-- File Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="obraz">Zdjęcie</label>
<div class="col-md-4">
<input id="obraz" name="obraz" class="input-file" type="file">
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="wyslij"></label>
<div class="col-md-4">
<button id="wyslij" name="wyslij" class="btn btn-primary">Dodaj</button>
</div>
</div>
</fieldset>
</form>
这是脚本:
<?php
$errors= array();
$file_name = $_FILES['obraz']['name'];
$file_size =$_FILES['obraz']['size'];
$file_tmp =$_FILES['obraz']['tmp_name'];
$file_type=$_FILES['obraz']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['obraz']['name'])));
$expensions= array("jpeg","jpg","png");
if(in_array($file_ext,$expensions)=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if(empty($errors)==true){
move_uploaded_file($file_tmp,"obrazki/".$file_name);
echo "Success";
}else{
print_r($errors);
}
?>
它返回“扩展名不允许,请选择 JPEG 或 PNG 文件。”
它也在打印
注意:未定义索引:obraz
每当我使用$_FILES['obraz']['...'] 变量时。
【问题讨论】:
-
echo $file_ext;和error_reporting(E_ALL); ini_set('display_errors', '1'); -
您是否检查过
$file_ext的值,是否会报告错误以防出现任何其他警告/错误? -
当您分配
$file_name、$file_type等变量时,我会使用if (!isset($_FILES['obraz']['name'])) { throw new \Exception('Missing file name)"; }之类的东西进行验证 -
你使用的是什么版本的 PHP?
-
strtolower(end(explode('.',$_FILES['obraz']['name'])))生成错误Strict standards: Only variables should be passed by reference其end()导致错误。 PHP5.6 & PHP5.5 & PHP 5.4 中以及 PHP7.0.5 中的通知