简体   繁体   English

从PHP网页将图像下载到文件夹

[英]Download images to folder from PHP web page

I have created a simple php web page that displays data from a mysql database in a table. 我创建了一个简单的php网页,该网页在表中显示mysql数据库中的数据。 Users can upload images for specific rows and these are stored in an upload folder on a server. 用户可以上传特定行的图像,这些图像存储在服务器上的上传文件夹中。 I would like to be able to add the ability to click a button for a specific row and the image(s) for that row will be downloaded onto the users machine in a folder named after the part number of the row. 我希望能够添加单击特定行的按钮的功能,并且该行的图像将以该行的部件号命名的文件夹中下载到用户计算机上。 Here is the upload image dialog box code: 这是上传图像对话框代码:

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

});

And here is the code that uploads the image: 这是上传图像的代码:

$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";
}
      }

Any help would be greatly appreciated! 任何帮助将不胜感激!

As many people have noted, the browser does not allow you to access the local machine. 正如许多人指出的那样,浏览器不允许您访问本地计算机。 There are various workarounds if you use Flash, Java, or my favorite - Silverlight saving to the IsolatedStorage folder. 如果使用Flash,Java或我最喜欢的方法,则有多种解决方法-将Silverlight保存到IsolatedStorage文件夹中。 Here is a bunch of sample code for using isolated storage: http://msdn.microsoft.com/en-us/library/cc265154(v=vs.95).aspx 这是一堆使用隔离存储的示例代码: http : //msdn.microsoft.com/zh-cn/library/cc265154(v=vs.95).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM