PHP+ajax image file asynchronous upload sample code

WBOY
Release: 2016-07-25 08:53:07
Original
1130 people have browsed it
  1. jQuery.extend({

  2. createUploadIframe: function(id, uri)
  3. {
  4. //create frame
  5. var frameId = 'jUploadFrame' + id;
  6. var iframeHtml = '
  • Copy the code

    index.js file:

    1. $(function(){
    2. $("#upload_file").change(function(){
    3. $("#uploadFrom").submit();
    4. });
    5. });
    6. function stopSend(str){
    7. var im="";
    8. $("#msg").append(im);
    9. }
    Copy code

    upload.php file:

    1. $file=$_FILES['upfile'];
    2. $name=rand(0,500000).dechex(rand(0,10000)).".jpg";
    3. move_uploaded_file ($file['tmp_name'],"upload/images/".$name);
    4. //Call the js function of the iframe parent window
    5. echo "

    6. Browse...
    7. Upload
  • Copy code

    php代码:

    1. if(isset($_FILES["myfile"]))
    2. {
    3. $ret = array();
    4. $uploadDir = 'images'.DIRECTORY_SEPARATOR.date("Ymd").DIRECTORY_SEPARATOR;
    5. $dir = dirname(__FILE__).DIRECTORY_SEPARATOR.$uploadDir;
    6. file_exists($dir) || (mkdir($dir,0777,true) && chmod($dir,0777));
    7. if(!is_array($_FILES["myfile"]["name"])) //single file
    8. {
    9. $fileName = time().uniqid().'.'.pathinfo($_FILES["myfile"]["name"])['extension'];
    10. move_uploaded_file($_FILES["myfile"]["tmp_name"],$dir.$fileName);
    11. $ret['file'] = DIRECTORY_SEPARATOR.$uploadDir.$fileName;
    12. }
    13. echo json_encode($ret);
    14. }
    15. ?>
    复制代码


    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 Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!