tags:

views:

139

answers:

1

I am practicing with PHP and AJAX but I have some problems!

I'm trying to get the filename, type, size from a jQuery alert after select an image to upload, but I keep getting an empty string.

<form action="upload_file.php" method="post" enctype="multipart/form-data">
    <label for="file">Filename:</label>
    <input type="file" name="my_file" />
    <input type="submit" name="submit" value="Submit" />
</form>

This form includes a JS file that has an ajax script that asks to a php page the filename of the posted file, but it doesn't work. Do I need to upload the file before I try to get this data?

The JS file:

$("input").change (function () {
    $.post("preview_uploader.php", { action:"get_size" }, function (data) {
        alert (data);
    });
});

The PHP file preview_uploader.php:

<?php
    if ($_POST["action"] == "get_test") print "text string test works!";
    if ($_POST["action"] == "get_name") print $_FILES["my_file"]["name"];
    if ($_POST["action"] == "get_size") print $_FILES["my_file"]["size"];
    if ($_POST["action"] == "get_type") print $_FILES["my_file"]["type"];
?>

It works if i make a test with action:"get_test" with php page but it doesn't work with $_FILES["my_file"]["name"] or $_FILES["my_file"]["type"] etc...

Can someone help me find where I am wrong?

Thanks!

+2  A: 

Your JS script is sending a separate request to the server and so your PHP is unaware of the file, and $_FILES["my_file"] is not a valid index.

You do not need to go to the server to get the file name, simply use this to get the file name:

$("input[type=file]").val();

I believe it brings back the path as well, so you will need to strip the path off to get the filename.

If you are trying to get all the additional details (filesize, etc) you will either need to upload the file first (which defeats what you are trying to do) or use a Flash + JavaScript combination like these solutions:

Doug Neiner
It's browser-dependent; Firefox, for example, will only return the leaf name. And it's not straightforward to split the path because you don't know what the path separator is on the user's platform. You can manage it for the majority of cases (Windows, Linux, OS X) by splitting on each slash, whether forward-slash or backslash.
bobince
Sorry, I've been a little bit vague about my objectives with this script. I need it not only to check the filename, but also the size and the other file props to be sure of what I'm ready to upload.Is there a way to get file size, width, height and file type before upload it or am I need to upload the file and check if it's ok?
Vittorio Vittori
Vitto, I updated my comment to address some of what you need. To do this on the client side you will need to either find a Flash+Javascript library that already gets image width and height, or you will need to edit the Flash Source for Uploadify or SWF Upload to also give that information. SWFUpload for instance provides name, filesize but not height and width. JavaScript otherwise has no access to local information like that.
Doug Neiner
@vitto The server won't know anything about a file on the client machine. You either need to use client side scripting to gather the information or deliver the file to the server where it can be analyzed.
bd808
thanks dceiner and thanks bender, I'm new with php and I don't know this rules.
Vittorio Vittori