I'm having an unsolvable problem with uploading images to the server using php code. The code below is the file that uploads my images to the folder in the hosting, and saves the path to the database. I am using apache2 and php 7.4 with mysql. When I select the image to upload to the hosting directory, the
copy($_FILES['cons_image']['tmp_name'], $consname);
do not copy the file to the directory on the hosting, and will lead to an error in this function.
if (!$copied) {
echo '<h1>Copy unsuccessful!</h1>';
$errors = 1;
}
I tried everything to solve the problem, and people said that I installed the missing php_gd library, but when I checked the php.ini file I already installed GD, used the phpinfo.php file to check the library. GD, everything is on. But when I check the php.ini file in the paragraph
[gd]
; Tell the jpeg decode to ignore warnings and try to create
; a gd image. The warning will then be displayed as notices ; disabled by
default ; http://php.net/gd.jpeg-ignore-warning
;gd.jpeg_ignore_warning = 1
I don't see it having extension=gd line like in xampp on local machine. Can you help me check why the copy() function doesn't copy the images to the folder on my hosting. It still writes the path to the database but cannot copy the images to the hosting folder. I have included my code below hope anyone can help!
<?php
set_time_limit(0);
include './controllers/config.php';
//define a maxim size for the uploaded images
define("MAX_SIZE", "500");
// define the width and height for the thumbnail
// note that these dimensions are considered the maximum dimension and are not fixed,
// because we have to keep the image ratio intact or it will be deformed
define("WIDTH", "187"); //set here the width you want your thumbnail to be
define("HEIGHT", "105"); //set here the height you want your thumbnail to be.
// this is the function that will create the thumbnail image from the uploaded image
// the resize will be done considering the width and height defined, but without deforming the image
function make_thumb($img_name, $filename, $new_w, $new_h) {
//get image extension.
$ext = getExtension($img_name);
//creates the new image using the appropriate function from gd library
if (!strcmp("jpg", $ext) || !strcmp("jpeg", $ext))
$src_img = imagecreatefromjpeg($img_name);
if (!strcmp("png", $ext))
$src_img = imagecreatefrompng($img_name);
if (!strcmp("gif", $ext))
$src_img = imagecreatefromgif($img_name);
//gets the dimensions of the image
$old_x = imageSX($src_img);
$old_y = imageSY($src_img);
// next we will calculate the new dimensions for the thumbnail image
// the next steps will be taken:
// 1. calculate the ratio by dividing the old dimensions with the new ones
// 2. if the ratio for the width is higher, the width will remain the one define in WIDTH variable
// and the height will be calculated so the image ratio will not change
// 3. otherwise we will use the height ratio for the image
// as a result, only one of the dimensions will be from the fixed ones
$ratio1 = $old_x / $new_w;
$ratio2 = $old_y / $new_h;
if ($ratio1 > $ratio2) {
$thumb_w = $new_w;
$thumb_h = $old_y / $ratio1;
} else {
$thumb_h = $new_h;
$thumb_w = $old_x / $ratio2;
}
// we create a new image with the new dimensions
$dst_img = ImageCreateTrueColor($thumb_w, $thumb_h);
// resize the big image to the new created one
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $thumb_w, $thumb_h, $old_x, $old_y);
// output the created image to the file. Now we will have the thumbnail into the file named by $filename
if (!strcmp("png", $ext))
imagepng($dst_img, $filename);
else
imagejpeg($dst_img, $filename);
if (!strcmp("gif", $ext))
imagegif($dst_img, $filename);
//destroys source and destination images.
imagedestroy($dst_img);
imagedestroy($src_img);
}
// This function reads the extension of the file.
// It is used to determine if the file is an image by checking the extension.
function getExtension($str) {
$i = strrpos($str, ".");
if (!$i) {
return "";
}
$l = strlen($str) - $i;
$ext = substr($str, $i + 1, $l);
return $ext;
}
// This variable is used as a flag. The value is initialized with 0 (meaning no error found)
//and it will be changed to 1 if an error occurs. If the error occurs the file will not be uploaded.
$errors = 0;
// checks if the form has been submitted
$error_message = "";
$sucess_message = "";
if (isset($_POST['Submit'])) {
//reads the name of the file the user submitted for uploading
$image = $_FILES['cons_image']['name'];
$code_img = $_POST['code'];
//// check code input
if (empty($code_img)) {
$error_message = "Hãy điền mã cho hình ảnh, không được trùng mã của nhau!";
} else {
$checkDup = "SELECT count(*) FROM uploads WHERE codes = :codes";
$dupStmt = $sqlCon->prepare($checkDup);
$dupStmt->bindValue(':codes', $code_img);
$dupStmt->execute();
$counts = $dupStmt->fetchColumn();
if ($counts > 0) {
$error_message = "Mã đã tồn tại, hãy chọn mã khác!";
} else {
// if it is not empty
if ($image) {
// get the original name of the file from the clients machine
$filename = stripslashes($_FILES['cons_image']['name']);
// get the extension of the file in a lower case format
$extension = getExtension($filename);
$extension = strtolower($extension);
// if it is not a known extension, we will suppose it is an error, print an error message
//and will not upload the file, otherwise we continue
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) {
$error_message = "Tệp không rõ! chỉ tải lên duy nhất tệp .gif, .jpg hoặc .png !";
$errors = 1;
} else {
// get the size of the image in bytes
// $_FILES[\'image\'][\'tmp_name\'] is the temporary filename of the file in which
//the uploaded file was stored on the server
$size = getimagesize($_FILES['cons_image']['tmp_name']);
$sizekb = filesize($_FILES['cons_image']['tmp_name']);
//compare the size with the maxim size we defined and print error if bigger
if ($sizekb > MAX_SIZE * 56024) {
$error_message = "Dung lượng giới hạn 5MB cho mỗi tệp!";
$errors = 1;
}
$rand = rand(0, 1000);
//we will give an unique name, for example a random number
$image_name = $rand . '.' . $extension;
//the new name will be containing the full path where will be stored (images folder)
$consname = "thumb/" . $image_name; //change the image/ section to where you would like the original image to be stored
$consname2 = "thumb/thumbs/" . $image_name; //change the image/thumb to where you would like to store the new created thumb nail of the image
$copied = copy($_FILES['cons_image']['tmp_name'], $consname);
$copied = copy($_FILES['cons_image']['tmp_name'], $consname2);
$sql = "INSERT INTO uploads(thumb, small_thumb, codes) VALUE(:thumb, :images, :codes) ";
$statement = $sqlCon->prepare($sql);
$statement->bindValue(':thumb', $consname2);
$statement->bindValue(':images', $consname);
$statement->bindValue(':codes', $code_img);
$count = $statement->rowCount();
$statement->execute();
$statement->closeCursor();
$sucess_message = "Tải lên hình ảnh thành công!";
//we verify if the image has been uploaded, and print error instead
if (!$copied) {
echo '<h1>Copy unsuccessful!</h1>';
$errors = 1;
} else {
// the new thumbnail image will be placed in images/thumbs/ folder
$ori_image = $consname;
$thumb_name = $consname2;
// call the function that will create the thumbnail. The function will get as parameters
//the image name, the thumbnail name and the width and height desired for the thumbnail
$thumb = make_thumb($consname, $thumb_name, WIDTH, HEIGHT);
}
}
}
}
}
}
//If no errors registered, print the success message and how the thumbnail image created
//if (isset($_POST['Submit']) && !$errors) {
// $error_message = "Tải lên hình ảnh thành công!";
//}
//echo "<form name=\"newad\" method=\"post\" enctype=\"multipart/form-data\" action=\"\">";
//echo "<input type=\"file\" name=\"cons_image\" >";
//echo "<input name=\"Submit\" type=\"submit\" id=\"image1\" value=\"Upload image\" />";
//echo "</form>";
// lay tat ca hinh anh len trang
$selectImg = "SELECT * FROM uploads ORDER BY id DESC";
$stmtImg = $sqlCon->prepare($selectImg);
$stmtImg->execute();
$imgList = $stmtImg->fetchAll();
?>
<html>
<head>
<meta charset="UTF-8">
<title>Thiên Long Huyết Tử</title>
<link rel="icon" type="image/png" href="imgs/favicon.ico"/>
<link rel="stylesheet" href="ht-admin/css/main.min.css">
<link href="http://tl-huyettu.us/css/aos.css" rel="stylesheet">
<script src="http://tl-huyettu.us/js/aos.js"></script>
<link href="http://tl-huyettu.us/ht-admin/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="http://tl-huyettu.us/ht-admin/js/bootstrap.min.js"></script>
<script src="http://tl-huyettu.us/ht-admin/js/jquery-1.11.1.min.js"></script>
</head>
<body style="background-color: #2b3035;">
<?php include './ht-admin/pages/NarvigationBar.php'; ?>
<!-- nav bar -->
<!-- end nav bar --->
<?php include './ht-admin/pages/verticleBar.php'; ?>
<div class="form-upload">
<form name="newad" method="post" enctype="multipart/form-data" action="">
<span><?php echo $error_message; ?></span>
<h5><?php echo $sucess_message; ?></h5>
<br><br>
<lable>Code</lable>
<input class="code" name="code" type="text" placeholder="Hãy tạo mã cho hình ảnh, không được trùng nhau..."><br>
<div class="file-image">
<lable>Hình ảnh</lable>
<input type="file" name="cons_image">
</div>
<input class="button-s" type="submit" name="Submit" id="image1" value="Upload">
<div class="thumbnails">
<label>Thumbnails</label>
<img src="<?php echo $thumb_name; ?>" width="150" height="70" /><br>
<label>Original Image</label>
<img src="<?php echo $ori_image; ?>" width="350" height="175" />
</div>
</form>
<div class="image-panel-list">
<table>
<tbody class="body-image-list" >
<?php foreach ($imgList as $lits): ?>
<tr>
<td><img src="<?php echo $lits['thumb'] ?>" width="150" height="70" /></td>
<td class="td-code-list" ><?php echo $lits['codes'] ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
<!-- tabs image script -->
</body>
</html>