Html+php+ajax Uploading Files

Source: Internet
Author: User
Tags explode

HTML

<form class= "Formajax"
<?php if (!isset ($datalist)):? >action= "/project/projectattachment/insertattachinfo"//upload image for the first time

<?php Else:?> action= "/project/projectattachment/editattachinfo"//Modify Picture

<?php endif;?> method= "POST"
class= "Formajax form-horizontal" novalidate= "Novalidate"
enctype= "Multipart/form-data" >

<td><p class= "Short-input ue-clear" >
<label> Accessory Name:</label>
<input name= "Annexname" type= "text" id= " annexname"/>
<input name= "id" type= "hidden" id= "id"/>
<a name= "id" id= "sky_txt"/>
</p>

</td>

Ajax

$ ('. Formajax '). Ajaxsubmit (
                {
type: ' POST ',
URL: '/project/projectattachment/insertattachinfo ',
beforesumbit:function () {
$ ("#sky_txt"). html ("Picture crosses ...");
                    },
success:function (data)
                    {
var Datajson = $.parsejson (data)
if (datajson.success = = ' 0 ')
                        {
alert (datajson.msg);
                        }
                        {
window.parent.getDataList (0,10);
                        }
                    }
                }
            )

PHP Code

Public function insertattachinfo ()
    {
$name =$_files[' file ' [' name '];
$newname =explode('. ', $name); the//explode () function breaks a string into arrays. Explode (separator,string,limit); separator: Required. Specifies where to split the string. string: Required. The string to split.
$namesize =count ($newname);
$size = $_files[' file ' [' Size '];
$insertAccidentFilesData = Array ();
$title =explode ('. '). $newname [$namesize-1], $name) [0];
if (!empty ($_files))
        {
$path = '/uploads/filerurl/'. $_post[' Projectcode ']. " /";
$targetFolder = Fcpath. $path;
if (!is_dir($targetFolder)) mkdir($targetFolder); //Is_dir determine if the directory exists
$strarr = Explode ('. ', $_files[' file ' [' name ']);
$filename = str_replace("", "", $strarr [0].time (). ". $strarr [1]); the//str_replace () function replaces some other characters in a string with a string.
$futurename =iconv("Utf-8", "gb2312", RTrim ($targetFolder, '/'). '/'. $filename); //iconv Function Library can complete the conversion between various character sets
if (is_uploaded_file($_files[' file ' [' Tmp_name ']) && move_uploaded_file($_files[ ' File '] [' tmp_name '], $futurename)) {
$annextype = ' other ';
if (strstr($_files[' file ' [' type '], ' image '))
$annextype = ' picture ';
                 
$insertAccidentFilesData = Array (
' type ' = ' project attachment ',
' annexname ' = ' $_post[' annexname '],
' Annexurl ' and $path. $filename,
' projectcode ' = ' $_post[' Projectcode '],
' annextype ' = $annextype,
' createuser ' = $this->sessioninfo[' fullName '],
' createdatetime ' + date (' y-m-d h:i:s '),
                    
                );
$insertFile = $this->db->insert (' Sysattachs ', $insertAccidentFilesData);
$this->setlogs ("Upload attachment", $insertAccidentFilesData);
$result = Array (
' Success ' = ' 1 ',
' msg ' = ' + ' upload file successfully! ',
                );
        
}else{
$result = Array (
' success ' = ' 0 ',
' msg ' = ' = ' upload file failed! ',
                );
echo "<script>alert (' Upload file failed! ');</script> ";
            }
Echo Json_encode ($result);
//echo "<script>parent.location.reload ();</script>";
                
        }
    }

Public function editattachinfo () {
$id = $_post[' id '];
$annexname = $_post[' annexname ');
$sql = "SELECT * from sysattachs where id = '". $id. "'"; //Database SQL statement query
$query = $this->mydb->find ($sql); //Execute SQL statement
$this->setlogs ("Before modifying the attachment information", $query [' obj ']);
$sql = "Update sysattachs set annexname = '". $annexname. "' WHERE id = '. $id." "; //Update database
$this->mydb->execsql ($sql);
$sql = "SELECT * from sysattachs where id = '". $id. "'";
$query = $this->mydb->find ($sql);
$this->setlogs ("After modifying the attachment information", $query [' obj ']);
Echo Json_encode ($query);
    }

Html+php+ajax Uploading Files

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.