函数类似于Javascript中的move_uploaded_file()

时间:2014-04-05 05:10:33

标签: javascript php jquery html

您好我已经为上传文件编写了代码。我只是想在Javasript / JQuery代码中编写类似move_uploaded_file()的条件,或者告诉我如何将这些值传递给我的Javascript / JQuery,以便我可以通过{{<li>将{image}文件插入insertImage() 1}}。

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代码

$('#addImageLink').click(function(e)
 {`gallery.insertImage('<li><img src="" alt=""></li>`)`}`

2 个答案:

答案 0 :(得分:0)

$('#addImageLink').click(function(e){
    $.post( "upload.php", function( data ) {
       $("#anydiv").append(data);
       //gallery.insertImage('<li><img src="" alt=""></li>');
    });
});

变量数据包含upload.php文件中打印的内容。

答案 1 :(得分:0)

因此,在提交表单时,请抓住提交过程并

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);
}

然后,在服务器端(即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);

或类似的东西。我可能会弄错(如果我,请,请更正我),但这应该将文件存储为服务器上/ uploads /中的1287916771myPicture.jpg,并使用JSON变量(对continueSubmission()函数)进行响应在服务器上包含fileName。