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

标签 php javascript html

我创建了一个简单的 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";
}
      }

任何帮助将不胜感激!

最佳答案

正如许多人所指出的,浏览器不允许您访问本地计算机。如果您使用Flash、Java 或我最喜欢的Silverlight 保存到IsolatedStorage 文件夹,有多种解决方法。以下是一堆使用隔离存储的示例代码:http://msdn.microsoft.com/en-us/library/cc265154(v=vs.95).aspx

关于php - 从 PHP 网页下载图像到文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15933089/

相关文章:

php - 评论系统 CSS 文本开箱即用

php - Symfony2 : mime_content_type() not recognized

html - 如何在 Linux 中使用 GUI 构建器为 Html 表单提供干净的代码?就像在 Windows 中使用 Dreamweaver 一样

c# - 没有类型为 'IEnumerable<SelectListItem>' 且键为 '......' 的 ViewData 项

php - 在生产中使用 PHP 内置服务器

PHP 在 <a - 标签之前添加空格

javascript - Azure 移动服务 Javascript 库和更新 - 无法读取方法

javascript - Dialogflow 实现访问代码中的实体

javascript - 如何在每个 jquery 链接后添加 "br"?

css - 在 .row 中对齐图标和文本的垂直中心