我正在使用PHP,jQuery,AJAX和MySql。我正在使用 move_uploaded_file 函数将图像存储在本地文件夹和数据库中图像的路径中。我的图像路径是存储n DB。但图像本身不会转到本地文件夹。
upload.php的
<form id="mmyForm" action="mypage.php" method="POST" enctype="multipart/form-data">
<input type="file" id="photo" name="photo">
<?php
$output_dir = "myfolder/";
if(isset($_FILES["photo"]))
{
if ($_FILES["photo"]["error"] > 0)
{
$result['error'] = "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
move_uploaded_file( $_FILES["photo"]["tmp_name"],$output_dir. $_FILES["photo"]["name"] );
$result['success'] = "Uploaded Document :".$_FILES["photo"]["name"];
}
}
else
{
$result['error'] = 'upload document';
}
?>
</form>
我的JavaScript
$('#Save').click(function() {
var pht = $('#photo').val();
var str2 = "myfolder/";
var upvid = str2.concat(pht);
$.ajax({
type:"POST",
url:"mypage.php",
data: { upvid : upvid, disp : "yes" },
success: function(data) {
$("#content").load("mypage.php");
}
});
});
使用数组
if(isset($_REQUEST['disp']))
{
array_pop( $_POST );
$upload_type= $media->AddPhtImage($_POST, 'mytable');
return true;
}
function.php
function AddPhtImage($varArray,$tablename ){
if(!$this->DBLogin())
{
$this->HandleError("Database login failed!");
return false;
}
$a=$varArray['upvid'];
$file = str_replace( "\\", '/', $a );
$ofile = basename( $file );
$oofile ="myfolder/".$ofile;
$date = date('m/d/Y', time());
$qry = "INSERT INTO $tablename ( image ) VALUES ('".$oofile."') ";
$result = mysql_query($qry,$this->connection);
return $result;
}