我创建了一个简单的 php 网页,它在表中显示来自 mysql 数据库的数据。用户可以上传特定行的图像,这些图像存储在服务器上的上传文件夹中。我希望能够添加单击特定行按钮的功能,并且该行的图像将下载到用户计算机上以该行的部件号命名的文件夹中。下面是上传图片对话框代码:
//function to open a dialog where the user can upload an image to an existing row
$(function()
{
//$uniqueID = $('#uniqueID').val();
//opens the dialog form using the 'imgupdialog' fields outlined below
$( "#imgupdialog" ).dialog
({
autoOpen: false,
modal: true,
buttons:
{
Cancel: function()
{
$( this ).dialog( "close" );
}
},
});
//when the 'imgup' button is clicked the ID of the row will be obtained and the dialog will open
$(document).on('click', '.imgup', function()
{
//$( ".imgup" ).click(function() {
$('#uniqueID').val(($(this).attr('id')));
$("#imgupdialog").dialog( "open" );
});
});
这是上传图像的代码:
$con = mysql_connect("", "");
if (!$con) {
die("Error: " . mysql_error());
}
mysql_select_db("web", $con);
if(isset($_FILES['files']))
{
$UniqueID = $_POST['var1'];
$desired_dir="uploads/";
$errors= array();
foreach($_FILES['files']['tmp_name'] as $key => $tmp_name )
{
$file_name = $_FILES['files']['name'][$key];
$file_size =$_FILES['files']['size'][$key];
$file_tmp =$_FILES['files']['tmp_name'][$key];
$file_type=$_FILES['files']['type'][$key];
if($file_size > 2097152)
{
$errors[]='File size must be less than 2 MB';
}
$query="INSERT INTO web.PE_upload_data (ID, FILE_NAME, FILE_SIZE, FILE_TYPE) VALUES ('$UniqueID', '$file_name','$file_size','$file_type')";
if(empty($errors)==true){
if(is_dir($desired_dir)==false)
{
mkdir("$desired_dir", 0700); // Create directory if it does not exist
}
if(is_dir("$desired_dir/".$file_name)==false)
{
move_uploaded_file($file_tmp,"$desired_dir/".$file_name);
}else
{ // rename the file if another one exists
$new_dir="$desired_dir/".$file_name.time();
rename($file_tmp,$new_dir) ;
}
mysql_query($query);
}
else
{
print_r($errors);
}
}
if(empty($error))
{
echo "Success";
}
}
任何帮助将不胜感激!