Simple PHP image upload program _php tips

Source: Internet
Author: User
Tags file size
First type:
PHP section

Copy Code code as follows:

<?php
if ($_files[' file '] [' ERROR '] > 0) {
Echo '!problem: ';
Switch ($_files[' file '] [' ERROR '])
{
Case 1:echo ' file size exceeds server limit ';
Break
Case 2:echo ' file is too big! ';
Break
Case 3:echo ' file only loads a part! ';
Break
Case 4:echo ' file failed to load! ';
Break
}

Exit
}
if ($_files[' file '] [' size '] > 1000000) {
Echo ' file is too big! ';
Exit
}
if ($_files[' file '] [' type ']!= ' image/jpeg ' && $_files[' file '] [' type ']!= ' image/gif ']) {
Echo ' file is not a JPG or GIF picture! ';
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 ' Move file failed! ';
Exit
}
}
Else
{
Echo ' problem! ';
Exit
}
Echo ' echo ' File size: '. $_files[' file ' [' Size ']. ' bytes '. ' <Br> ';
Echo ' File path: '. $upfile;
Echo ' $dirr = ' upfile/';
$dir = Opendir ($DIRR);
Echo $dirr. '--listing:<ul> ';
while ($file = Readdir ($dir)) {
echo "<li> $file </li>";
}
Echo ' </ul> ';
Closedir ($dir);
?>


The second type:
Copy Code code as follows:

<?php

if (empty ($_get[submit]))

{

?>
<form enctype= "Multipart/form-data" action= "<?php $_server[' php_self ']?>?submit=1" method= "POST" >
Send This file: <input name= "filename" type= "file" >
<input type= "Submit" value= "Confirm upload" >
</form>
<?php
}else{
$path = "uploadfiles/"; Upload path

echo $_files["filename" ["type"];


if (!file_exists ($path))
{
Check if there is a folder, if it is not created, and gives the highest permissions
mkdir ("$path", 0700);
}//end IF
File formats that allow uploading
$TP = Array ("Image/gif", "Image/pjpeg", "image/png");
Check whether the uploaded file is in the type allowed to upload
if (!in_array ($_files["filename"] ["type"], $TP))
{
echo "wrong format";
Exit
}//end IF
if ($_files["filename" ["Name"])
{
$file 1=$_files["filename" ["Name"];
$file 2 = $path. Time (). $file 1;
$flag = 1;
}//end IF
if ($flag) $result =move_uploaded_file ($_files["filename"] ["Tmp_name"], $file 2);
Special note Here The first parameter passed to Move_uploaded_file is the temporary file uploaded to the server
if ($result)
{
echo "Upload success!". $file 2;
echo "<script language= ' JavaScript ' >";
echo "alert (\" Upload succeeded!) \");";
echo "Location= ' add_aaa.php?pname= $file 2 '";
echo "</script>";
}//end IF


}

?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.