Hello I had written a code for upload file. I just wanted to write similar condition like move_uploaded_file()
in Javasript/JQuery code or otherwise tell me how to pass those value to my Javascript/JQuery so that I can insert my image file in those <li>
via insertImage()
.
upload.php
<?php
$name = $_FILES['file']['name'];
$tmp_name = $_FILES['file']['tmp_name'];
if(isset($name))
{
if(!empty($name))
{
$location ='images/';
if(move_uploaded_file($tmp_name, $location.$name))
{`echo '<table width="50%" border="0" cellpadding="4" cellspacing="0">';`
echo '<tr>';
echo '</tr><tr>';
echo '<td align="center"><img src="images/'.$name.'" alt="Resized Image"></td>';
echo '</tr>';
echo '</table>';
echo "Uploaded!!";
}
else {
echo "Please choose the file";
}
}
Jquery code
$('#addImageLink').click(function(e)
{`gallery.insertImage('<li><img src="" alt=""></li>`)`}`
$('#addImageLink').click(function(e){
$.post( "upload.php", function( data ) {
$("#anydiv").append(data);
//gallery.insertImage('<li><img src="" alt=""></li>');
});
});
the variable data contains what is printed in upload.php file.
So, when the form is submitted, catch the submission process and
var file = document.getElementById('fileBox').files[0]; //Files[0] = 1st file
var reader = new FileReader();
reader.readAsText(file, 'UTF-8');
reader.onload = shipOff;
//reader.onloadstart = ...
//reader.onprogress = ... <-- Allows you to update a progress bar.
//reader.onabort = ...
//reader.onerror = ...
//reader.onloadend = ...
function shipOff(event) {
var result = event.target.result;
var fileName = document.getElementById('fileBox').files[0].name; //Should be 'picture.jpg'
$.post('/myscript.php', { data: result, name: fileName }, continueSubmission);
}
Then, on the server side (ie myscript.php):
$data = $_POST['data'];
$fileName = $_POST['fileName'];
$serverFile = time().$fileName;
$fp = fopen('/uploads/'.$serverFile,'w'); //Prepends timestamp to prevent overwriting
fwrite($fp, $data);
fclose($fp);
$returnData = array( "serverFile" => $serverFile );
echo json_encode($returnData);
Or something like it. I may be mistaken (and if I am, please, correct me), but this should store the file as something like 1287916771myPicture.jpg in /uploads/ on your server, and respond with a JSON variable (to a continueSubmission() function) containing the fileName on the server.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.