Home > php教程 > php手册 > 简单的PHP图片上传程序

简单的PHP图片上传程序

WBOY
Release: 2016-06-06 20:36:14
Original
971 people have browsed it

简单的PHP图片上传程序

第一种:
php部分

复制代码 代码如下:


if($_FILES['file']['error'] > 0){
echo '!problem:';
switch($_FILES['file']['error'])
{
case 1: echo '文件大小超过服务器限制';
break;
case 2: echo '文件太大!';
break;
case 3: echo '文件只加载了一部分!';
break;
case 4: echo '文件加载失败!';
break;
}

exit;
}
if($_FILES['file']['size'] > 1000000){
echo '文件过大!';
exit;
}
if($_FILES['file']['type']!='image/jpeg' && $_FILES['file']['type']!='image/gif'){
echo '文件不是JPG或者GIF图片!';
exit;
}
$today = date("YmdHis");
$filetype = $_FILES['file']['type'];
if($filetype == 'image/jpeg'){
$type = '.jpg';
}
if($filetype == 'image/gif'){
$type = '.gif';
}
$upfile = 'upfile/' . $today . $type;
if(is_uploaded_file($_FILES['file']['tmp_name']))
{
if(!move_uploaded_file($_FILES['file']['tmp_name'], $upfile))
{
echo '移动文件失败!';
exit;
}
}
else
{
echo 'problem!';
exit;
}
echo '

success!


';
echo '文件大小:' . $_FILES['file']['size'] . '字节' . '
';
echo '文件路径:' . $upfile;
echo '
' . '

';
$dirr = 'upfile/';
$dir = opendir($dirr);
echo $dirr . '--Listing:

    ';
    while($file = readdir($dir)){
    echo "
  • $file
  • ";
    }
    echo '
';
closedir($dir);
?>


第二种:

复制代码 代码如下:



if(empty($_GET[submit]))

{

?>


Send this file:


}else{
$path="uploadfiles/"; //上传路径

//echo $_FILES["filename"]["type"];


if(!file_exists($path))
{
//检查是否有该文件夹,如果没有就创建,并给予最高权限
mkdir("$path", 0700);
}//END IF
//允许上传的文件格式
$tp = array("image/gif","image/pjpeg","image/png");
//检查上传文件是否在允许上传的类型
if(!in_array($_FILES["filename"]["type"],$tp))
{
echo "格式不对";
exit;
}//END IF
if($_FILES["filename"]["name"])
{
$file1=$_FILES["filename"]["name"];
$file2 = $path.time().$file1;
$flag=1;
}//END IF
if($flag) $result=move_uploaded_file($_FILES["filename"]["tmp_name"],$file2);
//特别注意这里传递给move_uploaded_file的第一个参数为上传到服务器上的临时文件
if($result)
{
//echo "上传成功!".$file2;
echo "";
}//END IF


}

?>
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template