我有以下代码,如果此文件夹不存在,则在uploads文件夹中创建文件夹年份和月份。
然后我调用我的函数uploadImage在month文件夹中进行上传:
我的代码:
if(!empty($_FILES['thumb']['tmp_name']))
{
$folder = '../uploads/';
$year = date('Y');
$month = date('m');
if(!file_exists($folder.$year)){
mkdir($folder.$year,0755);
}
if(!file_exists($folder.$year.'/'.$month)){
mkdir($folder.$year.'/'.$month,0755);
}
$img = $_FILES['thumb']; //get thumb of the form
$ext = substr($img['name'],-3);
$f['thumb'] = $year.'/'.$month.'/'.$f['url'].'.'.$ext;
uploadImage($img['tmp_name'], $f['url'].'.'.$ext, '800', $folder);
}
我的上传功能:
function uploadImage($tmp, $name, $width, $folder){
$ext = substr($name,-3);
switch($ext){
case 'jpg': $img = imagecreatefromjpeg($tmp); break;
case 'png': $img = imagecreatefrompng($tmp); break;
case 'gif': $img = imagecreatefromgif($tmp); break;
}
$x = imagesx($img);
$y = imagesy($img);
$height = ($width*$y) / $x;
$newImage = imagecreatetruecolor($width, $height);
imagealphablending($newImage,false);
imagesavealpha($newImage,true);
imagecopyresampled($newImage, $img, 0, 0, 0, 0, $width, $height, $x, $y);
switch($ext){
case 'jpg': imagejpeg($newImage, $folder.$name, 100); break;
case 'png': imagepng($newImage, $folder.$name); break;
case 'gif': imagegif($newImage, $folder.$name); break;
}
imagedestroy($img);
imagedestroy($newImage);
}
对我来说,一切似乎都很好,而且我没有错误,但上传工作无效!
答案 0 :(得分:1)
看起来您将基本文件夹传递给上传,应该是这样的:
'800', $folder.$year.'/'.$month);
此外,您的$ name似乎是某种类型的URL,它必须是有效的文件名。