下面是我尝试的代码,但显示了一些警告错误:
警告:getimagesize(C:\ xampp \ tmp \ php2F0B.tmp):无法打开流:第55行的C:\ xampp \ htdocs \ maahima \ admin \ uploadBulkImages.php中没有此类文件或目录
警告:imagejpeg()要求参数1为资源,字符串在第66行的C:\ xampp \ htdocs \ maahima \ admin \ uploadBulkImages.php中给出
我希望代码使用压缩图像批量插入文件夹中的图像。
<?php
if(isset($_FILES['file']['tmp_name'])){
$pro_image = $_FILES['file']['tmp_name'];
$no_of_file = count($_FILES['file']['tmp_name']);
$name = ''; $type = ''; $size = ''; $error = '';
for($i=0;$i < $no_of_file;$i++){
if(! is_uploaded_file($_FILES['file']['tmp_name'][$i])){
header("location:add_products.php?error=er8r5r");
}
else{
if(move_uploaded_file($_FILES['file']['tmp_name'][$i],"../products/".$_FILES['file']['name'][$i])){
if ($_FILES["file"]["error"][$i] > 0) {
$error = $_FILES["file"]["error"];
}
else {
$image="";
$error = "Uploaded image should be jpg or gif or png";
$url = '../smallSize-Image/'.$_FILES['file']['name'][$i];
echo $url;
$source_url=$_FILES["file"]["tmp_name"][$i];
$destination_url=$url;
$quality=12;
$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);
imagejpeg($image, $destination_url, $quality);
return $destination_url;
$buffer = file_get_contents($url);
/* Force download dialog... */
//header("Content-Type: application/force-download");
//header("Content-Type: application/octet-stream");
//header("Content-Type: application/download");
// header("location:add_products.php?succcess=s8sc5s");
/* Don't allow caching... */
// header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
/* Set data type, size and filename */
//header("Content-Type: application/octet-stream");
//header("Content-Length: " . strlen($buffer));
//header("Content-Disposition: attachment; filename=$url");
/* Send our file... */
echo $buffer;
}
}
else{
header("location:add_products.php?error=er8r5r");
}
}
}
}
else{
}
?>
答案 0 :(得分:1)
你的脚本有很多不合逻辑的功能。如果您要重定向,则需要exit
。你不应该在脚本中间有return
,这有点随机(可能这是未完成的脚本,它在另一个函数/方法中?)。如果我这样做,我会做一些事情。
如果您还没有配置有一些绝对定义,我会创建一个并将其包含在顶部的每个初始加载页面上。
如果正确实施并且已经证明,这是经过测试和运作的。使用它的一部分,它的一部分,或者不使用它,但它确实有用:
<强> /config.php 强>
<?php
define('DS',DIRECTORY_SEPARATOR);
define('ROOT_DIR',__DIR__);
define('UPLOAD_DIR',ROOT_DIR.DS.'uploads'.DS.'images');
define('THUMB_DIR',ROOT_DIR.DS.'uploads'.DS.'thumbs');
define('VENDOR_DIR',ROOT_DIR.DS.'vendors');
我会考虑制作一些快速函数,在这种情况下是一个类/方法系统来完成一些简单的任务。
<强> /vendors/Files.php 强>
class Files
{
/*
** @description This will turn a numbered $_FILES array to a normal one
*/
public static function normalize($key = 'file')
{
foreach($_FILES[$key]['tmp_name'] as $fKey => $value) {
$FILES[] = array(
'tmp_name'=>$_FILES[$key]['tmp_name'][$fKey],
'name'=>$_FILES[$key]['name'][$fKey],
'error'=>$_FILES[$key]['error'][$fKey],
'size'=>$_FILES[$key]['size'][$fKey],
'type'=>$_FILES[$key]['type'][$fKey]
);
}
return $FILES;
}
/*
** @description I would contain this step into an editable method for
** ease of use and reuse.
*/
public static function compress($path, $dest, $quality = 12)
{
$info = getimagesize($path);
if($info['mime'] == 'image/jpeg')
$image = imagecreatefromjpeg($path);
elseif($info['mime'] == 'image/gif')
$image = imagecreatefromgif($path);
elseif($info['mime'] == 'image/png')
$image = imagecreatefrompng($path);
imagejpeg($image,$dest,$quality);
}
}
把它们放在一起,在这种情况下,我可能会抛出异常并将错误保存到会话中。在add_products.php
页面上循环浏览它们。
# Add our config
include(__DIR__.DIRECTORY_SEPARATOR.'config.php');
# Add our file handler class (spl_autoload_register() is a better option)
include(VENDOR_DIR.DS.'Files.php');
# Observe for file upload
if(!empty($_FILES['file']['tmp_name'])){
# Use our handy convert method
$FILES = Files::normalize();
# Try here
try {
# Loop through our normalized file array
foreach($FILES as $i => $file){
# Throw error exception here
if(!is_uploaded_file($file['tmp_name']))
throw new Exception('File is not an uploaded document.');
# I like to add a create directory line or two to cover all bases
if(!is_dir(UPLOAD_DIR)) {
if(!mkdir(UPLOAD_DIR,0755,true))
throw new Exception('Upload folder could not be created.');
}
# Ditto
if(!is_dir(THUMB_DIR)) {
if(!mkdir(THUMB_DIR,0755,true))
throw new Exception('Thumb folder could not be created.');
}
# Create an absolute path for the full size upload
$path = UPLOAD_DIR.DS.$file['name'];
# If no errors and file is moved properly, compress and save thumb
if(($file["error"]) == 0 && move_uploaded_file($file['tmp_name'],$path))
# Create a thumbnail from/after the file you moved, not the temp file
Files::compress($path,THUMB_DIR.DS.$file['name']);
# Throw error on fail
else
throw new Exception('An unknown upload error occurred.');
}
}
# Catch, assign, redirect
catch (Exception $e) {
$_SESSION['errors'][] = $e->getMessage();
header("location: add_products.php?error=er8r5r");
exit;
}
}