我对move_uploaded_file函数有问题。问题是当我选择要上传的图像时,它不会移入我设置的文件中。
<?php
include("db_connect.php");
$valid_extensions = array('jpeg', 'jpg', 'png', 'gif', 'bmp');
$path = 'images/';
if( $_FILES['image'])
{
$img = $_FILES['image']['name'];
$tmp = $_FILES['image']['tmp_name'];
$ext = strtolower(pathinfo($img, PATHINFO_EXTENSION));
$final_image = rand(1000,1000000).$img;
// echo $img.$tmp.$final_image;
if(in_array($ext, $valid_extensions))
{
$path = $path.strtolower($final_image);
if(move_uploaded_file($tmp,$path))
{
echo "<img src='$path' />";
$insert = $mysqli->query("CALL sp_uploadImage('".$path."')");
}
else
{
echo 'invalid';
echo '<pre>';
print_r($_FILES);
}
}
}
?>
答案 0 :(得分:1)
您应该为if语句运行适当的条件,并提供备用路由,以防其未按计划进行。
<?php
include("db_connect.php");
$valid_extensions = array('jpeg', 'jpg', 'png', 'gif', 'bmp');
$path = 'images/';
if(isset($_FILES['image']['name']) && $_FILES['image']['name'] != ''){
// check if file name is set and not empty
$img = $_FILES['image']['name'];
$tmp = $_FILES['image']['tmp_name'];
$ext = strtolower(pathinfo($img, PATHINFO_EXTENSION));
$final_image = rand(1000,1000000).$img;
if(in_array($ext, $valid_extensions)){
$path .= strtolower($final_image);
if(move_uploaded_file($tmp,$path)){
echo "<img src='$path' />";
$insert = $mysqli->query("CALL sp_uploadImage('".$path."')");
}else {
echo 'Failed to move Files';
}
}else{
echo 'Not a Valid Extension';
}
}else{
echo 'No Files Found';
}
?>
这样,它将返回适当的消息,并让您知道发生了什么事情。