Pergunta

So I've got this file index.php that gives a form to upload an image.

<!DOCTYPE html>
<html>
    <head>
        <title>Unnamed</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">

        <!-- Include styles -->
        <link href = "bootstrap/css/bootstrap.min.css" rel="stylesheet">
        <link href = "css/style.css" rel="stylesheet">

        <!-- Include jQuery -->
        <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
        <!-- Include form plugin -->
        <script src="http://malsup.github.com/jquery.form.js"></script>
        <!-- Javascript --> 
        <script> 
            // wait for the DOM to be loaded 
            $(document).ready(function() { 
                // bind 'myForm' and provide a simple callback function 
                $('#insert_movie').ajaxForm(function() { 
                    $('#message').load('upload_file.php'); 
                }); 
            }); 
        </script>
    </head>

    <body>
        <!-- Message banner -->
        <div id='message'></div>

        <!-- Insert a movie into the database -->
        <h1>Add a new movie to the list</h1>
        <form id="insert_movie" action="upload_file.php" method="post" enctype="multipart/form-data">

            <label for="file">Filename:</label>
            <input type="file" name="file" id="file"><br>
            <input type="submit" name="submit" value="Submit">
        </form>

    </body>
</html>

NOTE: it might not be familiar to you guys, but I've used a plug-in for jQuery in order to do easy AJAX-ing. You might want to check this link

I evaluate this form via upload_file.php:

<?php
error_reporting(E_ALL);
//Upload the image
$allow = array("jpg", "jpeg", "gif", "png");

$todir = 'images/';

if(isset($_POST['submit'])){
    if ( $_FILES['file']['tmp_name'] ) // is the file uploaded yet?
    {
        $info = explode('.', strtolower( $_FILES['file']['name']) ); // whats the extension of the file

        if ( in_array( end($info), $allow) ) // is this file allowed
        {
            if ( move_uploaded_file( $_FILES['file']['tmp_name'], $todir . basename($_FILES['file']['name'] ) ) )
            {
                echo "Great";
            }
        }
        else
        {
            // error this file ext is not allowed
            echo 'Choose another file extension';
        }
    }
}
 echo "this works";

?>

So the problem is that nothing beside "this works" is ever rendered in the tags. However the upload was performed perfectly.

All help is appreciated!

EDIT: so when I just forget the ajaxing and simply get redirected to upload_file.php, everything gets rendered perfectly fine...

Foi útil?

Solução

The problem is that your form is send via ajaxForm to upload_file.php and everything should work fine in this call returning the expected string. With this server response your callback function is called and here you call upload_file.php again without any parameters and render the result in the div, that is only "this works" . You should use the plugin like the following:

$('#insert_movie').ajaxForm({
      success: function(response) { 
           $('#message').html(response); 
      }});

Outras dicas

May be you don't need use !!$_FILES['file']['tmp_name'] better write this

if ($_FILES['file']['tmp_name'])

But better you should learn how to debug your code, that's the easiest way:

if(isset($_POST['submit'])){
var_dump($_FILES['file']['tmp_name']);
if ( !!$_FILES['file']['tmp_name'] ) // is the file uploaded yet?
{
    $info = explode('.', strtolower( $_FILES['file']['name']) ); // whats the extension of the file
    var_dump($info);
    if ( in_array( end($info), $allow) ) // is this file allowed
    {
        var_dump($todir . basename($_FILES['file']['name']));
        if ( move_uploaded_file( $_FILES['file']['tmp_name'], $todir . basename($_FILES['file']['name'] ) ) )
        {
            echo "Great";
        }
    }
    else
    {
        // error this file ext is not allowed
        echo 'Choose another file extension';
    }
}
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top