[英]image not moving to uploads folder in php
我正在將照片上傳到數據庫。 圖像目標確實出現在數據庫中,但是,當我將 go 轉到我的上傳文件夾時,圖像不存在。 該文件夾具有讀取和寫入權限。 我在我的代碼中創建了兩個函數。 一個用於從圖像中收集信息(我嘗試一次上傳 3 張圖像)。 另一個用於壓縮圖像並返回目的地。
然后我使用該目的地並將其插入到我的 sql 數據庫表中。 問題是它確實被上傳到數據庫表中,但是當我 go 檢查上傳文件夾時,圖像不存在。 這是我的代碼:
if(isset($_POST['upload'])){
// Set up variables based on the inputs from the form on upload-page.php
$img1_name = $_FILES['img1']['name'];
$img1_tmp = $_FILES['img1']['tmp_name'];
$img2_name = $_FILES['img2']['name'];
$img2_tmp = $_FILES['img2']['tmp_name'];
$img3_name = $_FILES['img3']['name'];
$img3_tmp = $_FILES['img3']['tmp_name'];
// Variables to hold destination of compressed image
$img1_comp = compressed($img1_name, $img1_tmp);
$img2_comp = compressed($img2_name, $img2_tmp);
$img3_comp = compressed($img3_name, $img3_tmp);
// Insert values into the SQL Projects table
$sql = "INSERT INTO Projects(img1, img2, img3) VALUES (?,?,?)";
$stmt = mysqli_stmt_init($conn);
if(!mysqli_stmt_prepare($stmt, $sql)){
header("Location: ../upload-page.php?error=sqlerror");
exit();
}
else{
mysqli_stmt_bind_param($stmt, "sss", $img1_comp, $img2_comp, $img3_comp);
mysqli_stmt_execute($stmt);
header("Location: ../upload-page.php?upload=success");
exit();
}
}
else{
header("Location: ../upload-page.php");
exit();
}
// Function that will gather file info and pass it to compressImage() function
function compressed($img_name, $img_tmp){
// File info
$uploadPath = "uploads/";
$imageUploadPath = $uploadPath.$img_name;
$fileType = pathinfo($imageUploadPath, PATHINFO_EXTENSION);
// Allow certain file formats
$allowTypes = array('jpg','png','jpeg');
if(in_array($fileType, $allowTypes)){
// Compress size and upload image
$compressedImage = compressImage($img_tmp, $imageUploadPath, 75);
if($compressedImage){
$compressedImageSize = filesize($compressedImage);
move_uploaded_file($img_tmp, $compressedImage);
return $compressedImage; //Return the compressed image destination
}else{
$statusMsg = "Image compress failed!";
}
}else{
$statusMsg = 'Sorry, only JPG, JPEG & PNG files are allowed to upload.';
}
}
// Function that will compress image and return destination
function compressImage($source, $destination, $quality) {
// Get image info
$imgInfo = getimagesize($source);
$mime = $imgInfo['mime'];
// Create a new image from file
switch($mime){
case 'image/jpeg':
$image = imagecreatefromjpeg($source);
break;
case 'image/png':
$image = imagecreatefrompng($source);
break;
case 'image/gif':
$image = imagecreatefromgif($source);
break;
default:
$image = imagecreatefromjpeg($source);
}
// Save image
imagejpeg($image, $destination, $quality);
// Return compressed image
return $destination;
}
我以前遇到過這個問題,為我解決的問題是使用服務器根路徑
$uploadPath= $_SERVER['DOCUMENT_ROOT'].'/uploads/';
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.