-1
我試圖上傳圖像並將其發送到數據庫,但它不工作。 當我再次嘗試的var_dump 這是輸出:錯誤文件上傳使用php
array(1) {
["upload"]=> array(5) {
["name"]=> string(9) "error.jpg"
["type"]=> string(10) "image/jpeg"
["tmp_name"]=> string(24) "C:\xampp\tmp\php8E94.tmp"
["error"]=> int(0)
["size"]=> int(82805)
}
}
這是我的代碼:
<?php
include ('includes/config.php');
$mysqli = new mysqli(DB_SERVER,DB_UNAME,DB_PASSWD,DB_NAME);
if($mysqli->connect_errno){
echo "MYSQLI connect error no {$mysqli->connect_errno} : {$mysqli->connect_error}";
die();
}
if(isset($_POST['addbtn'])){
$itemcode = $_POST['icode'];
$itemname = $_POST['iname'];
$brandname = $_POST['brandname'];
$upload = basename ($_FILES['upload']['name']);
$path = "img/";
if(!empty($upload)){
$i1 = strrpos($upload,".");
if (!$i1) { return ""; }
$l1 = strlen($upload) - $i1;
$ext1 = substr($upload,$i1+1,$l1);
$ext1 = strtolower($ext1);
$news_name1=time()+(1).'.'.$ext1;
$newname1 = $path.$news_name1;
$copied1 = copy($_FILES['upload']['tmp_name'], $newname1);
}else{
$news_name1 = '';
}
$iadd = $mysqli->prepare("INSERT INTO table_item (`itemcode`,`itemname`,`brandname`,`upload`) VALUES ('".$itemcode."', '".$itemname."','".$brandname."','".$news_name1."') ");
$iadd->execute();
$iadd->close();
$mysqli->close();
var_dump($_FILES);
}
?>
檢查您的最大文件上傳限制在php.ini –
您沒有上傳您的文件到數據庫。您只需將其FILENAME保存在數據庫中即可。您也容易受到SQL注入攻擊,沒有上傳驗證碼,沒有任何數據庫錯誤處理代碼。 –
P.S.使用'move_uploaded_file()'而不是'copy()'。 –