我创建了一个php文件,首先让dir上传图片第二部分上传图片到它,但下次再次尝试制作dir并给出错误,脚本应该接受创建的dir并继续上传图片:还检查是不是脚本正常或建议更好的脚本上传图像和显示图像。
<?php
//create the new directory
$newDir = mkdir('newupload', 0777);
$info_file_exts = array("jpg", "jpeg", "gif", "png");
$info_upload_exts = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 2000000)
&& in_array($info_upload_exts, $info_file_exts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
$info_file_exts = array("jpg", "jpeg", "gif", "png");
if($info_file_exts[0]=='jpg'){
$ink=explode('.'.$info_file_exts[0],$_FILES["file"]["name"]);
$time=time();
$info_new_file_name =$ink[0].'_'.$time.'.'.$info_file_exts[0];
}
else if($info_file_exts[1]=='jpeg'){
$ink=explode('.'.$info_file_exts[1],$_FILES["file"]["name"]);
$time=time();
$info_new_file_name =$ink[0].'_'.$time.'.'.$info_file_exts[0];
}
else if($info_file_exts[2]=='gif'){
$ink=explode('.'.$info_file_exts[2],$_FILES["file"]["name"]);
$time=time();
$info_new_file_name =$ink[0].'_'.$time.'.'.$info_file_exts[0];
}
else if($info_file_exts[3]=='png'){
$ink=explode('.'.$info_file_exts[3],$_FILES["file"]["name"]);
$time=time();
$info_new_file_name =$ink[0].'_'.$time.'.'.$info_file_exts[0];
}
$info_old_file_path="newupload/" . $_FILES["file"]["name"];
$info_new_file_path="newupload/" . $info_new_file_name;
move_uploaded_file($_FILES["file"]["tmp_name"],
"newupload/" . $_FILES["file"]["name"]);
// echo "<div class='sucess'>"."Stored in: " . "c:\wamp\www\upload/newupload/" . $_FILES["file"]["name"]."</div>";
$name=rename($info_old_file_path,$info_new_file_path);
echo "<img src='newupload/" . $info_new_file_name."' style='width:300; height:250px;' />";
echo "</br>Your file is ".$_FILES['file']['name']." uploaded.";
}
}
else
{
echo "<div class='error'>Invalid file</div>";
}
?>
答案 0 :(得分:0)
在创建目录之后检查目录是否已设置,如下所示
if(!is_dir($newDir))
{
//create the new directory
$newDir = mkdir('newupload', 0777);
}
答案 1 :(得分:0)
只需替换
if (!file_exists('newupload')) {
$newDir = mkdir('newupload', 0777);
}
它将检查文件/文件夹是否存在,它不会再尝试创建它。