Misloading error through ajax



  • The loading error occurs if there are one or more points in the name of the picture: 72247.343.jpg or fon.image.jpg I can't find in the violin what function is responsible for this.

    And if you can download the name.001.JPG() Make it up to the bottom register (001.jpg)

    I've got a load. ajax Use it. jquery.form.js Maybe the problem is somewhere. jquery.form.js

    Html:

    <div class="bloc_upload">   
        <form id="photo-frm" method="post" enctype="multipart/form-data">       
            <div id="preview99" class="file_upload">    
                <img src="css/img/delete.png" alt="Remove file" class="clear-image" id="" title="" width="30" height="30" />
                <img src="css/img/default.png" class="default" alt="Фото Незагрузилось !!!" id="file-preview"  />           
                <input type="file" name="photo" id="photo" />
                <input type="submit" id="upload-submit" name="Upload"  value="Upload_photo" />  
            </div>      
        </form>
    </div>
    

    Js:

    $(function(){   
       // Options for Ajax submittion
        var options = { 
            beforeSubmit: showRequest, 
            beforeSend: function()
            { $('#file-preview').attr('src', 'css/img/upload_send.png'); },
            success: showResponse , 
            url: 'upload_image.php',
            type: 'POST'   
        }; 
    
    // bind to the form's submit event 
    $('#photo-frm').submit(function() { 
        $(this).ajaxSubmit(options); 
        return false; 
    }); 
    
    // Before submitting
    function showRequest(formData, jqForm, options) {        
        // TODO: Do some validation before submitting
    } 
    
    // After submitting
    function showResponse(responseText, statusText, xhr, $form)  {
        // Preview file
        $('#file-preview').attr('src', './uploads/' + responseText);
    
        //$('#name-image').html(responseText); 
        $('.clear-image').attr('id', responseText);   
    
    
        // Assign file path to hidden input to be saved
        $('#image-name-text').val(responseText);
        // Show X image to clear-image image
        $('.clear-image').show();
    }           
    
    $('#photo').change(function(){
        // submit form
        $('#photo-frm').submit();
        $('#photo').hide();
    });
    
    // Remove picture clicked
    $('.clear-image').click(function(){
        // TODO: Remove file from server
    
        // Remove preview image
        $('#file-preview').attr('src', 'css/img/default.png');
        // Clear hidden and file input
        $('#photo').val('');
        $('#image-name-text').val('');      
        $('#fil').html('Файл не выбран');   
    
        // Hide X image 
        $('.clear-image').hide();  
        $('#photo').show();
    });
    

    });

    upload_image.php:

    $valid_formats = array("jpg", "png");
    $uploads = "./uploads/" ;

    if ( $_FILES['photo']['name'] )
    {
    $name = $_FILES['photo']['name'];
    $size = $_FILES['photo']['size'];

    if (strlen($name)) {
    list($sometext, $extension) = explode(".", $name);
    if (in_array($extension, $valid_formats))
    {
    if ($size < (2048 * 1024))
    {
    $imagename = time() . "." . $extension;
    $tmp = $_FILES['photo']['tmp_name'];
    if (move_uploaded_file($tmp, $uploads . $imagename)) {
    echo $imagename;
    } else {
    $imagename = "erorr/erorr_image.jpg";
    echo $imagename;
    }
    } else {
    $imagename = "erorr/erorr_2Mb.jpg";
    echo $imagename;
    }
    } else {
    $imagename = "erorr/erorr_format.jpg";
    echo $imagename;
    }
    } else {
    echo "Выберите фото...!";
    }
    exit();
    }

    We still need to connect the library. jquery-1.9.0.min.js and jquery.form.js



  • The error is in the line

    list($sometext, $extension) = explode(".", $name); 
    

    for example, where on the name of the file several - explode The mass will be returned with more than two elements, but only the first and second will be recorded in the variables.

    It is possible to take advantage of the function. http://php.net/manual/ru/function.end.php

    $extension = end(explode(".", $name));
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2