从PHP网页下载图像到文件夹
我创建了一个简单的php网页,在表中显示来自mysql数据库的数据。用户可以上载特定行的图像,这些图像存储在服务器上的上载文件夹中。我想能够添加功能,点击一个特定行的按钮,该行的图像将下载到用户机器上的一个以该行的零件号命名的文件夹中。以下是“上载图像”对话框代码:从PHP网页下载图像到文件夹,php,javascript,html,Php,Javascript,Html,我创建了一个简单的php网页,在表中显示来自mysql数据库的数据。用户可以上载特定行的图像,这些图像存储在服务器上的上载文件夹中。我想能够添加功能,点击一个特定行的按钮,该行的图像将下载到用户机器上的一个以该行的零件号命名的文件夹中。以下是“上载图像”对话框代码: //function to open a dialog where the user can upload an image to an existing row $(function() { //$uniqueID = $
//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";
}
}
任何帮助都将不胜感激 正如许多人所指出的,浏览器不允许您访问本地计算机。如果使用Flash、Java或我最喜欢的Silverlight保存到IsolatedStorage文件夹,则有多种解决方法。下面是一组使用独立存储的示例代码:用户无法从服务器下载文件夹,或者服务器无法在用户计算机上创建文件夹,您可以创建一个包含图像的压缩文件夹zip,它将可下载。但是,一些托管服务器出于安全原因禁用了zlib库,因此最好先检查一下。您好,谢谢。我只是不确定如何进行下载…只是想确认您无法指定下载文件的位置,只有用户可以这样做。如果你真的想保持文件夹结构的话,可以使用zip文件。或tar,但它通常不包含在某些操作系统中,例如windows,但人们可以下载软件来打开这些文件