Reply to Passing file name through AJAX query to the class

Passing file name through AJAX query to the class new!
by Jukka, 6 years, 6 months ago
Just needed some advice on passing the filename to the class using AJAX query. I got it working but cannot get the correct filenaem being used.

I have created HTML code for the image file.
<input type="file" id="file" name="file" />
<input type="button" class="button" value="Upload" id="but_upload">


Whent the upload button is pressed, the jQuery calls AJAX to pass the form data to the php-page for processing.

var fd = new FormData();
var files = $('#file')[0].files[0];
fd.append('file',files);

$.ajax({
	url: 'ajax_upload_image.php',
	type: 'post',
	data: fd,
	contentType: false,
	processData: false,
	success: function(response){
		   $("#ajax_result").html(response);

	},
});

The AJAX passes the file information to the PHP.

However the class.upload.php does not seem to be able to have the reference to the tmp file created when pressing the HTML upload button.

I can get the upload to work using the below lines:-
$filename = $_FILES['file']['name'];
echo "FILENAME : ".  $filename;	
$filename = $_FILES['file']['tmp_name'];
$handle = new Upload($filename);

The $_FILES['file']['name'] has the correct image filename. But if I pass this via $handle = new Upload($filename);, the class complains that the image file is not found.

If I then use the $filename = $_FILES['file']['tmp_name'];, the class find the image file and uploads it correctly, but the filename become the tmp-file name e.g. php3851.jpg instead of the original filename.

Is there a workaround for this? I did not seem to find any AJAX related queries.Reply

Your reply

Name *
Email 
Title *
Text *
CAPTCHA image
Enter the code displayed on the image:
Click on the image to generate another one if it is hard to read it. The case is important