2

For saving a document/image in PHP, I am using move_uploaded_file($tmp, "location").

I'm getting the value of the document/image while the file is changing, so I got the file name, but I can't create tmp_name, because I'm getting name in the following way:

HTML:

<form action="" method="post" enctype="multipart/form-data">
    <input type="file" name="file" id="upload" onChange="change(this.value)" />
    <input type="submit" name="chk" value="Submit" />
    <div id="div"></div>
</form>

JavaScript:

<script src="js/jquery1.js"></script>
<script>
function change(val){
    $.ajax({
        url:"filecheck.php?id=" + val,
        success:function(res){
            alert(res);
        }
    });
}

PHP: filecheck.php

mysql_connect("localhost","root","");
mysql_select_db("newdatabase");

if(isset($_GET['id']))
{
    $name=$_GET['id'];
    $tmp=$_GET['id']['tmp_name']; //here i need temp file name
    move_uploaded_file($tmp, "C:/xampp/htdocs/jerome/project/newimage/".$name); 
    echo $name;
}

If I get the filename by $_FILES['file']['name'], it means I can get $_FILES['file']['tmp_name'], but I can't get by this way. Can you please give me a suggestion on how to get $_FILES['file']['tmp_name']?

4
  • 2
    You're referencing $_GET and not $_FILES..... That will be your issue. Commented Jul 22, 2014 at 7:49
  • yes i am getting file name while onchange.. see the script Commented Jul 22, 2014 at 7:57
  • Does not matter, tmp name will never be the original filename.. U have to use the $_FILES array for this (sanwebe.com/2012/06/ajax-file-upload-with-php-and-jquery) Commented Jul 22, 2014 at 7:58
  • 1
    calling a function on onChange event doesn't mean that you are uploading a file. So $_FILES will be empty at that time. Certainly you are doing it wrong. Commented Jul 22, 2014 at 8:03

2 Answers 2

1

Your issue is that you're trying to access the file from the $_GET superglobal instead of the $_FILES superglobal where it would be held.

A simple check with the below code would show you the file data:

print_r($_FILES);

There is also an issue with your ajax.

function change(val){
    $.ajax({
        url:"filecheck.php?id=" + val,
        type: "post",
        data: $('#yourformid').serialize()
        success:function(res){
            alert(res);
        }
    });
}

I can see you're trying to do an instant upload via ajax. The way you are doing it now is bad.

You should look at the following tutorials to grasp an understanding of how it should be done:

Otherwise you'd just check like this:

if(!empty($_FILES)) {

    $name = $_FILES['file']['name'];
    $tmp = $_FILES['file']['tmp_name'];
}

NOTE

Look at code-jaffs comment that verifies my point above stating that your files won't upload the way you are currently doing this.

Sign up to request clarification or add additional context in comments.

3 Comments

U got a copy/paste error in the last link (google one)
@Darren, Thanks. Will try another way
i am using this plugin get what i want a2zwebhelp.com/jquery-php-file-upload..
0

The problem is with $tmp=$_GET['id']['tmp_name'];..This should be changed to $tmp=$_FILES['file']['tmp_name']; as code-jaff says..

The usage of get can be found here http://php.net/manual/en/reserved.variables.get.php

2 Comments

i can only get file name by GET, can not get by FILES[].. because it's in another page, Is there any way create tmp_name by GET?
@user3822048 see $_FILES['file']['tmp_name']; will contain the temporary file name of the file on the server. This is just a placeholder on your server until you process the file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.