使用以下代码上传多个图片并使用以下代码重命名,压缩,创建缩略图图片一切正常,但上传文件名并不保存到SQL,
有人可以帮助找到代码中的错误吗
<?php
require('includes/config.php');
if (!empty($_POST)) {
$newname = md5(rand() * time());
if (isset($_FILES['files'])) {
$uploadedFiles = array();
foreach ($_FILES['files']['tmp_name'] as $key => $tmp_name) {
$errors = array();
$file_name = md5(uniqid("") . time());
$file_size = $_FILES['files']['size'][$key];
$file_tmp = $_FILES['files']['tmp_name'][$key];
$file_type = $_FILES['files']['type'][$key];
if ($file_type == "image/gif") {
$sExt = ".gif";
} elseif ($file_type == "image/jpeg" || $file_type == "image/pjpeg") {
$sExt = ".jpg";
} elseif ($file_type == "image/png" || $file_type == "image/x-png") {
$sExt = ".png";
}
if (!in_array($sExt, array('.gif', '.jpg', '.png'))) {
$errors[] = "Image types alowed are (.gif, .jpg, .png) only!";
}
if ($file_size > 2097152000) {
$errors[] = 'File size must be less than 2 MB';
}
$desired_dir = "upload/";
if (empty($errors)) {
if (is_dir($desired_dir) == false) {
mkdir("$desired_dir", 0700);
}
if (move_uploaded_file(compress_image($file_tmp, "$desired_dir/" . $file_name . $sExt, 50))) {
$uploadedFiles[$key] = array($file_name . $sExt, 1);
} else {
echo "Couldn't upload file " . $_FILES['files']['name'][$key];
$uploadedFiles[$key] = array($_FILES['files']['name'][$key], 0);
}
} else {
}
}
foreach ($uploadedFiles as $key => $row) {
if (!empty($row[1])) {
$codestr = '$file' . ($key + 1) . ' = $row[0];';
eval($codestr);
} else {
$codestr = '$file' . ($key + 1) . ' = NULL;';
eval($codestr);
}
}
}
$orig_directory = "$desired_dir";
$thumb_directory = "upload/thumb/";
$dir_handle = opendir($orig_directory);
if ($dir_handle > 1) {
$allowed_types = array('jpg', 'jpeg', 'gif', 'png');
$file_type = array();
$ext = '';
$title = '';
$i = 0;
while ($file_name = readdir($dir_handle)) {
if ($file_name == '.' || $file_name == '..') {
continue;
}
$file_type = \explode('.', $file_name);
$ext = strtolower(array_pop($file_type));
$title1 = implode('.', $file_type);
$title = htmlspecialchars($title1);
if (in_array($ext, $allowed_types)) {
$nw = 250;
$nh = 180;
$source = "$desired_dir{$file_name}";
$stype1 = explode(".", $source);
$stype = $stype1[count($stype1) - 1];
$dest = "upload/thumb/{$file_name}";
$size = getimagesize($source);
$w = $size[0];
$h = $size[1];
switch ($stype) {
case 'gif':
$simg = imagecreatefromgif($source);
break;
case 'jpg':
$simg = imagecreatefromjpeg($source);
break;
case 'png':
$simg = imagecreatefrompng($source);
break;
}
$dimg = resizePreservingAspectRatio($simg, $nw, $nh);
imagepng($dimg, $dest);
}
}closedir($dir_handle);
}
$stmt = $db->prepare("INSERT INTO allpostdata(apdtitle, apdcategory, apdsubcategory, brand, model, driven, mobscat, mbrand, mmodel, ecate, ebrand, reoption, bhk, sfeet, jcategory, jobtype, scatergory, pcategory, ecategory, gender, notcate, posted, usernme, view, location, pnumber, prodprice, apddescription, img1, img2, img3, img4)"
. " VALUES (:apdtitle, :apdcategory, :apdsubcategory, :brand, :model, :driven, :mobscat, :mbrand, :mmodel, :ecate, :ebrand, :reoption, :bhk, :sfeet, :jcategory, :jobtype, :scatergory, :pcategory, :ecategory, :gender, :notcate, :posted, :usernme, :view, :location, :pnumber, :prodprice, :apddescription, :img1, :img2, :img3, :img4)");
$stmt->bindParam(':apdtitle', $apdtitle, PDO::PARAM_STR, 100);
$stmt->bindParam(':apdcategory', $apdcategory, PDO::PARAM_STR, 100);
$stmt->bindParam(':apdsubcategory', $apdsubcategory, PDO::PARAM_STR, 100);
$stmt->bindParam(':brand', $brand, PDO::PARAM_STR, 100);
$stmt->bindParam(':model', $model, PDO::PARAM_STR, 100);
$stmt->bindParam(':driven', $driven, PDO::PARAM_STR, 100);
$stmt->bindParam(':mobscat', $mobscat, PDO::PARAM_STR, 100);
$stmt->bindParam(':mbrand', $mbrand, PDO::PARAM_STR, 100);
$stmt->bindParam(':mmodel', $mmodel, PDO::PARAM_STR, 100);
$stmt->bindParam(':ecate', $ecate, PDO::PARAM_STR, 100);
$stmt->bindParam(':ebrand', $ebrand, PDO::PARAM_STR, 100);
$stmt->bindParam(':reoption', $reoption, PDO::PARAM_STR, 100);
$stmt->bindParam(':bhk', $bhk, PDO::PARAM_STR, 100);
$stmt->bindParam(':sfeet', $sfeet, PDO::PARAM_STR, 250);
$stmt->bindParam(':jcategory', $jcategory, PDO::PARAM_STR, 100);
$stmt->bindParam(':jobtype', $jobtype, PDO::PARAM_STR, 100);
$stmt->bindParam(':scatergory', $scatergory, PDO::PARAM_STR, 100);
$stmt->bindParam(':pcategory', $pcategory, PDO::PARAM_STR, 100);
$stmt->bindParam(':ecategory', $ecategory, PDO::PARAM_STR, 100);
$stmt->bindParam(':gender', $gender, PDO::PARAM_STR, 100);
$stmt->bindParam(':notcate', $notcate, PDO::PARAM_STR, 100);
$stmt->bindParam(':posted', $added_on, PDO::PARAM_STR, 100);
$stmt->bindParam(':usernme', $username, PDO::PARAM_STR, 100);
$stmt->bindParam(':view', $location, PDO::PARAM_STR, 100);
$stmt->bindParam(':location', $location, PDO::PARAM_STR, 100);
$stmt->bindParam(':pnumber', $pnumber, PDO::PARAM_STR, 100);
$stmt->bindParam(':prodprice', $prodprice, PDO::PARAM_STR, 100);
$stmt->bindParam(':apddescription', $apddescription, PDO::PARAM_STR, 100);
$stmt->bindParam(':img1', $file1, PDO::PARAM_STR, 100);
$stmt->bindParam(':img2', $file2, PDO::PARAM_STR, 100);
$stmt->bindParam(':img3', $file3, PDO::PARAM_STR, 100);
$stmt->bindParam(':img4', $file4, PDO::PARAM_STR, 100);
if ($stmt->execute()) {
header('Location: index.php');
}exit;
}
function compress_image($source_url, $destination_url, $quality) {
$info = getimagesize($source_url);
if ($info['mime'] == 'image/jpeg') {
$image = imagecreatefromjpeg($source_url);
} elseif ($info['mime'] == 'image/gif') {
$image = imagecreatefromgif($source_url);
} elseif ($info['mime'] == 'image/png') {
$image = imagecreatefrompng($source_url);
} elseif ($info['mime'] == 'image/jpg') {
$image = imagecreatefrompng($source_url);
}
imagejpeg($image, $destination_url, $quality);
return $destination_url;
}
function resizePreservingAspectRatio($img, $targetWidth, $targetHeight) {
$srcWidth = imagesx($img);
$srcHeight = imagesy($img);
$srcRatio = $srcWidth / $srcHeight;
$targetRatio = $targetWidth / $targetHeight;
if (($srcWidth <= $targetWidth) && ($srcHeight <= $targetHeight)) {
$imgTargetWidth = $srcWidth;
$imgTargetHeight = $srcHeight;
} else if ($targetRatio > $srcRatio) {
$imgTargetWidth = (int) ($targetHeight * $srcRatio);
$imgTargetHeight = $targetHeight;
} else {
$imgTargetWidth = $targetWidth;
$imgTargetHeight = (int) ($targetWidth / $srcRatio);
}
$targetImg = imagecreatetruecolor($targetWidth, $targetHeight);
$targetTransparent = imagecolorallocate($targetImg, 255, 0, 255);
imagefill($targetImg, 0, 0, $targetTransparent);
imagecolortransparent($targetImg, $targetTransparent);
imagecopyresampled($targetImg, $img, 0, 0, 0, 0, $targetWidth, $targetHeight, $srcWidth, $srcHeight);
return $targetImg;
}
?>
PHP错误日志
[14-Jun-2018 11:22:25 Asia/Kolkata] PHP Warning: imagecreatefrompng(): 'upload/26745882e73c3148b2c3cbc9d73b67d1.png' is not a valid PNG file in C:\wamp\www\Classfied\add_listing.php on line 117
[14-Jun-2018 11:22:25 Asia/Kolkata] PHP Stack trace:
[14-Jun-2018 11:22:25 Asia/Kolkata] PHP 1. {main}() C:\wamp\www\Classfied\add_listing.php:0
[14-Jun-2018 11:22:25 Asia/Kolkata] PHP 2. imagecreatefrompng() C:\wamp\www\Classfied\add_listing.php:117
[14-Jun-2018 11:22:25 Asia/Kolkata] PHP Warning: imagesx() expects parameter 1 to be resource, boolean given in C:\wamp\www\Classfied\add_listing.php on line 180
[14-Jun-2018 11:22:25 Asia/Kolkata] PHP Stack trace:
[14-Jun-2018 11:22:25 Asia/Kolkata] PHP 1. {main}() C:\wamp\www\Classfied\add_listing.php:0
[14-Jun-2018 11:22:25 Asia/Kolkata] PHP 2. resizePreservingAspectRatio() C:\wamp\www\Classfied\add_listing.php:120
[14-Jun-2018 11:22:25 Asia/Kolkata] PHP 3. imagesx() C:\wamp\www\Classfied\add_listing.php:180
[14-Jun-2018 11:22:25 Asia/Kolkata] PHP Warning: imagesy() expects parameter 1 to be resource, boolean given in C:\wamp\www\Classfied\add_listing.php on line 181
[14-Jun-2018 11:22:25 Asia/Kolkata] PHP Stack trace:
[14-Jun-2018 11:22:25 Asia/Kolkata] PHP 1. {main}() C:\wamp\www\Classfied\add_listing.php:0
[14-Jun-2018 11:22:25 Asia/Kolkata] PHP 2. resizePreservingAspectRatio() C:\wamp\www\Classfied\add_listing.php:120
[14-Jun-2018 11:22:25 Asia/Kolkata] PHP 3. imagesy() C:\wamp\www\Classfied\add_listing.php:181
[14-Jun-2018 11:22:25 Asia/Kolkata] PHP Warning: Division by zero in C:\wamp\www\Classfied\add_listing.php on line 182
[14-Jun-2018 11:22:25 Asia/Kolkata] PHP Stack trace:
[14-Jun-2018 11:22:25 Asia/Kolkata] PHP 1. {main}() C:\wamp\www\Classfied\add_listing.php:0
[14-Jun-2018 11:22:25 Asia/Kolkata] PHP 2. resizePreservingAspectRatio() C:\wamp\www\Classfied\add_listing.php:120
[14-Jun-2018 11:22:25 Asia/Kolkata] PHP Warning: imagecopyresampled() expects parameter 2 to be resource, boolean given in C:\wamp\www\Classfied\add_listing.php on line 198
[14-Jun-2018 11:22:25 Asia/Kolkata] PHP Stack trace:
[14-Jun-2018 11:22:25 Asia/Kolkata] PHP 1. {main}() C:\wamp\www\Classfied\add_listing.php:0
[14-Jun-2018 11:22:25 Asia/Kolkata] PHP 2. resizePreservingAspectRatio() C:\wamp\www\Classfied\add_listing.php:120
[14-Jun-2018 11:22:25 Asia/Kolkata] PHP 3. imagecopyresampled() C:\wamp\www\Classfied\add_listing.php:198
[14-Jun-2018 11:22:50 Asia/Kolkata] PHP Warning: imagecreatefrompng(): 'upload/419785bf8a7d56c9a869f77b909989dd.png' is not a valid PNG file in C:\wamp\www\Classfied\add_listing.php on line 117
[14-Jun-2018 11:22:50 Asia/Kolkata] PHP Stack trace:
[14-Jun-2018 11:22:50 Asia/Kolkata] PHP 1. {main}() C:\wamp\www\Classfied\add_listing.php:0
[14-Jun-2018 11:22:50 Asia/Kolkata] PHP 2. imagecreatefrompng() C:\wamp\www\Classfied\add_listing.php:117
[14-Jun-2018 11:22:50 Asia/Kolkata] PHP Warning: imagesx() expects parameter 1 to be resource, boolean given in C:\wamp\www\Classfied\add_listing.php on line 180
[14-Jun-2018 11:22:50 Asia/Kolkata] PHP Stack trace:
[14-Jun-2018 11:22:50 Asia/Kolkata] PHP 1. {main}() C:\wamp\www\Classfied\add_listing.php:0
[14-Jun-2018 11:22:50 Asia/Kolkata] PHP 2. resizePreservingAspectRatio() C:\wamp\www\Classfied\add_listing.php:120
[14-Jun-2018 11:22:50 Asia/Kolkata] PHP 3. imagesx() C:\wamp\www\Classfied\add_listing.php:180
[14-Jun-2018 11:22:50 Asia/Kolkata] PHP Warning: imagesy() expects parameter 1 to be resource, boolean given in C:\wamp\www\Classfied\add_listing.php on line 181
[14-Jun-2018 11:22:50 Asia/Kolkata] PHP Stack trace:
[14-Jun-2018 11:22:50 Asia/Kolkata] PHP 1. {main}() C:\wamp\www\Classfied\add_listing.php:0
[14-Jun-2018 11:22:50 Asia/Kolkata] PHP 2. resizePreservingAspectRatio() C:\wamp\www\Classfied\add_listing.php:120
[14-Jun-2018 11:22:50 Asia/Kolkata] PHP 3. imagesy() C:\wamp\www\Classfied\add_listing.php:181
[14-Jun-2018 11:22:50 Asia/Kolkata] PHP Warning: Division by zero in C:\wamp\www\Classfied\add_listing.php on line 182
[14-Jun-2018 11:22:50 Asia/Kolkata] PHP Stack trace:
[14-Jun-2018 11:22:50 Asia/Kolkata] PHP 1. {main}() C:\wamp\www\Classfied\add_listing.php:0
[14-Jun-2018 11:22:50 Asia/Kolkata] PHP 2. resizePreservingAspectRatio() C:\wamp\www\Classfied\add_listing.php:120
[14-Jun-2018 11:22:50 Asia/Kolkata] PHP Warning: imagecopyresampled() expects parameter 2 to be resource, boolean given in C:\wamp\www\Classfied\add_listing.php on line 198
[14-Jun-2018 11:22:50 Asia/Kolkata] PHP Stack trace:
[14-Jun-2018 11:22:50 Asia/Kolkata] PHP 1. {main}() C:\wamp\www\Classfied\add_listing.php:0
[14-Jun-2018 11:22:50 Asia/Kolkata] PHP 2. resizePreservingAspectRatio() C:\wamp\www\Classfied\add_listing.php:120
[14-Jun-2018 11:22:50 Asia/Kolkata] PHP 3. imagecopyresampled() C:\wamp\www\Classfied\add_listing.php:198
[14-Jun-2018 11:23:17 Asia/Kolkata] PHP Warning: imagecreatefrompng(): 'upload/7d64d4edebe88b16d3285b1bde173c23.png' is not a valid PNG file in C:\wamp\www\Classfied\add_listing.php on line 117
[14-Jun-2018 11:23:17 Asia/Kolkata] PHP Stack trace:
[14-Jun-2018 11:23:17 Asia/Kolkata] PHP 1. {main}() C:\wamp\www\Classfied\add_listing.php:0
[14-Jun-2018 11:23:17 Asia/Kolkata] PHP 2. imagecreatefrompng() C:\wamp\www\Classfied\add_listing.php:117
[14-Jun-2018 11:23:17 Asia/Kolkata] PHP Warning: imagesx() expects parameter 1 to be resource, boolean given in C:\wamp\www\Classfied\add_listing.php on line 180
[14-Jun-2018 11:23:17 Asia/Kolkata] PHP Stack trace:
[14-Jun-2018 11:23:17 Asia/Kolkata] PHP 1. {main}() C:\wamp\www\Classfied\add_listing.php:0
[14-Jun-2018 11:23:17 Asia/Kolkata] PHP 2. resizePreservingAspectRatio() C:\wamp\www\Classfied\add_listing.php:120
[14-Jun-2018 11:23:17 Asia/Kolkata] PHP 3. imagesx() C:\wamp\www\Classfied\add_listing.php:180
[14-Jun-2018 11:23:17 Asia/Kolkata] PHP Warning: imagesy() expects parameter 1 to be resource, boolean given in C:\wamp\www\Classfied\add_listing.php on line 181
[14-Jun-2018 11:23:17 Asia/Kolkata] PHP Stack trace:
[14-Jun-2018 11:23:17 Asia/Kolkata] PHP 1. {main}() C:\wamp\www\Classfied\add_listing.php:0
[14-Jun-2018 11:23:17 Asia/Kolkata] PHP 2. resizePreservingAspectRatio() C:\wamp\www\Classfied\add_listing.php:120
[14-Jun-2018 11:23:17 Asia/Kolkata] PHP 3. imagesy() C:\wamp\www\Classfied\add_listing.php:181
[14-Jun-2018 11:23:17 Asia/Kolkata] PHP Warning: Division by zero in C:\wamp\www\Classfied\add_listing.php on line 182
[14-Jun-2018 11:23:17 Asia/Kolkata] PHP Stack trace:
[14-Jun-2018 11:23:17 Asia/Kolkata] PHP 1. {main}() C:\wamp\www\Classfied\add_listing.php:0
[14-Jun-2018 11:23:17 Asia/Kolkata] PHP 2. resizePreservingAspectRatio() C:\wamp\www\Classfied\add_listing.php:120
[14-Jun-2018 11:23:17 Asia/Kolkata] PHP Warning: imagecopyresampled() expects parameter 2 to be resource, boolean given in C:\wamp\www\Classfied\add_listing.php on line 198
[14-Jun-2018 11:23:17 Asia/Kolkata] PHP Stack trace:
[14-Jun-2018 11:23:17 Asia/Kolkata] PHP 1. {main}() C:\wamp\www\Classfied\add_listing.php:0
[14-Jun-2018 11:23:17 Asia/Kolkata] PHP 2. resizePreservingAspectRatio() C:\wamp\www\Classfied\add_listing.php:120
[14-Jun-2018 11:23:17 Asia/Kolkata] PHP 3. imagecopyresampled() C:\wamp\www\Classfied\add_listing.php:198
[14-Jun-2018 11:23:41 Asia/Kolkata] PHP Warning: imagecreatefrompng(): 'upload/ee1b337a631f0c99704a2e2dfbd5667d.png' is not a valid PNG file in C:\wamp\www\Classfied\add_listing.php on line 117
[14-Jun-2018 11:23:41 Asia/Kolkata] PHP Stack trace:
[14-Jun-2018 11:23:41 Asia/Kolkata] PHP 1. {main}() C:\wamp\www\Classfied\add_listing.php:0
[14-Jun-2018 11:23:41 Asia/Kolkata] PHP 2. imagecreatefrompng() C:\wamp\www\Classfied\add_listing.php:117
[14-Jun-2018 11:23:41 Asia/Kolkata] PHP Warning: imagesx() expects parameter 1 to be resource, boolean given in C:\wamp\www\Classfied\add_listing.php on line 180
[14-Jun-2018 11:23:41 Asia/Kolkata] PHP Stack trace:
[14-Jun-2018 11:23:41 Asia/Kolkata] PHP 1. {main}() C:\wamp\www\Classfied\add_listing.php:0
[14-Jun-2018 11:23:41 Asia/Kolkata] PHP 2. resizePreservingAspectRatio() C:\wamp\www\Classfied\add_listing.php:120
[14-Jun-2018 11:23:41 Asia/Kolkata] PHP 3. imagesx() C:\wamp\www\Classfied\add_listing.php:180
[14-Jun-2018 11:23:41 Asia/Kolkata] PHP Warning: imagesy() expects parameter 1 to be resource, boolean given in C:\wamp\www\Classfied\add_listing.php on line 181
[14-Jun-2018 11:23:41 Asia/Kolkata] PHP Stack trace:
[14-Jun-2018 11:23:41 Asia/Kolkata] PHP 1. {main}() C:\wamp\www\Classfied\add_listing.php:0
[14-Jun-2018 11:23:41 Asia/Kolkata] PHP 2. resizePreservingAspectRatio() C:\wamp\www\Classfied\add_listing.php:120
[14-Jun-2018 11:23:41 Asia/Kolkata] PHP 3. imagesy() C:\wamp\www\Classfied\add_listing.php:181
[14-Jun-2018 11:23:41 Asia/Kolkata] PHP Warning: Division by zero in C:\wamp\www\Classfied\add_listing.php on line 182
[14-Jun-2018 11:23:41 Asia/Kolkata] PHP Stack trace:
[14-Jun-2018 11:23:41 Asia/Kolkata] PHP 1. {main}() C:\wamp\www\Classfied\add_listing.php:0
[14-Jun-2018 11:23:41 Asia/Kolkata] PHP 2. resizePreservingAspectRatio() C:\wamp\www\Classfied\add_listing.php:120
[14-Jun-2018 11:23:41 Asia/Kolkata] PHP Warning: imagecopyresampled() expects parameter 2 to be resource, boolean given in C:\wamp\www\Classfied\add_listing.php on line 198
[14-Jun-2018 11:23:41 Asia/Kolkata] PHP Stack trace:
[14-Jun-2018 11:23:41 Asia/Kolkata] PHP 1. {main}() C:\wamp\www\Classfied\add_listing.php:0
[14-Jun-2018 11:23:41 Asia/Kolkata] PHP 2. resizePreservingAspectRatio() C:\wamp\www\Classfied\add_listing.php:120
[14-Jun-2018 11:23:41 Asia/Kolkata] PHP 3. imagecopyresampled() C:\wamp\www\Classfied\add_listing.php:198
答案 0 :(得分:0)
<强> 已更新 强>
PHP GD库提供了几种创建图像对象/资源的功能。这些名称很容易跟随imagecreatefromjpeg()
,imagecreatefrompng()
等等......但是这些要求您正在为函数提供正确的图像类型,否则它们很可能会失败或出现问题。顺便说一句,没有位图的本机功能。在php.net上,一个人为imaagecreatefrombmp()
写了一个非常好的函数。
还有另一个名为imagecreatefromstring()
的函数。这是我使用的唯一一个。如果无法从字符串生成对象,此函数也将返回false,因此图像出现问题。
$source = imagecreatefromstring(file_get_contents($file));
if($source){
//Do something with your image. This is where you will do some additional checks for dimensions and for mime-type.
} else{
echo 'There was an error with your image.';
}
至于你所有的错误。它们是不创建有效图像对象/资源的结果。
从错误列表中:
createimagefrompng()
- 这是因为图像不是png,或者您没有正确的图像路径。确保检查您正在使用的路径上是否存在该文件。请改用imagecreatefromstring()
。imagesx()
&amp; imagesy()
- 这些都需要图像资源作为参数。您可以使用imagecreatefromstring()
函数获取图像资源。 imagecopyresamped()
- 第二个参数需要是资源。再次,使用imagecreatefromstring()
生成资源。此外,这很重要。完成后,请确保您正在销毁图像资源。它们会快速消耗大量内存。
imagedestroy($resource)
由于所有错误,您的代码可能甚至无法访问您的查询。
修复所有这些问题然后如果仍然无法解决问题,请使用最新代码发布另一个问题。