Home  >  Article  >  Backend Development  >  Simple PHP image upload program_PHP tutorial

Simple PHP image upload program_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:52:57720browse

The first type:
php part

Copy code The code is as follows:

if($_FILES['file']['error'] > 0){
echo '!problem:';
switch($_FILES['file']['error'])
{
case 1: echo 'The file size exceeds the server limit';
break;
case 2: echo 'The file is too large! ';
break;
case 3: echo 'Only part of the file is loaded! ';
break;
case 4: echo 'File loading failed! '; The file is too large! ';
exit;
}
if($_FILES['file']['type']!='image/jpeg' && $_FILES['file']['type']!= 'image/gif'){
echo 'The file is not a JPG or GIF image! ';
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 'Failed to move file! ';
exit;
}
}
else
{
echo 'problem!';
exit; success!
';
echo 'File size:' . $_FILES['file']['size'] . 'Bytes' . '
';
echo 'File path:' . $upfile;
echo '
' . '

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

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



Second type:



Copy code

The code is as follows:


if(empty($_GET[submit]))

{

?>

Send this file:


}else{
$path="uploadfiles/"; //Upload path

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


if(!file_exists( $path))
{
//Check if the folder exists, if not, create it and give it the highest permissions
mkdir("$path", 0700);
}//END IF
//File formats allowed to be uploaded
$tp = array("image/gif","image/pjpeg","image/png");
//Check whether the uploaded file is allowed to be uploaded Type
if(!in_array($_FILES["filename"]["type"],$tp))
{
echo "Incorrect format";
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);
//Specially note that the first parameter passed to move_uploaded_file here is the temporary file uploaded to the server
if($result)
{
//echo "Upload successful !".$file2;
echo "";
}//END IF


}

?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/318777.htmlTechArticleFirst type: Copy the php part of the code as follows: ?php if($_FILES['file'][' error']0){ echo'!problem:'; switch($_FILES['file']['error']) { case1:echo'The file size exceeds the server limit...
Statement:
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