PHP';mkdir():文件存在';当文件大小太大时?
我正在制作一个上传脚本,将多张照片上传到一个文件夹,同时上传到另一个文件夹 我的问题是,当我上传超过2.5mb的文件时,出现错误:PHP';mkdir():文件存在';当文件大小太大时?,php,file-upload,Php,File Upload,我正在制作一个上传脚本,将多张照片上传到一个文件夹,同时上传到另一个文件夹 我的问题是,当我上传超过2.5mb的文件时,出现错误:mkdir():文件存在。。。我想。Myphpinfo()显示我的post_max_大小为8M,而upload_max_文件大小为40M。所以我不确定为什么它只在大型或多个文件上执行此操作。如果我上传较小的图像(~1mb),那么一切正常。但是当我上传大文件时,我会出现这个错误 注意:脚本墙 以下是功能: <?PHP function makeFile($
mkdir():文件存在
。。。我想。Myphpinfo()
显示我的post_max_大小
为8M,而upload_max_文件大小
为40M。所以我不确定为什么它只在大型或多个文件上执行此操作。如果我上传较小的图像(~1mb),那么一切正常。但是当我上传大文件时,我会出现这个错误
注意:脚本墙
以下是功能:
<?PHP
function makeFile($gallery_title, $gallery_date, $target_dir){
$new_file = fopen($target_dir . "info.php", "w") or die("Unable to open file!");
$txt = '<div class="info"><div class="title">'.$gallery_title.'</div><div class="date">'.$gallery_title.'</div><div class="enter">Enter</div></div>';
fwrite($new_file, $txt);
fclose($new_file);
}
function resize($file, $width, $height, $target_dir, $new_name, $image_type){
/* Get original image x y*/
list($w, $h) = getimagesize($file);
/* calculate new image size with ratio */
$ratio = max($width/$w, $height/$h);
$h = ceil($height / $ratio);
$x = ($w - $width / $ratio) / 2;
$w = ceil($width / $ratio);
/* new file name */
$path = $target_dir.$new_name;
/* read binary data from image file */
$imgString = file_get_contents($file);
/* create image from string */
$image = imagecreatefromstring($imgString);
$tmp = imagecreatetruecolor($width, $height);
imagecopyresampled($tmp, $image,
0, 0,
$x, 0,
$width, $height,
$w, $h);
/* Save image */
switch ($image_type) {
case 'image/jpeg':
imagejpeg($tmp, $path, 100);
break;
case 'image/png':
imagepng($tmp, $path, 0);
break;
case 'image/gif':
imagegif($tmp, $path);
break;
default:
exit;
break;
}
return $path;
/* cleanup memory */
imagedestroy($image);
imagedestroy($tmp);
}
?>
下面是脚本:
<?PHP
error_reporting(E_ALL);
$gallery_name = $_POST['galleryName'];
$gallery_title = $_POST['galleryTitle'];
$gallery_date = $_POST['galleryDate'];
$target_dir = "../galleries/".$gallery_name."/";
$target_dir_high = $target_dir."high/";
$target_dir_low = $target_dir."low/";
$valid_formats = array("jpg", "png", "gif", "zip", "bmp");
$max_file_size = 1024000000; //1mb
$count = 0;
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
if (mkdir($target_dir, 0777, true) && mkdir($target_dir_high, 0777, true) && mkdir($target_dir_low, 0777, true)) {
makeFile($gallery_title, $gallery_date, $target_dir); //Makes the info file
// Loop $_FILES to execute all files
foreach ($_FILES['files']['name'] as $f => $name) {
if ($_FILES['files']['error'][$f] == 4) {
$message[] = "$name has an error";
continue; // Skip file if any error found
}
if ($_FILES['files']['error'][$f] == 0) {
if( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
$message[] = "$name is not a valid format";
continue; // Skip invalid file formats
} else { // No error found! Move uploaded files
$old_name = $_FILES['files']['name'][$f];
$ext = end((explode(".", $old_name)));
$count_padded = sprintf("%02d", $count);
$new_name = $gallery_name."-".$count_padded.".".$ext;
$image_type = $_FILES['files']['type'][$f];
if(move_uploaded_file($_FILES['files']['tmp_name'][$f], resize($_FILES['files']['tmp_name'][$f], 1920, 1080, $target_dir_high, $new_name, $image_type))){
list($w, $h) = getimagesize($target_dir_high.$new_name);
$max_size = 500;
$long_side = max($w, $h);
$ratio = ($max_size/$long_side); //This is a percentage
$new_width = floor($w*$ratio);
$new_height = floor($h*$ratio);
$destImage = imagecreatetruecolor($new_width, $new_height);
$sourceImage = imagecreatefromjpeg($target_dir_high.$new_name);
imagecopyresampled($destImage, $sourceImage, 0, 0, 0, 0, $new_width, $new_height, $w, $h);
imagejpeg($destImage, $target_dir_low.$new_name, 100);
imagedestroy($destImage);
imagedestroy($sourceImage);
if($count == 0){
copy($target_dir_low.$new_name, $target_dir.$new_name);
}
$count++;
echo $message = 'Congratulations! The file '.$target_dir.$new_name.' was accepted.<br />';
}
}
}
}
} else {
die('This gallery exists. Go back and rename.');
}
}
?>
首先,您需要检查目录是否已经存在,使用它。i、 e:
$dir = "/home/path/dir";
if(is_dir($dir))
{
echo ("dir $dir already exists");
}
else
{
echo ("dir $dir doesn't exist");
}
(菲律宾比索4,菲律宾比索5) is_dir-告诉文件名是否为目录
返回
TRUE
如果文件名存在并且是目录,FALSE
否则
你说你正在上传多个文件?它们的组合大小不能超过最大post大小,因此在您的情况下,8MB应该使用
basename($galleryName)
来初始化$target\u dir
。想象一下,如果有人输入一个以(多个)开头的标题,会发生什么情况。/
1024000000
字节不是1MB(如注释中所述)。它是或。查查。@Augwa,谢谢你-我想这可能会回答这个问题。我将对此进行一些测试。佩德罗,谢谢你-我认为问题更多的是我正在达到我的post_max_size
,这导致了这个错误的发生。所以,如果我把代码放在你建议的地方,我只会得到错误“目录已经存在”。我将进一步探讨post\u max\u size
is_dir