I'm uploading photos to a database. The image destination does appear in the database, however, when I go to my uploads folder, the images aren't there. The folder has both read and write permissions. I created two functions in my code. One is used to gather the information from the images (I am trying to upload 3 images at a time). The other one is used to compress the image and return the destination.
I then use that destination and insert it into my sql database table. The problem is that it does get uploaded into the database table, BUT when I go check the uploads folder, the images aren't there. Here is my code:
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;
}
I have had this problem before and what solved it for me was using the server root path
$uploadPath= $_SERVER['DOCUMENT_ROOT'].'/uploads/';
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
DBMS is a database management system. It is designed to change, search, add and delete information in the database. There are many DBMSs designed for similar purposes with different features. One of the most popular is MySQL.
It is a software tool designed to work with relational SQL databases. It is easy to learn even for site owners who are not professional programmers or administrators. MySQL DBMS also allows you to export and import data, which is convenient when moving large amounts of information.
https://www.mysql.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.